Here you will get program for midpoint circle algorithm in C and C++. It is an algorithm used in computer graphics for drawing circle.

This program will work in Turbo C or Turbo C++ compiler as it uses graphics.h header file.

Change the path of BGI file inside initgraph() function according to your system to make this program run.

## Program for Midpoint Circle Algorithm in C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include<stdio.h> #include<graphics.h> void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err <= 0) { y += 1; err += 2*y + 1; } if (err > 0) { x -= 1; err -= 2*x + 1; } } } int main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi"); printf("Enter radius of circle: "); scanf("%d", &r); printf("Enter co-ordinates of center(x and y): "); scanf("%d%d", &x, &y); drawcircle(x, y, r); return 0; } |

## Program for Midpoint Circle Algorithm in C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include<iostream.h> #include<graphics.h> void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7); putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err <= 0) { y += 1; err += 2*y + 1; } if (err > 0) { x -= 1; err -= 2*x + 1; } } } int main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi"); cout<<"Enter radius of circle: "; cin>>r; cout<<"Enter co-ordinates of center(x and y): "; cin>>x>>y; drawcircle(x, y, r); return 0; } |

**Output**

Nice

Thank you very much..

hello sir…

while i run this programme in turbo c++ 4.0 compiler, it takes the input of radius, x and y then shows…

BGI Error:Graphics not initialized (use ‘initgraph’)…

bro! read the lines before the program. it is directed “Change the path of BGI file inside initgraph() function according to your system to make this program run”.

change directory to bgi first,then save program again…also check linker to confirm graphics.h file is included or not

YOUR PROGRAM IS CORRECT BUT YOU SHOULD PUT A DELAY

FUNCTION IN WHILE LOOP.

OTHERWISE VERY GOOD FOR PROGRAMME.

your program is totally wrong

This is great! Can you explain the difference between this C algorithm and the C midpoint algorithm on Wikipedia? I don’t understand the difference. The link is: https://en.wikipedia.org/wiki/Midpoint_circle_algorithm#C_example

Interesting example you’ve got here. I’ve tested already few code bits that using Bresenham algorithm, i have to say, your implementation is most ’round’ one ðŸ™‚

Little question, how would you fill the circle with your method?

Cheers,

Ev

Super easy program sir Thanks for great Help ……