Bresenham’s Line Drawing Method



ALGORITHM

  1.              Read the line end points  (x1,y1) & (x2,y2) such that they are not equal.
  2.             If the points are same, plot the points & exit (Else continue with the            calculation)
  3.             Calculate
     rx  =  |x2-x1|
     ry  =  |y2-y1|
4.               [initialize the starting point]
                  x=x1
                   y=y1
5.               e = 2 * ry - rx
                  [initialize value of decision variable or error to compensate for non zero                                    intercepts]
6.                              i = 1[initialize counter]
7.                              plot(x , y)
8.                              while (e ≥ 0)
                             {
                                    y = y+1                                   
                                    e =  e – 2 * rx
                               }  
                                    x = x+1
                                    e =  e+2 * ry
       9.                i = i+1
       10.              if ( i ≤ rx )  then go to step 6.
       11.              Stop.  


 C code

 #include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
      int gd = DETECT, gm;
      int dx, dy, p, end;
      float x1, x2, y1, y2, x, y;
      initgraph(&gd, &gm, "c:\tc\bgi");
      printf("Enter Value of X1: ");
      scanf("%f", &x1);
      printf("Enter Value of Y1: ");
      scanf("%f", &y1);
      printf("Enter Value of X2: ");
      scanf("%f", &x2);
      printf("Enter Value of Y2: ");
      scanf("%f", &y2);
      dx = abs(x1 - x2);
      dy = abs(y1 - y2);
      p = 2 * dy - dx;
      if(x1 > x2)
      {
            x = x2;
            y = y2;
            end = x1;
      }
      else
      {
            x = x1;
            y = y1;
            end = x2;
      }
      putpixel(x, y, 10);
      while(x < end)
      {
            x = x + 1;
            if(p < 0)
            {
                  p = p + 2 * dy;
            }
            else
            {
                  y = y + 1;
                  p = p + 2 * (dy - dx);
            }
            putpixel(x, y, 10);
      }
      getch();
      closegraph();
}
                       

Comments

Popular posts from this blog

PROCEDURE TO CREATE AN ANIMATION TO REPRESENT THE GROWING MOON.

PROCEDURE TO CREATE AN ANIMATION TO INDICATE A BALL BOUNCING ON STEPS.

PROCEDURE TO CHANGE A CIRCLE INTO A SQURE USING FLASH.