forked from devaprasad-n-m/CG_LAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_circle.py
117 lines (95 loc) · 2.87 KB
/
4_circle.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
def ClearScreen():
glClearColor(0.0, 0.0, 0.0, 1.0)
gluOrtho2D(-100.0, 100.0, -100.0, 100.0)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.0, 1.0, 0.0)
glPointSize(5.0)
def SetPixel(x, y):
glBegin(GL_POINTS)
glVertex2f(x, y)
glEnd()
glFlush()
# MidPointCircle_Method
def MidPointCircle(r, xc, yc):
x, y = 0, r
SetPixel(x+xc, y+yc)
SetPixel(x+xc, -y+yc)
SetPixel(y+xc, x+yc)
SetPixel(-y+xc, x+yc)
p = 5/4-r
while x < y:
x = x+1
if p < 0:
p = p+2*x+1
else:
y = y - 1
p = p+2*x-2*y+1
SetPixel(x+xc, y+yc)
SetPixel(-x+xc, y+yc)
SetPixel(-x+xc, -y+yc)
SetPixel(x+xc, -y+yc)
SetPixel(y+xc, x+yc)
SetPixel(-y+xc, x+yc)
SetPixel(-y+xc, -x+yc)
SetPixel(y+xc, -x+yc)
#PolarCircle_Method
def PolarCircle(r,xc,yc):
x,y=0,r
angle = 0
end_angle = (22/7)/4
while angle <= end_angle:
angle += 0.001
x = r*math.cos(angle)
y = r*math.sin(angle)
SetPixel(x+xc, y+yc)
SetPixel(-x+xc, y+yc)
SetPixel(-x+xc, -y+yc)
SetPixel(x+xc, -y+yc)
SetPixel(y+xc, x+yc)
SetPixel(-y+xc, x+yc)
SetPixel(-y+xc, -x+yc)
SetPixel(y+xc, -x+yc)
#NonPolarCircle_Method
def NonPolarCircle(r,xc,yc):
x,y=0,r
while x <= y:
y -= 0.01
x = math.sqrt(r*r-y*y)
SetPixel(x+xc, y+yc)
SetPixel(-x+xc, y+yc)
SetPixel(-x+xc, -y+yc)
SetPixel(x+xc, -y+yc)
SetPixel(y+xc, x+yc)
SetPixel(-y+xc, x+yc)
SetPixel(-y+xc, -x+yc)
SetPixel(y+xc, -x+yc)
def Main():
choice = 0
while choice != 4:
choice=int(input("Choose the Algorithm \n 1)Mid_Point Algorithm \n 2)Polar Algorithm \n 3)NonPolar Algorithm\n 4)exit \n"))
if choice==1 or choice==2 or choice==3:
r = float(input("Enter the Radius"))
print("Enter the Center of the Circle")
c1 = float(input("xc = "))
c2 = float(input("yc = "))
print("starting window....")
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(500, 500)
glutInitWindowPosition(200, 200)
glutCreateWindow("Circle")
if choice ==1:
glutDisplayFunc(lambda :MidPointCircle(r, c1, c2))
if choice ==2:
glutDisplayFunc(lambda :PolarCircle(r, c1, c2))
if choice ==3:
glutDisplayFunc(lambda :NonPolarCircle(r, c1, c2))
ClearScreen()
glutMainLoop()
else :
print("Invalid Choice")
Main()