-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_bresenham_circle_drawing.py
More file actions
43 lines (38 loc) · 1.07 KB
/
7_bresenham_circle_drawing.py
File metadata and controls
43 lines (38 loc) · 1.07 KB
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
import matplotlib.pyplot as plt
def bresenham_circle(xc, yc, r):
points = []
x = 0
y = r
p = 3 - 2 * r
while x <= y:
points.append((xc+x, yc+y))
points.append((xc+x, yc-y))
points.append((xc-x, yc+y))
points.append((xc-x, yc-y))
points.append((xc+y, yc+x))
points.append((xc+y, yc-x))
points.append((xc-y, yc+x))
points.append((xc-y, yc-x))
if p < 0:
x = x + 1
p = p + 4 * x + 6
else:
x = x + 1
y = y - 1
p = p + 4 * (x - y) + 10
return points
xc, yc = 5, 5
radius = 3
circle_points = bresenham_circle(xc, yc, radius)
x_coords, y_coords = zip(*circle_points)
plt.figure(figsize=(7,7))
plt.scatter(xc, yc, c='red', s=50, label='Circle Center')
plt.scatter(x_coords, y_coords, c='blue', s=50, label='Circle Points')
circle = plt.Circle((xc, yc), radius, color='green', fill=False, label='Ideal Circle')
plt.gca().add_patch(circle)
plt.gca().set_aspect('equal')
plt.xlim(0, 10)
plt.ylim(0, 10)
plt.grid(True)
plt.legend()
plt.show()