Skip to content

Instantly share code, notes, and snippets.

@blzzua
Created May 18, 2020 10:21
Show Gist options
  • Save blzzua/22fb093774861f992f8b733e3e2086ce to your computer and use it in GitHub Desktop.
Save blzzua/22fb093774861f992f8b733e3e2086ce to your computer and use it in GitHub Desktop.
import turtle
import math
from math import cos, sin, pi
# turtle.delay(50)
def cart2pol(x, y):
r = sqrt(x**2 + y**2)
phi = atan(y, x)
return (r, phi)
def pol2cart(r, phi, radian_scale_factor=1):
x = r * cos(phi*radian_scale_factor)
y = r * sin(phi*radian_scale_factor)
return (x, y)
def star(centerx, centery, internal_radius, external_radius, ray_count = 24, radian_scale_factor=1):
vertex_count = ray_count * 2
VERTEX_POLAR = [[external_radius if (i % 2 == 0) else internal_radius, \
(i * 2 * pi/vertex_count)] for i in range(-vertex_count//2,vertex_count//2)]
VERTEX_CART = [pol2cart(*v, radian_scale_factor=radian_scale_factor) for v in VERTEX_POLAR]
# move center of star to ( centerx, centery )
center = (centerx, centery)
VERTEX_CART = [[sum(i) for i in zip(v, center)] for v in VERTEX_CART]
return VERTEX_CART
#for step in range(vlen):
def move_other(): # the function to move the second turtle
global step, vlen, tlist
if step < vlen:
for ct in tlist:
VERTEX_CART=ct['v']
ct['t'].goto(*VERTEX_CART[step])
if not ct['t'].isdown(): ct['t'].pd()
screen.ontimer(move_other,150) # which sets itself up to be called again
step += 1
screen.update()
else:
for ct in (tlist[-1],):
VERTEX_CART=ct['v']
ct['t'].goto(*VERTEX_CART[0])
if not ct['t'].isdown(): ct['t'].pu()
step = 0
for ct in tlist[0:-1]:
ct['t'].hideturtle()
screen.update()
print("circle done")
min_rad=20
max_rad=500
tlist = []
steps=32
VERTEX_CART_STEPS = 16
turtle.colormode(255)
screen = turtle.Screen()
screen.tracer(0,0)
for i in range(steps):
ct=dict()
ct['t']=turtle.Turtle()
cur_min_rad = min_rad+(steps-i)*(max_rad-min_rad)/steps
cur_max_rad = min_rad+(steps-i+1)*(max_rad-min_rad)/steps
VERTEX_CART=star(0, 0, cur_min_rad, cur_max_rad , ray_count = VERTEX_CART_STEPS, radian_scale_factor= (i+1) * 1/steps)
ct['v']=VERTEX_CART
ct['t'].pencolor(int(255 * (steps-i)/ steps), 100, int(255 * i/ steps) )
ct['t'].speed(1)
ct['t'].shape('circle')
ct['t'].shapesize(0.25)
ct['t'].pu()
#ct['t'].hideturtle()
tlist.append(ct)
vlen = len(VERTEX_CART)
step = 0
move_other()
ct['t'].goto(*VERTEX_CART[0])
screen.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment