# Midpoint Circle Algorithm in C and C++

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

```#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++

```#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

### 19 thoughts on “Midpoint Circle Algorithm in C and C++”

1. 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’)…

1. 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”.

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

2. YOUR PROGRAM IS CORRECT BUT YOU SHOULD PUT A DELAY
FUNCTION IN WHILE LOOP.
OTHERWISE VERY GOOD FOR PROGRAMME.

3. 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

4. sorry i could’t understand the need of 7 in this program. why not another numbers

1. You also change your colour code with other number , like 5 is for pink ..
And as well as you can use colour name in Capital letter ” GREEN ” like this..

5. Captain Deadpool

#include
#include
#include
#include
void main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, “c:\\turboc3\\bgi”);
cout<>r;
cout<>x>>y;
int x0=x;
int y0=y;
x = 0;
y = r;
int pk = 1-r;

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);
delay(10);

if (pk < 0)
{
x += 1;
pk = pk + 2*x + 1;
}
else
{
x += 1;
y -= 1;
pk = pk – 2*y + 2*x + 1;
}
}
getch();
closegraph();
}

6. draw a circle using midpoint circle algorithm then draw two lines cross sectioned with each other above the circle where section point is centre of the circle. How to solve this???