I have made a fractal, and I noticed that some parts are disfigured. The reference shape in my code is symmetrical from left to right, but after like 2 levels of recursions the smaller shape is no longer symmetrical (relative to the rotation and size). I don’t know what could be causing this.
import pygame
from math import *
screen = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()
def checkExit():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
def pointat2D(i,p):
dx = p[0]-i[0]
dy = p[1]-i[1]
if dy == 0:
if dx < 0:
pd = -90
else:
pd = 90
else:
if dy < 0:
pd = degrees(radians(180)+atan(dx/dy))
else:
pd = degrees(atan(dx/dy))
return(pd)
def rotate(origin, point, angle):
angle = radians(angle)
ox,oy = origin
px,py = point
qx = ox+cos(angle)*(px-ox)-sin(angle)*(py-oy)
qy = oy+sin(angle)*(px-ox)+cos(angle)*(py-oy)
return [qx,qy]
size = 600
reference = [[0,0],[250,0],[300,-220],[350,0],[600,0]]
lines = [[100,300],[500,300]]
def recurse(amount=1):
global lines
for _ in range(amount):
newLines = []
for i in range(len(lines)-1):
a = pointat2D(lines[i],lines[i+1])-90
d = dist(lines[i],lines[i+1])
t = []
for p in reference:
new = rotate([0,0],p,-a)
new[0] *= d/size
new[1] *= d/size
t.append([new[0]+lines[i][0],new[1]+lines[i][1]])
newLines.extend(t)
lines = []
#remove duplicates
for i in newLines:
if i not in lines:
lines.append(i)
recurse(2)
zoom = 1
m = 0
focus = [300,153.33]
while True:
checkExit()
screen.fill((20,20,20))
lines = [[100,300],[500,300]]
mouse = list(pygame.mouse.get_pos())
mouse[1] -= 300
reference = [[0,0],[250,0],mouse,[350,0],[600,0]]
recurse(3)
# if zoom >= 1.2:
# zoom = 1.1
# zoom += 0.001
m = zoom**10
display = list(map(lambda x: [(x[0]-focus[0])*m+focus[0],(x[1]-focus[1])*m+focus[1]],lines))
pygame.draw.lines(screen,(255,255,255),False,display)
pygame.display.update()
clock.tick(60)
I apologize for posting mostly code, but there really isn’t any other way to show this problem.
I checked my code and it is perfectly symmetrical.
reference = [[0,0],[250,0],[300,-220],[350,0],[600,0]]
Maybe there is something wrong with my code? Or maybe this supposed to happen, idk.