Skip to content

Commit 4cf6cee

Browse files
committed
add py file
1 parent a9b5466 commit 4cf6cee

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

polygon.circle.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
# polygon.circle.py
3+
4+
import math
5+
import matplotlib.pyplot as plt
6+
7+
def calculate_radii(s, n):
8+
# Calculate the radius R of the circumcircle
9+
R = s / (2 * math.sin(math.pi / n))
10+
# Calculate the radius r of the inscribed circle
11+
r = s / (2 * math.tan(math.pi / n))
12+
return R, r
13+
14+
def plot_polygon_and_circles(s, n):
15+
R, r = calculate_radii(s, n)
16+
17+
# Calculate vertex coordinates of regular polygons
18+
vertices = [
19+
(R * math.cos(2 * math.pi * i / n), R * math.sin(2 * math.pi * i / n))
20+
for i in range(n)
21+
]
22+
vertices.append(vertices[0]) # Add the first point for closed shapes
23+
24+
# Plot Settings
25+
fig, ax = plt.subplots()
26+
ax.set_aspect('equal')
27+
ax.grid(True, which='both')
28+
29+
# Drawing an extracorporeal circle
30+
outer_circle = plt.Circle((0, 0), R, color='blue', fill=False, linestyle='--', label='Circle circumscribed by a polygon')
31+
ax.add_artist(outer_circle)
32+
33+
# Draw an inversion circle
34+
inner_circle = plt.Circle((0, 0), r, color='red', fill=False, linestyle='--', label='Circle inscribed in a polygon')
35+
ax.add_artist(inner_circle)
36+
37+
# Draw regular polygons
38+
polygon = plt.Polygon(vertices, edgecolor='green', fill=None, label=f'Definite {n} polygon')
39+
ax.add_artist(polygon)
40+
41+
# axis setting
42+
max_radius = max(R, r)
43+
ax.set_xlim(-max_radius * 1.1, max_radius * 1.1)
44+
ax.set_ylim(-max_radius * 1.1, max_radius * 1.1)
45+
46+
# Add legend
47+
ax.legend()
48+
49+
# Plot display
50+
plt.title(f'Definite {n} squares with side length {s} and other inscribed and inscribed circles')
51+
plt.xlabel('X coordinate')
52+
plt.ylabel('Y coordinate')
53+
plt.show()
54+
55+
# User Input
56+
s = float(input("Enter the length s of one side of a regular polygon: "))
57+
n = int(input("Enter the number n of sides of a regular polygon (in integers greater than three): "))
58+
59+
if n < 3:
60+
print("n must be an integer greater than or equal to 3.")
61+
else:
62+
plot_polygon_and_circles(s, n)
63+
64+

0 commit comments

Comments
 (0)