Let us understand what is Midpoint circle drawing algorithm in computer graphics and then let us see how to draw a circle using midpoint circle algorithm in C programming using different methods.

#### What is Midpoint Circle Algorithm?

The midpoint circle drawing algorithm constructs a circle using the coordinates of X-axis and Y-axis and creates an image in a vector graphics format. Here, the perimeter points of the circle are plotted on the graph using the X and Y coordinates.

The same algorithm can be used to create a vector graphics image for any 2-dimensional curve such as a parabola, hyperbola, ellipse, etc. Unlike **Bresenham’s line drawing algorithm**, this algorithm can be optimized to perform integer based calculations.

The getmaxy() function used in the second method is available in the graphics.h library. It returns the maximum Y – coordinate for the available driver and graphics mode.

The equation of the circle **x ^{2} + y^{2} = radius^{2}** is used in this circle generation algorithm, and this algorithm is based on Bresenham’s circle drawing algorithm.

However, there are a few disadvantages with this algorithm as well. The execution time for drawing the circle is comparatively high and also the pixel distance is a bit unequal which doesn’t provide a smooth lined circle.

If you face any issues while compiling this algorithm in computer graphics, you may have to **install graphics.h** in your operating system.

**Note:** This C program for Midpoint circle generation algorithm is compiled with Turbo C compiler on Microsoft Windows 10 operating system.

#### Method 1: C Program To Implement Midpoint Circle Algorithm using Function

#include<stdio.h> #include<graphics.h> void midpoint_algorithm(int x0, int y0, int radius); int main() { int graphics_driver = DETECT, graphics_mode; float x0, y0, radius; initgraph(&graphics_driver, &graphics_mode, "c:\\turboc3\\bgi"); printf("\nEnter the radius of Circle:\t"); scanf("%f", &radius); printf("\nEnter the coordinates of the centre point of the circle:\n"); printf("Enter the X - coordinate value:\t"); scanf("%f", &x0); printf("Enter the Y - coordinate value:\t"); scanf("%f", &y0); midpoint_algorithm(x0, y0, radius); return 0; } void midpoint_algorithm(int x0, int y0, int radius) { float dx = 1, dy = 1; float x = radius - 1, y = 0; float error = dx - (radius << 1); for(;x >= y;) { putpixel(x0 + x, y0 + y, 5); putpixel(x0 + y, y0 + x, 5); putpixel(x0 - y, y0 + x, 5); putpixel(x0 - x, y0 + y, 5); putpixel(x0 - x, y0 - y, 5); putpixel(x0 - y, y0 - x, 5); putpixel(x0 + y, y0 - x, 5); putpixel(x0 + x, y0 - y, 5); if(error > 0) { x = x - 1; dx = dx + 2; error = error + (-radius << 1) + dx; } if(error <= 0) { y = y + 1; error = error + dy; dy = dy + 2; } } }

#### Method 2: Midpoint Circle Drawing algorithm in C Programming without Function

#include<stdio.h> #include<graphics.h> int main() { int graphics_driver = DETECT, graphics_mode = DETECT; float radius, x, y, midpoint_x, midpoint_y, error; initgraph(&graphics_driver, &graphics_mode ,"c:\\turboc3\\bgi"); printf("Enter the Radius of circle:\t"); scanf("%d", &radius); x = 0; y = radius; error = 1.30 - radius; midpoint_x = getmaxx() / 2; midpoint_y = getmaxy() / 2; while(x <= y) { putpixel(midpoint_x + x, midpoint_y + y, 4); putpixel(midpoint_x - x, midpoint_y - y, 4); putpixel(midpoint_x - x, midpoint_y + y, 4); putpixel(midpoint_x + x, midpoint_y - y, 4); putpixel(midpoint_x + y, midpoint_y + x, 4); putpixel(midpoint_x + y, midpoint_y - x, 4); putpixel(midpoint_x - y, midpoint_y - x, 4); putpixel(midpoint_x - y, midpoint_y + x, 4); x = x + 1; if(error < 0) { error = error + 2 * x + 1; } else { y = y - 1; error = error + 2 * (x - y) + 1; } } closegraph(); return 0; }

#### Output

Enter the radius of Circle: 10

Enter the coordinates of the centre point of the circle:

Enter the X – coordinate value: 20

Enter the Y – coordinate value: 10

Let us discuss more on this midpoint circle drawing algorithm in C programming in the comment section below.

Do let us know if you have any doubts or have any information to share. For information, check Wikipedia.

This circle drawing algorithm can be manipulated for integer based calculations.