Sunday, May 3, 2009

C Program for Bresenham Circle Drawing 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,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);

}
getch();
closegraph();
}

13 comments:

Nameet Nayan said...

this code is working yaar.....thanks..

i want a program for "plot the sin(x),x is 0 to 2pi,using character '*'...if u get it plz mail me ...my id is namit.nayan@gmail.com

Angad said...

I am busy with my final year preps, so ill try to post the program here!

Anonymous said...

thanks for sharing this site. you can download lots of ebook from here

http://feboook.blogspot.com

rick120 said...

and algorithm clipping circles???...please...

reckonme said...

thanks u so much dude....

Anonymous said...

yippyiiiiiiiiii i draw the circle

Anonymous said...

thnx dude.

Anonymous said...

Rohit:
How can intensity of a pixel be controlled ?
Suppose, if I want to assign 30% of any drawing color to a pixel, what should be the code?
How will I pick a shed of a color from color palette or look-up table through coding?
Please help me. mail me in the following e-mail id with answer:
rohitju50@yahoo.com

Anonymous said...

Thank You, you are superb...

murad khan said...

wow!! its work..............

maria said...

Hi

I am trying to compile this code in Visual Studio 2008 but an erro occurs
"fatal error C1083: Cannot open include file: 'graphics.h': No such file or directory"

what is going worng? I cannot compile it to Visual Studio? Can I use another program to compile it

Thanks
Maria

Anonymous said...

The code is not working because of putpixel points are not correctly implemented

Manjesh P M said...

y=y; what this line meant for?