-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_graph_integer_coord_edges.py
55 lines (44 loc) · 1.36 KB
/
start_graph_integer_coord_edges.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
import matplotlib.pyplot as plt
def gcd(a, b):
while b:
a, b = b, a % b
return a
def pythagoreanTriplets(limits):
triplets = []
c, m = 0, 2
while c < limits:
for n in range(1, m):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limits:
break
if gcd(a, b) == 1:
triplets.append((a, b, c))
m = m + 1
return triplets
def plotTriplets(triplets, n):
plt.figure(facecolor='white')
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.grid(color='gray', linestyle='--', linewidth=0.5)
points_plotted = 0
for triplet in triplets:
if points_plotted >= n:
break
a, b, _ = triplet
points = [(a, b), (-a, b), (a, -b), (-a, -b), (b, a), (-b, a), (b, -a), (-b, -a)]
for x, y in points:
if points_plotted >= n:
break
plt.plot([0, x], [0, y], marker='o')
plt.text(x, y, f'({x}, {y})', fontsize=9, ha='right')
points_plotted += 1
plt.xlim(-25, 25)
plt.ylim(-25, 25)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title(f'{n}-star graph with integer edges and coordinates')
plt.show()
triplets = pythagoreanTriplets(105)
plotTriplets(triplets, n=20)