Drawing a Circle with Mid – Point Circle Algorithm in C/C++

As in the previous line drawing algorithm, we sample at unit intervals and determine the closest pixel position to the specified circle path at each step. For a given radius r and screen center position (xc, yc), we can first set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0, 0). Then each calculated  position (x, y) is moved to its proper screen position by adding xc to x and yc to y. Along the circle section from x = 0 to x = y in the first quadrant, the slope of the curve varies from 0 to –1.  Therefore, we can take unit steps in the positive x direction over this octant and use a decision parameter to determine which of the two possible y positions is closer to the circle path at each step. Position in the other seven octants are then obtained by symmetry.

The following section implements Mid – Point Circle  Algorithm in C/C++. The source code is compiled using gcc Compiler and Code::Blocks IDE. To print a pixel, SetPixel() function of windows.h is used.

Note: to run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGWlib )  in linker setting.
Souce Code

Output SHARE Drawing a Circle with Mid – Point Circle Algorithm in C/C++

5 Responses

1. bishnu syangja says:

your circle mid-point line drawing algorithm when running in code blocks displays two errors in error logs .The errors are given below:
line 3…..error: cmath: No such file or directory
line 27…error: expected ';', ',' or ')' before '&' token

2. Bibek123 says:

@bishnu:- This is fully tested code. Make your filename .cpp not .c.

3. Bonny Kamal says:

works great …

4. JeCsOn says:

hmmmmm

5. loseweightngainmuscle.com says:

Hey there! Would you mind if I share your blog with my twitter group?
There’s a lot of folks that I think would really appreciate 