-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon.circle.py
64 lines (48 loc) · 1.92 KB
/
polygon.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
# polygon.circle.py
import math
import matplotlib.pyplot as plt
def calculate_radii(s, n):
# Calculate the radius R of the circumcircle
R = s / (2 * math.sin(math.pi / n))
# Calculate the radius r of the inscribed circle
r = s / (2 * math.tan(math.pi / n))
return R, r
def plot_polygon_and_circles(s, n):
R, r = calculate_radii(s, n)
# Calculate vertex coordinates of regular polygons
vertices = [
(R * math.cos(2 * math.pi * i / n), R * math.sin(2 * math.pi * i / n))
for i in range(n)
]
vertices.append(vertices[0]) # Add the first point for closed shapes
# Plot Settings
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.grid(True, which='both')
# Drawing an extracorporeal circle
outer_circle = plt.Circle((0, 0), R, color='blue', fill=False, linestyle='--', label='Circle circumscribed by a polygon')
ax.add_artist(outer_circle)
# Draw an inversion circle
inner_circle = plt.Circle((0, 0), r, color='red', fill=False, linestyle='--', label='Circle inscribed in a polygon')
ax.add_artist(inner_circle)
# Draw regular polygons
polygon = plt.Polygon(vertices, edgecolor='green', fill=None, label=f'Definite {n} polygon')
ax.add_artist(polygon)
# axis setting
max_radius = max(R, r)
ax.set_xlim(-max_radius * 1.1, max_radius * 1.1)
ax.set_ylim(-max_radius * 1.1, max_radius * 1.1)
# Add legend
ax.legend()
# Plot display
plt.title(f'Definite {n} squares with side length {s} and other inscribed and inscribed circles')
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.show()
# User Input
s = float(input("Enter the length s of one side of a regular polygon: "))
n = int(input("Enter the number n of sides of a regular polygon (in integers greater than three): "))
if n < 3:
print("n must be an integer greater than or equal to 3.")
else:
plot_polygon_and_circles(s, n)