Quadratic Koch Snowflake from a Triangle (Source Code)

The quadratic Koch snowflake starts from a square. Draw the quadratic Koch snowflake starting with a triangle.

Quadratic Koch Snowflake from a Triangle

Source Code:

import turtle

screen = turtle.Screen()
screen.title('Quadratic Koch Curve From Triangle - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()
turtle.bgcolor('midnight blue')
turtle.color('white')

def koch(x1,y1,x2,y2):
    distance=((x2-x1)**2 + (y2-y1)**2)**0.5
    if distance<10:
        turtle.up()
        turtle.goto(x1,y1)
        turtle.down()
        turtle.goto(x2,y2)
        return
    turtle.up()
    turtle.goto(x1,y1)
    direction=turtle.towards(x2,y2)
    turtle.seth(direction)
    turtle.fd(distance/3)
    x3,y3 = turtle.xcor(), turtle.ycor()
    turtle.left(90)
    turtle.fd(distance/3)
    x4,y4 = turtle.xcor(), turtle.ycor()
    turtle.right(90)
    turtle.fd(distance/3)
    x5,y5 = turtle.xcor(), turtle.ycor()
    turtle.right(90)
    turtle.fd(distance/3)
    x6,y6 = turtle.xcor(), turtle.ycor()
    koch(x1,y1,x3,y3)
    koch(x3,y3,x4,y4)
    koch(x4,y4,x5,y5)
    koch(x5,y5,x6,y6)
    koch(x6,y6,x2,y2)
    

koch(500,-200,-500,-200)
koch(-500,-200,0,500*3**0.5-200)
koch(0,500*3**0.5-200,500,-200)
screen.update()

Quadratic Koch Snowflake with Python Turtle (Source Code)

The original Koch Snowflake is based on triangles. Draw quadratic Koch snowflake that breaks a line into 4 smaller pieces of 1/3 of the original length as shown.

Depth 1 Recursion
Depth 2 Recursion
Depth 3 Recursion

The following is drawn with 4 initial lines that forms a square.

Quadratic Koch Snowflake

Source Code:

import turtle

screen = turtle.Screen()
screen.title('Quadratic Koch Curve - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()
turtle.bgcolor('midnight blue')
turtle.color('white')

def koch(x1,y1,x2,y2):
    distance=((x2-x1)**2 + (y2-y1)**2)**0.5
    if distance<10:
        turtle.up()
        turtle.goto(x1,y1)
        turtle.down()
        turtle.goto(x2,y2)
        return
    turtle.up()
    turtle.goto(x1,y1)
    direction=turtle.towards(x2,y2)
    turtle.seth(direction)
    turtle.fd(distance/3)
    x3,y3 = turtle.xcor(), turtle.ycor()
    turtle.left(90)
    turtle.fd(distance/3)
    x4,y4 = turtle.xcor(), turtle.ycor()
    turtle.right(90)
    turtle.fd(distance/3)
    x5,y5 = turtle.xcor(), turtle.ycor()
    turtle.right(90)
    turtle.fd(distance/3)
    x6,y6 = turtle.xcor(), turtle.ycor()
    koch(x1,y1,x3,y3)
    koch(x3,y3,x4,y4)
    koch(x4,y4,x5,y5)
    koch(x5,y5,x6,y6)
    koch(x6,y6,x2,y2)
    

koch(-400,-400,-400,400)
koch(-400,400,400,400)
koch(400,400,400,-400)
koch(400,-400,-400,-400)
screen.update()

Pythagoras Tree Fractal with Filled Color (Source Code)

Based on Pythagoras Tree, draw a version with filled color. Color branches with brown and leaves with green.

Pythagoras Tree Fractal with Filled Color

Source Code:

import turtle
screen = turtle.Screen()
screen.title('Colored Fractal Pythagoras Tree - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(2)

def squaretree(x,y,length,tilt,n):
    if n==0: return
    if n<6:
        turtle.color('forest green')
    else:
        turtle.color('saddle brown')
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.begin_fill()
    turtle.seth(tilt)
    turtle.fd(length)
    turtle.left(90)
    turtle.fd(length)
    turtle.left(45)
    turtle.fd(length/2**0.5)
    x1,y1 = turtle.xcor(), turtle.ycor()    
    turtle.left(90)
    turtle.fd(length/2**0.5)
    x2,y2 = turtle.xcor(), turtle.ycor()
    turtle.left(45)
    turtle.fd(length)
    turtle.left(90)
    turtle.end_fill()

    squaretree(x1,y1,length/2**0.5,tilt-45,n-1)
    squaretree(x2,y2,length/2**0.5,tilt+45,n-1)
    
squaretree(-150,-600,300,0,13)
screen.update()
    
    

Five Branch Fractal Tree (Source Code)

Draw a fractal tree with five branches.

Five Branch Fractal Tree

Source Code:

import turtle
import colorsys

screen = turtle.Screen()
screen.title('Five Branch Fractal Tree - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()

def fivebranchtree(x,y,length,direction,n):
    if n==0: return
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.seth(direction)
    turtle.pensize(length/100)
    turtle.fd(length)
    px,py = turtle.xcor(), turtle.ycor()
    fivebranchtree(px,py,length*0.445,direction,n-1)
    fivebranchtree(px,py,length*0.445,direction+360/7,n-1)
    fivebranchtree(px,py,length*0.445,direction+2*360/7,n-1)
    fivebranchtree(px,py,length*0.445,direction-2*360/7,n-1)
    fivebranchtree(px,py,length*0.445,direction-360/7,n-1)

L=1000    
fivebranchtree(0,-900,L,90,8)
screen.update()

Colored Slanted Fractal Tree (Source Code)

Based on Slanted Fractal Tree, draw a colored version with leaves green and branches brown.

Colored Slanted Fractal Tree

Source Code:

import turtle

screen = turtle.Screen()
screen.title('Slanted Fractal Tree - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()

def slanted_tree(x,y,length,direction):
    if length < 2: return
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.seth(direction)
    turtle.pensize(length/50)
    if length > 20: turtle.color('saddle brown')
    else: turtle.color('green')
    turtle.fd(length)
    px,py = turtle.xcor(), turtle.ycor()
    slanted_tree(px,py,length*0.7,direction-30)
    slanted_tree(px,py,length*0.7,direction+60)

L=400
slanted_tree(100,-500,L,90)
screen.update()

Slanted Fractal Tree 2 (Source Code)

Slightly modify the slanted fractal tree to make the color of the branch lighter as the branch gets thinner. Also slightly modify the slant angles.

Slanted Fractal Tree 2

Source Code:

import turtle

screen = turtle.Screen()
screen.title('Slanted Fractal Tree - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()

def slanted_tree(x,y,length,direction):
    if length < 2: return
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.seth(direction)
    turtle.pensize(length/50)
    turtle.color((0.5-length/L*0.5,0.5-length/L*0.5,0.5-length/L*0.5))
    turtle.fd(length)
    px,py = turtle.xcor(), turtle.ycor()
    slanted_tree(px,py,length*0.7,direction+60)
    slanted_tree(px,py,length*0.7,direction-30)

L=400
slanted_tree(100,-500,L,90)
screen.update()

Slanted Fractal Tree (Source Code)

Use recursion to draw the following slated fractal tree. Notice that the pensize also changes.

Slanted Fractal Tree

Source Code:

import turtle

screen = turtle.Screen()
screen.title('Slanted Fractal Tree - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()

def slanted_tree(x,y,length,direction):
    if length < 7: return
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.seth(direction)
    turtle.pensize(length/50)
    turtle.fd(length)
    px,py = turtle.xcor(), turtle.ycor()
    slanted_tree(px,py,length*0.75,direction+45)
    slanted_tree(px,py,length*0.75,direction-15)
    
slanted_tree(100,-500,300,90)

Colored Square Tree Fractal (Source Code)

Color the square tree fractal using the colorsys library.

Colored Square Tree Fractal

Source Code:

import turtle
import colorsys

screen = turtle.Screen()
screen.title('Squares Tree Colored - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
turtle.speed(0)
turtle.hideturtle()

def squaretree(x,y,length,tilt,n):
    if n==0: return       
    turtle.up()
    turtle.goto(x,y)
    turtle.down()
    turtle.seth(tilt)
    turtle.begin_fill()
    c=colorsys.hsv_to_rgb(0.4*(N-n)/N,1,0.7)
    turtle.color(c)
    turtle.fd(length)
    turtle.left(90)
    turtle.fd(length)
    x1,y1 = turtle.xcor(), turtle.ycor()
    turtle.left(90)
    turtle.fd(length)
    x2,y2 = turtle.xcor(), turtle.ycor()
    turtle.left(90)
    turtle.fd(length)
    turtle.left(90)
    turtle.end_fill()
    
    squaretree(x2,y2,length/2**0.5,tilt+45,n-1)
    turtle.up()
    turtle.goto(x1,y1)
    turtle.seth(tilt+135)
    turtle.fd(length/2**0.5)
    squaretree(turtle.xcor(),turtle.ycor(),length/2**0.5,tilt-45,n-1)

N=14    
squaretree(-150,-600,300,0,N)
screen.update()
    
    

Continuous Clock with Python Turtle (Source Code)

In a previous project you animated a clock. Improve the clock by making all hands move continuously.

Source Code:

import turtle
import datetime
import math

screen = turtle.Screen()
screen.title('Continuous Clock - PythonTurtle.Academy')
screen.bgcolor('sky blue')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)


class clock:
    def __init__(self,hour,minute,second):
        self.hour, self.minute, self.second = hour, minute, second
        self.microsecond = 0
        self.face = turtle.Turtle()
        self.hand = turtle.Turtle()
        self.face.hideturtle()
        self.hand.hideturtle()

    def draw(self):
        self.draw_face()
        self.draw_hand()
        
    def draw_face(self):
        self.face.clear()
        self.face.up()
        self.face.goto(0,-700)
        self.face.pensize(4)
        self.face.down()
        self.face.fillcolor('white')
        self.face.begin_fill()
        self.face.circle(700,steps=100)
        self.face.end_fill()
        self.face.up()
        self.face.goto(0,0)
        self.face.dot(10)
        self.face.pensize(2)
        for angle in range(0,360,6):
            self.face.up()
            self.face.goto(0,0)
            self.face.seth(90-angle)
            self.face.fd(620)
            self.face.down()
            self.face.fd(30)
            
        self.face.pensize(3)
        for angle in range(0,360,30):
            self.face.up()
            self.face.goto(0,0)
            self.face.seth(90-angle)
            self.face.fd(600)
            self.face.down()
            self.face.fd(50)
            
        self.face.pensize(4)
        for angle in range(0,360,90):
            self.face.up()
            self.face.goto(0,0)
            self.face.seth(90-angle)
            self.face.fd(580)
            self.face.down()
            self.face.fd(70)
        
    def draw_hand(self):    
        self.hand.clear()       
        self.hand.up()
        self.hand.goto(0,0)
        self.hand.seth(90-math.floor(((self.hour%12)*60*60*1000000+self.minute*60*1000000+self.second*1000000+self.microsecond)/3600000000*30))
        self.hand.down()
        self.hand.color('black')
        self.hand.pensize(6)
        self.hand.fd(300)

        self.hand.up()
        self.hand.goto(0,0)
        self.hand.seth(90-math.floor((self.minute*60*1000000+self.second*1000000+self.microsecond)/60000000*6))
        self.hand.down()
        self.hand.color('black')
        self.hand.pensize(4)
        self.hand.fd(400)

        self.hand.up()
        self.hand.color('red')
        self.hand.goto(0,0)
        self.hand.dot(5)
        self.hand.seth(90-(self.second*1000000+self.microsecond)/1000000*6)
        self.hand.down()
        self.hand.pensize(2)
        self.hand.fd(570)

def animate():
    global c
    d = datetime.datetime.now()
    c.hour, c.minute, c.second, c.microsecond = d.hour, d.minute, d.second, d.microsecond
    c.draw_hand()
    screen.update()
    screen.ontimer(animate,100)
    
d = datetime.datetime.now()
c = clock(d.hour,d.minute,d.second)
c.draw_face()
screen.update()
animate()

Clock with Python Turtle (Source Code)

Define a ‘clock’ class with Python and use the datetime library to draw an animated clock shown.

Source Code:

import turtle
import datetime
screen = turtle.Screen()
screen.title('Clock - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
screen.tracer(0,0)
screen.bgcolor('sky blue')

class clock:
    def __init__(self,hour,minute,second):
        self.hour, self.minute, self.second = hour, minute, second
        self.face = turtle.Turtle()
        self.hand = turtle.Turtle()
        self.face.hideturtle()
        self.hand.hideturtle()

    def draw(self):
        self.draw_face()
        self.draw_hand()
        
    def draw_face(self):
        self.face.clear()
        self.face.up()
        self.face.goto(0,-700)
        self.face.pensize(5)
        self.face.down()
        self.face.fillcolor('white')
        self.face.begin_fill()
        self.face.circle(700)
        self.face.end_fill()
        self.face.up()
        self.face.goto(0,0)
        self.face.dot(10)
        self.face.pensize(2)
        for angle in range(0,360,6):
            self.face.up()
            self.face.goto(0,0)
            self.face.seth(90-angle)
            self.face.fd(620)
            self.face.down()
            self.face.fd(30)
        self.face.pensize(4)
        for angle in range(0,360,30):
            self.face.up()
            self.face.goto(0,0)
            self.face.seth(90-angle)
            self.face.fd(600)
            self.face.down()
            self.face.fd(50)
        
    def draw_hand(self):    
        self.hand.clear()       
        self.hand.up()
        self.hand.goto(0,0)
        self.hand.seth(90-self.hour%12*360//12)
        self.hand.down()
        self.hand.color('black')
        self.hand.pensize(6)
        self.hand.fd(300)

        self.hand.up()
        self.hand.goto(0,0)
        self.hand.seth(90-self.minute*6)
        self.hand.down()
        self.hand.color('black')
        self.hand.pensize(4)
        self.hand.fd(400)

        self.hand.up()
        self.hand.color('red')
        self.hand.goto(0,0)
        self.hand.dot(5)
        self.hand.seth(90-self.second*6)
        self.hand.down()
        self.hand.pensize(2)
        self.hand.fd(600)

def animate():
    global c
    d = datetime.datetime.now()
    c.hour, c.minute, c.second = d.hour, d.minute, d.second
    c.draw_hand()
    screen.update()
    screen.ontimer(animate,1000)
    
d = datetime.datetime.now()
c = clock(d.hour,d.minute,d.second)
c.draw_face()
screen.update()
animate()