Saturday 17 September 2011

BRESENHAM’S CIRCLE ALGORITHM


BRESENHAM’S CIRCLE ALGORITHM

Aim:
     To write a C++ Program to draw a line using Bresenhams algorithm.


Algorithm:
Start the program .
Initialize the variables.
Call the initgraph() function
Get the values for left and right end points for drawing a line..
Calculate the distance to be traveled by the line.
Put the pixels in the given color to draw the line.
Display the output.
stop the program



PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,p;
void plotcircle(int xc,int yc,int color)
{
putpixel(xc+x,yc+y,color);
putpixel(xc+y,yc+x,color);
putpixel(xc+x,yc-y,color);
putpixel(xc+y,yc-x,color);
putpixel(xc-x,yc-y,color);
putpixel(xc-y,yc-x,color);
putpixel(xc-x,yc+y,color);
putpixel(xc-y,yc+x,color);
}
void bicircle(int xcen,int ycen,int rad,int color)
{
x=0;
y=rad;
plotcircle(xcen,ycen,color);
p=1-rad;
while(x<y)
{
if(p<0)
++x;
else
{
++x;
--y;
}
if(p<0)
p+=2*x+1;
else
{
p+=2*(x-y)+1;
plotcircle(xcen,ycen,color);
}
}
}
void main()
{
int gd=DETECT,gm,xc,yc,radius,color;
initgraph(&gd,&gm,"");
printf("\nEnter x pos,y pos and radius");
scanf ("%d%d%d",&xc,&yc,&radius);
x=0;
y=radius;
plotcircle(xc,yc,color);
p=1-radius;
while(x<y)
{
if(p<0)
++x;
else
{
++x;
--y;
}
if(p<0)
p+=2*x+1;
else
{
p+=2*(x-y)+1;
plotcircle(xc,yc,color);
}
}
getch();
bicircle(xc,yc,radius,color);
}

0 comments:

Post a Comment