CG -2 Circle Drawing using DDA, Midpoint and Bresenham's Algorithm

Circle Drawing using DDA, Midpoint and Bresenham's Algorithm

DDA Algorithm

/* Refer page 59 from Computer Graphics by A.P. Godse */
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
float x1,y1,x2,y2,startx,starty,epsilon;
int gd,gm,i,val;
int r;
clrscr();

/* Read two end points of line
---------------------------------- */ 
printf("Enter the radius of a circle :");
scanf("%d",&r);

/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

/* Initialise starting point
-------------------------------- */
x1=r*cos(0);
y1=r*sin(0);
startx = x1;
starty = y1;

/*Calculations for epsilon 
-----------------------------*/
i=0;
do
{
val = pow(2,i);
i++;
}while(val<r);
epsilon = 1/pow(2,i-1);


do
   {
    x2= x1 + y1*epsilon;
    y2 = y1 - epsilon*x2;
    putpixel(200+x2,200+y2,15);

/* Reinitialise the current point 
---------------------------------- */
    x1=x2;
    y1=y2;
    delay(1000);   /* Delay is purposely inserted to see
     observe the line drawing process */
    }
  while( (y1 - starty ) < epsilon || (startx - x1) > epsilon);
getch();
closegraph();

}
Bresenham's Algorithm
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>

void main()
{
 int gd=DETECT,gm;
 int r,x,y,p,xc=320,yc=240;

 initgraph(&gd,&gm,"C:\\TC\\BGI");
 cleardevice();

 printf("Enter the radius ");
 scanf("%d",&r);
 x=0;
 y=r;
 putpixel(xc+x,yc-y,1);

 p=3-(2*r);

 for(x=0;x<=y;x++)
 {
  if(p<0)
  {
   y=y;
   p=(p+(4*x)+6);
  }
  else
  {
   y=y-1;
   p=p+((4*(x-y)+10));
  }
  putpixel(xc+x,yc-y,4);
  putpixel(xc-x,yc-y,4);
  putpixel(xc+x,yc+y,4);
  putpixel(xc-x,yc+y,4);
  putpixel(xc+y,yc-x,5);
  putpixel(xc-y,yc-x,5);
  putpixel(xc+y,yc+x,5);
  putpixel(xc-y,yc+x,5);
 }
 getch();
 closegraph();
}

Midpoint Algorithm

# include<stdio.h>
// C program for implementing
// Mid-Point Circle Drawing Algorithm
#include<stdio.h>
 
// Implementing Mid-Point Circle Drawing Algorithm
void midPointCircleDraw(int x_centre, int y_centre, int r)
{
    int x = r, y = 0;
     
    // Printing the initial point on the axes 
    // after translation
    printf("(%d, %d) ", x + x_centre, y + y_centre);
     
    // When radius is zero only a single
    // point will be printed
    if (r > 0)
    {
        printf("(%d, %d) ", x + x_centre, -y + y_centre);
        printf("(%d, %d) ", y + y_centre, x + x_centre);
        printf("(%d, %d)\n", -y + y_centre, x + x_centre);
    }
     
    // Initialising the value of P
    int P = 1 - r;
    while (x > y)
    { 
        y++;
         
        // Mid-point is inside or on the perimeter
        if (P <= 0)
            P = P + 2*y + 1;
             
        // Mid-point is outside the perimeter
        else
        {
            x--;
            P = P + 2*y - 2*x + 1;
        }
         
        // All the perimeter points have already been printed
        if (x < y)
            break;
         
        // Printing the generated point and its reflection
        // in the other octants after translation
        printf("(%d, %d) ", x + x_centre, y + y_centre);
        printf("(%d, %d) ", -x + x_centre, y + y_centre);
        printf("(%d, %d) ", x + x_centre, -y + y_centre);
        printf("(%d, %d)\n", -x + x_centre, -y + y_centre);
         
        // If the generated point is on the line x = y then 
        // the perimeter points have already been printed
        if (x != y)
        {
            printf("(%d, %d) ", y + y_centre, x + x_centre);
            printf("(%d, %d) ", -y + y_centre, x + x_centre);
            printf("(%d, %d) ", y + y_centre, -x + x_centre);
            printf("(%d, %d)\n", -y + y_centre, -x + x_centre);
        }
    } 
}
 
// Driver code
int main()
{
    // To draw a circle of radius 3 centred at (0, 0)
    midPointCircleDraw(0, 0, 3);
    return 0;
}

Comments