Friday, February 04, 2011

Circle Algorithms



/*
 * THIS IS A C PROGRAM THAT IMPLEMENTS THE FOLLOWING CIRCLE DRAWING ALGORITHMS:
 *          NORMAL RASTERIZATION TECHNIQUE (GENERAL EXPLICIT METHOD)
 *          BRESENHAM'S CIRCLE DRAWING ALGORITHM
 *          MIDPOINT CIRCLE DRAWING ALGORITHM 
 */

#include"compgrap.h"
#include<math.h>
#include<dos.h>

void rasterizecircle(int, int, int) ;
void BresenhamCircle(int,int,int);
void midpointcircle(int, int, int) ;
void plotcirclepoints(int, int, int, int);
void plotothers(int,int,int,int);

int main(void)
{
  int x, y, r  ;
  int choice;

  initialize();                  /* basic initialization */

  /* setting shading style and highlight color of a cell */
  setfillstyle(1, DARKGRAY) ;

  NEXT:
  clrscr();
  cleardevice();

  printf("\n Enter the coordinates of the mid-point:  ");
  scanf("%d%d",&x,&y) ;
  printf("\n Enter the length of radius : ");
  scanf("%d",&r) ;

  clrscr();
  cleardevice() ;

  /* creating a grid */
  creategrid(DARKGRAY, WHITE) ;

  setcolor(WHITE);
  outtextxy(195,8,"Analysis of Circle Algorithms");
  outtextxy(15, 470, "1. Normal ");
  outtextxy(110, 470, "2. Bresenham ");
  outtextxy(250, 470, "3. Midpoint ");
  outtextxy(359, 470, "4. Actual ");
  outtextxy(460, 470, "5. Next");
  outtextxy(530, 470, "6. QUIT");

  do  {
  scanf("%d",&choice);

  switch(choice)  {
   setcolor(DARKGRAY);
   case(1):
             rasterizecircle(x, y, r) ;   /* drawing approximated circle by explicit method*/
             break;
   case(2):
             BresenhamCircle(x, y, r) ;   /* drawing approximated circle using Bresenham algorithm*/
             break;
   case(3):
             midpointcircle(x, y, r) ;   /* drawing approximated circle using Midpoint algorithm*/
             break;
   case(4):
            setcolor(WHITE);
             drawcircle(x,y,r)  ;                   /* drawing actual circle */
             break;
   case(5):
         goto NEXT;
   }
  }while(choice!=6);

   closegraph();            /* closing the graph */
   return 0;
 }

/* following is the function to draw an approximated circle by general explicit method */
void rasterizecircle(int xCenter, int yCenter, int radius)
{
            int x = 0, y ;
            int upperbound = (radius/sqrt(2)) ;
            double y_;

            while (x<=upperbound)           {
                        y_ = sqrt(radius*radius - x*x);  /* for a circle centred at origin */
                        y = (int)y_ ;
                        if(y_ - y<0.5)
                                    plotcirclepoints(xCenter, yCenter, x, y);
                        else
                                    plotcirclepoints(xCenter, yCenter, x, y+1);
                        x++;
            }
}

/* following is the function to draw an approximated circle using Bresenham algorithm */
 void BresenhamCircle(int cx,int cy,int r)
{
            int Delta,delta,x,y,limit;
            x = 0;   
y = r;
            Delta = 2*(1-r);
            limit = 0;
           
            while(y>=limit){
                        plotothers(x,y,cx,cy);
                       
                        if(Delta<0){
                         delta = 2*Delta + 2*y -1;
                         if(delta<=0)     {           /* choose mH(x+1,y) as the next pixel */
                          x = x +1;
                          Delta = Delta + 2*x +1;
                         }
                         else     {                       /* choose mD(x+1,y-1) as the next pixel */
                          x = x +1;
                          y = y -1;
                          Delta = Delta + 2*x - 2*y +2;
                         }
                        }
                        else if(Delta>0){
                         delta = 2*Delta - 2*x -1;
                         if(delta<=0)     {           /* choose mD(x+1,y-1) as the next pixel */
                          x = x +1;
                          y = y -1;
                          Delta = Delta + 2*x - 2*y +2;
                         }
                         else     {                       /* choose mV(x,y-1) as the next pixel */
                          y = y - 1;
                          Delta = Delta - 2*y +1;
                         }                     
                        }
                        else          {       /* Delta == 0, choose mD(x+1,y-1) as the next pixel */
                         x = x +1;
                         y = y -1;
                         Delta = Delta + 2*x - 2*y +2;
                        }
            }
 }

/* following is the function to draw an approximated circle using Midpoint circle algorithm */
void midpointcircle(int xCenter, int yCenter, int radius){

            int x = 0;
            int y = radius;
            int p = 1 - radius;
            plotcirclepoints(xCenter, yCenter, x, y);          /* Plot first set of points */
            while (x < y) {
                        x++;
                        if (p < 0)
                                    p += 2*x + 1;
                        else {
                                    y--;
                                    p += 2 * (x-y) + 1;
                        }
                        plotcirclepoints(xCenter, yCenter, x, y);
            }
}

/* following function accepts a point, plots it and 7 other points from 8-way circle symmetry */
void plotcirclepoints(int xCenter, int yCenter, int x, int y)
{
            plot(xCenter + x, yCenter + y);             delay(300);
            plot(xCenter - x, yCenter + y);             delay(300);
            plot(xCenter + x, yCenter - y);             delay(300);
            plot(xCenter - x, yCenter - y);              delay(300);
            plot(xCenter + y, yCenter + x);            delay(300);
            plot(xCenter - y, yCenter + x);             delay(300);
            plot(xCenter + y, yCenter - x);             delay(300);
            plot(xCenter - y, yCenter - x);              delay(300);
}
/* following function accepts a point in 1st quadrant, plots it and 3 other points in three other quadrants*/
 void plotothers(int x,int y,int cx, int cy)
{
            plot(cx+x,cy+y);           delay(300);
            plot(cx-x,cy+y);            delay(300);
            plot(cx-x,cy-y); delay(300);
            plot(cx+x,cy-y);            delay(300);
}

No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.