# Computer graphics program to implement Midpoint Circle Algorithm

0
0

## Program in Python graphics.py and C++ graphics library

### Python

``````# Mid-Point Circle Drawing Algorithm
from graphics import *
import time

def midPointCircleDraw(x_centre, y_centre, r):
win = GraphWin('Brasenham Line', 600, 480)
x = r
y = 0

print("(", x + x_centre, ", ",
y + y_centre, ")",
sep = "", end = "")

if (r > 0) :

print("(", x + x_centre, ", ",
-y + y_centre, ")",
sep = "", end = "")
print("(", y + x_centre, ", ",
x + y_centre, ")",
sep = "", end = "")
print("(", -y + x_centre, ", ",
x + y_centre, ")", sep = "")

P = 1 - r

while x > y:

y += 1

if P <= 0:
P = P + 2 * y + 1

else:
x -= 1
P = P + 2 * y - 2 * x + 1

if (x < y):
break

x1 = x + x_centre
y1 = y + y_centre

x2 = -x + x_centre
y2 = y + y_centre

x3 = x + x_centre
y3 = -y + y_centre

x4 = -x + x_centre
y4 = -y + y_centre
PutPixle(win, x1,y1)
PutPixle(win, x2,y2)
PutPixle(win, x3,y3)
PutPixle(win, x4,y4)
print("(", x1, ", ", y1,
")", sep = "", end = "")
print("(", x2, ", ", y2,
")", sep = "", end = "")
print("(", x3, ", ", y3,
")", sep = "", end = "")
print("(", x4, ", ", y4,
")", sep = "")

if x != y:
x1 = y + x_centre
y1 = x + y_centre

x2 = -y + x_centre
y2 = x + y_centre

x3 = y + x_centre
y3 = -x + y_centre

x4 = -y + x_centre
y4 =  -x + y_centre

PutPixle(win, x1,y1)
PutPixle(win, x2,y2)
PutPixle(win, x3,y3)
PutPixle(win, x4,y4)

print("(", x1, ", ", y1,
")", sep = "", end = "")
print("(", x2, ", ", y2,
")", sep = "", end = "")
print("(", x3, ", ", y3,
")", sep = "", end = "")
print("(", x4, ", ", y4,
")", sep = "")
win.getMouse()
win.close()
def PutPixle(win, x, y):
pt = Point(x,y)
pt.draw(win)
if __name__ == '__main__':
x,y = map(int, input("Enter center coordinates:").split())
midPointCircleDraw(x, y, r)
``````

C++

``````#include <graphics.h>
#include <iostream>
#include <conio.h>
#include <math.h>

using namespace std;

void midPointCircleDraw(int x_centre, int y_centre, int r){
int x = r, y = 0;
putpixel( x + x_centre, y + y_centre, WHITE);

if (r > 0) {
putpixel( x + x_centre, -y + y_centre, WHITE);
cout << x + x_centre << " "<<   -y + y_centre << endl;
putpixel( y + x_centre, x + y_centre, WHITE);
cout <<  y + x_centre << " "<<  x + y_centre << endl;
putpixel( -y + x_centre, x + y_centre, WHITE);
cout <<  -y + x_centre << " "<<  x + y_centre << endl;
}

int P = 1 - r;
while (x > y) {
y++;

if (P <= 0)
P = P + 2*y + 1;

else{
x--;
P = P + 2*y - 2*x + 1;
}

if (x < y)
break;

putpixel( x + x_centre, y + y_centre, WHITE);
cout << x + x_centre << " "<<  y + y_centre << endl;
putpixel( -x + x_centre, y + y_centre, WHITE);
cout << -x + x_centre << " "<<  y + y_centre << endl;
putpixel( x + x_centre, -y + y_centre, WHITE);
cout << x + x_centre << " "<<  -y + y_centre << endl;
putpixel( -x + x_centre, -y + y_centre, WHITE);
cout <<  -x + x_centre << " "<<  -y + y_centre << endl;

if (x != y) {
putpixel( y + x_centre, x + y_centre, WHITE);
cout << y + x_centre << " "<<  x + y_centre << endl;
putpixel( -y + x_centre, x + y_centre, WHITE);
cout << -y + x_centre << " "<<  x + y_centre << endl;
putpixel( y + x_centre, -x + y_centre, WHITE);
cout << y + x_centre << " "<<  -x + y_centre << endl;
putpixel( -y + x_centre, -x + y_centre, WHITE);
cout << -y + x_centre <<" "<<  -x + y_centre << endl;
}
}
}
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
cout << "Enter x, y, r: ";
int x, y, r;
cin >> x >> y >> r;
midPointCircleDraw(x, y, r);

getch();
closegraph();

return 0;
}
``````
SHARE
Hey! I am one of the 100,000 engineering students in India, with a step forward to support and provide resources to 99,999 other students.