Explaining the Bresenham circle program in C++

NOTE:This article is for C++ intermediate programmers anyone beginning with C++ will find it difficult to understand this article.HI guys!!!!!!!!welcome back to another brand new article on C++,in this article about the famous Bresenhem's circle algorithm and then write a code for it in C++,So let's start.

Explaining the concept behind the Bresenhem's Circle.
Let's see the diagram below,In the below diagram there are three points having axis co-ordinates (X,Y),(X+1,Y),(X+1,Y-1)

Now if we join these points, we need to consider the point which is nearest to the reference arc because we cannot draw a continuous arc in rect display, we have to choose a nearest point from the reference arc to draw the next point whether it will be (X+1,Y) or (X+1,Y-1).so what we are going to do in our code is we first initialize the radius and the initial co-ordinates of the circle. and then we will run a loop in which there will be conditional statement which will decide the next nearest point from the reference arc.In simple words we will first assign the initial co-ordinates of the circle and then we will draw the next co-ordinates using a loop.Link:C++ for beginners Ch-1Writing the algorithm for the Bresenham's Circle
1-Start2-Declare variables for radius,x and y co-ordinates,decision taking variable and for center co-ordinates of the circle.3-Input radius and center-ordinates from the user.4-Set x=0 and input y=r.5-Set decision variable d=3-(2*r).6-Repeat through step-9 until x<=r.7-if d<0 then d=d+4x+6.8-else y=y-1 and d=d+4(x-4)+10.9-execute putpixel(x,y,1).10-Stop.

Writing the code in C++ for the Bresenham's Circle

#include<iostream.h> #include<conio.h> #include<math.h> #include<graphics.h> void main(){ int gd=DETECT,gm; int r,x,y,d,xc,yc; initgraph(&gd,&gm,"C:/TC/BGI"); cleardevice(); cout<<"Enter the radius of the circle"; cin>>r; cout<<"Enter the center co-ordinates of the circle:"; cin>>xc>>yc; x=0; y=r; d=3-(2*r); putpixel(xc+x,yc-y,1);    //Initial coordinates while(x<=r){ if(d<0){ d=d+(4*x)+6; } else{ y=y-1; d=d+(4*(x-4)+10); } putpixel(xc+x,yc-y,1); //drawing pixels using loop by increasing the initial values 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); x++; } getch(); closegraph(); } 

Output of the above code:

So here is the code for the bresenham's circle, this is it for now see you guys in the next article till then goodbye.
Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page.

If you like this article, then please share it and help us grow.

Понравилась статья? Поделиться с друзьями:
Добавить комментарий