Page 126 - Algorithms Notes for Professionals
P. 126

/* A C program to implement Bresenham line drawing algorithm for |m|<1 */
       #include<stdio.h>
       #include<conio.h>
       #include<graphics.h>
       #include<math.h>

       int main()
       {
        int gdriver=DETECT,gmode;
        int x1,y1,x2,y2,delx,dely,p,i;
        initgraph(&gdriver,&gmode,"c:\\TC\\BGI");

       printf("Enter the intial points: ");
       scanf("%d",&x1);
       scanf("%d",&y1);
       printf("Enter the end points: ");
       scanf("%d",&x2);
       scanf("%d",&y2);

       putpixel(x1,y1,RED);

       delx=fabs(x2-x1);
       dely=fabs(y2-y1);
       p=(2*dely)-delx;
       for(i=0;i<delx;i++){
       if(p<0)
       {
        x1=x1+1;
        putpixel(x1,y1,RED);
        p=p+(2*dely);
       }
       else
       {
        x1=x1+1;
        y1=y1+1;
        putpixel(x1,y1,RED);
        p=p+(2*dely)-(2*delx);
       }
       }
        getch();
        closegraph();
        return 0;
       }

       Algorithm for slope |m|>1:


          1.  Input two end points (x1,y1) and (x2,y2) of the line.
          2.  Plot the first point (x1,y1).
          3.  Calculate
             Delx =| x2 – x1 |
             Dely = | y2 – y1 |
          4.  Obtain the initial decision parameter as
             P = 2 * delx – dely
          5.  For I = 0 to dely in step of 1

             If p < 0 then
             y1 = y1 + 1
             Pot(x1,y1)



       colegiohispanomexicano.net – Algorithms Notes                                                           122
   121   122   123   124   125   126   127   128   129   130   131