Skip to content

Instantly share code, notes, and snippets.

@ekm507
Created April 9, 2023 18:00
Show Gist options
  • Save ekm507/80ad334806968443e6a933639696a6ee to your computer and use it in GitHub Desktop.
Save ekm507/80ad334806968443e6a933639696a6ee to your computer and use it in GitHub Desktop.
draw the fractal designed by Armin Kheyrollahi
import numpy as np
import cv2
width, height = 4000, 4000
num_itterations = 4000000
n = 6
m = 3
def generate_initial_points(n, radius, center) -> list:
a = np.arange(0, n)
angles = 2 * np.pi / n * a
print(angles)
x = np.cos(angles) * radius + center[0]
y = np.sin(angles) * radius + center[1]
return list(zip(x, y))
def add_point(points:list, n, lasts:list):
t = np.random.randint(0, n)
l2 = lasts[:-2] + [lasts[-1]]
while t in l2:
t = np.random.randint(0, n)
p1 = points[t]
p2 = points[-1]
if p2[0] - p1[0] == 0:
return t, points
m = (p2[1] - p1[1]) / (p2[0] - p1[0])
d = (p2[0] - p1[0]) / 2
newp = (p1[0] + d,
p1[1] + d * m)
points.append(newp)
return lasts[1:] + [t], points
points = generate_initial_points(n, width // 2 - 1, (width // 2, height // 2))
lasts = [0] * m
for i in range(num_itterations):
lasts, points = add_point(points, n, lasts)
image = np.zeros((width, height))
for point in points:
image[tuple(map(int, list(point)))] += 50
cv2.imwrite('img.png', image)
# cv2.imshow('img', image)
# cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment