Created
April 13, 2023 16:01
-
-
Save OhadRubin/aacc4e72f6a3496a3d01062a73e15c99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class PythagoreanTriples: | |
def __init__(self): | |
self.m = 2 | |
self.n = 1 | |
def __iter__(self): | |
return self | |
def __next__(self): | |
while True: | |
a = self.m**2 - self.n**2 | |
b = 2 * self.m * self.n | |
c = self.m**2 + self.n**2 | |
triple = (a, b, c) | |
if self.n + 1 < self.m: | |
self.n += 1 | |
else: | |
self.m += 1 | |
self.n = 1 | |
if self.m <= 0 or self.n <= 0: | |
raise StopIteration | |
if a > 0 and b > 0 and c > 0: | |
return triple | |
def ascii_triangle(triple): | |
a, b, c = triple | |
if a > b: | |
a, b = b, a | |
ascii_art = "" | |
for i in range(1, a + 1): | |
ascii_art += " " * (a - i) + "*" * i + "\n" | |
for i in range(1, b + 1): | |
ascii_art += " " * (b - i) + "*" * i + "\n" | |
for i in range(1, c + 1): | |
ascii_art += " " * (c - i) + "*" * i + "\n" | |
return ascii_art | |
def ascii_triangle_scaled(a, b, c, M): | |
# ratio = (M - 1) / max(a, b) | |
ratio =1 | |
a_scaled = int(round(a * ratio)) | |
b_scaled = int(round(b * ratio)) | |
ascii_art = [] | |
for i in range(b_scaled): | |
spaces = int(round(a_scaled * (i / b_scaled))) | |
ascii_art.append(" " * spaces + "*" * (b_scaled - i) + "\n") | |
return ascii_art | |
def draw_triangle(K, I): | |
angles = np.linspace(0, 90, K) | |
alpha = angles[I] | |
beta = 90 - alpha | |
pt = PythagoreanTriples() | |
triples_iterator = iter(pt) | |
while True: | |
triple = next(triples_iterator) | |
a, b, c = triple | |
if a > b: | |
a, b = b, a | |
a_angle = np.degrees(np.arctan2(a, b)) | |
b_angle = np.degrees(np.arctan2(b, a)) | |
if np.isclose(a_angle, alpha, rtol=0.01) or np.isclose(b_angle, alpha, rtol=0.01): | |
# if np.isclose(a_angle, alpha, atol=0.05) or np.isclose(b_angle, alpha, atol=0.05): | |
break | |
return a,b,c | |
# return ascii_triangle_scaled(a, b, c, M) | |
K = 100 | |
I = 20 | |
K=128 | |
[draw_triangle(K=K, I=x) for x in [2,4,8,16,32,64]] | |
# print(ascii_triangle(draw_triangle(K, I))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment