Friday, December 9, 2022

Twisted Square Animation - Python Turtle Graphics

 Twisted Square Animation - Python Turtle Graphics

Youtube


Source code:

from turtle import *
from math import atan2,degrees
import colorsys as cs

setup(500,500)
bgcolor('black')
R = 150
tur = (Turtle(),Turtle(),Turtle(),Turtle())
tracer(0,0)
M = 0.001
color('white')
cols=['red','green','orange','cyan']
up()
dM = 0.0005
hh = 0
hideturtle()
while True:
    h = hh
    clear()
    for i in range(4):
        tur[i].hideturtle()
        tur[i].up()
        tur[i].color(cols[i])
        tur[i].clear()
    tur[0].goto(-R,R)
    tur[1].goto(R,R)
    tur[2].goto(R,-R)
    tur[3].goto(-R,-R)
    tur[0].seth(0)
    tur[1].seth(-90)
    tur[2].seth(180)
    tur[3].seth(90)
    r=R*2
    for i in range(20):
        color(cs.hsv_to_rgb(h,1,1))
        goto(tur[0].pos())
        down()
        goto(tur[1].pos())
        goto(tur[2].pos())
        goto(tur[3].pos())
        goto(tur[0].pos())
        up()
        a = degrees(atan2(M*r,r-M*r))
        for j in range(4):
            tur[j].fd(M*r)
            tur[j].rt(a)
        r=r-M*r
        h = h+0.025
    update()
    hh = hh+0.0025
    M=M+dM
    if M>=0.2 or M<=-0.1:
        dM=-dM
        dM=dM*1.25

done()

No comments:

Post a Comment

Python Turtle Graphics Drawing - Gigabyte

 Python Turtle Graphics Drawing - Gigabyte Youtube Source code: from turtle import * setup ( 600 , 600 ) color ( '#2D68AE' ) up (...