Based on the function that draws Isosceles Trapezoid in the this previous project, draw the following fractal tree with Python and Turtle.
Related Projects:
Based on the function that draws Isosceles Trapezoid in the this previous project, draw the following fractal tree with Python and Turtle.
Related Projects:
Fill the five pointed stars with random colors with random colors. Refer to this project that draw uncolored five pointed star fractal.
Draw a five pointed star fractal that have nested five pointed star as shown.
Hint: Create a function that draws five pointed star in any location, orientation, and size.
Source Code:
import turtle
import math
screen = turtle.Screen()
screen.title('Five Pointed Star Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(1)
def five_pointed_star(x,y,direction,r): #x,y is the center
turtle.up()
turtle.goto(x,y)
turtle.seth(direction)
turtle.fd(r)
turtle.right(180-18)
turtle.down()
length = r*math.sin(math.pi*2/5)/(1+math.sin(math.pi/10))
for _ in range(5):
turtle.fd(length)
turtle.left(72)
turtle.fd(length)
turtle.right(180-36)
def five_pointed_star_fractal(x,y,direction,r): #x,y is the center
five_pointed_star(x,y,direction,r)
if r < 20: return
five_pointed_star_fractal(x,y,180+direction,r*math.sin(math.pi/10)/math.cos(math.pi/5))
five_pointed_star_fractal(0,-40,90,1000)
Draw the following fractal made up of nesting pentagrams with either recursion or iteration.
Hint: Making a function that draws pentagram in any location, radius, and direction will help!
Source Code:
import turtle
import math
screen = turtle.Screen()
screen.title('Pentagram Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
def star(x,y,direction,r): #x,y is the center
turtle.up()
turtle.goto(x,y)
turtle.seth(direction)
turtle.fd(r)
turtle.right(180-18)
turtle.down()
length = 2*r*math.sin(math.pi*2/5)
for _ in range(5):
turtle.fd(length)
turtle.right(180-36)
def star_fractal(x,y,direction,r):
star(x,y,direction,r)
if r < 50: return
star_fractal(x,y,180+direction,r*math.sin(math.pi/10)/math.cos(math.pi/5))
star_fractal(0,0,90,1000)
Lori Gardi created this nested overlapping circle fractal in the paper ‘The Universe is a Fractal’. The fractal according to the paper looks similar to a supernova. A simple version to draw is to let the overlapping circles to pass through the centers of the other circle.
To draw this, it will be helpful to create a basic function that draw a circle given the center and radius. Then, create a function that draws the overlapping circles in horizontal and vertical orientations. With these two functions, the recursive function just need to flip the orientation and reduce the radius size in each recursive call. You need do a little geometry to figure out the radius of the smaller circles. The following is the source code that draw this fractal:
import turtle
screen = turtle.Screen()
screen.title('Gardi Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
turtle.color('teal')
def circle(x,y,radius):
turtle.up()
turtle.goto(x,y-radius)
turtle.down()
turtle.seth(0)
turtle.circle(radius, steps=360)
def two_circles(x,y,radius,orientation):
turtle.pensize(radius/50)
if orientation==0:
circle(x-radius/2,y,radius)
circle(x+radius/2,y,radius)
else:
circle(x,y-radius/2,radius)
circle(x,y+radius/2,radius)
def gardi_fractal(x,y,radius,orientation,n):
if n==0: return
two_circles(x,y,radius,orientation)
gardi_fractal(x,y,(4-7**0.5)/3*radius,1-orientation,n-1)
gardi_fractal(0,0,300,0,6)
screen.update()
In a previous project, we drew a star fractal with recursion. A tutorial and source code were provided in that project. Based on the code, draw the following colorful star fractal with colosys library.
Star Fractal with Tutorial and Source Code
Colorful Star Fractal 1
Blue Star
In a previous project, we drew a star fractal with recursion. A tutorial and source code were provided in that project. Based on the code, draw the following colorful star fractal with colosys library.
Star Fractal with Tutorial and Source Code
Colorful Star Fractal 2
Blue Star
Draw the following fractal made up with stars. You may need to use recursion and trigonometry to do this project.
The following figures show star fractal with different recursion depths (from depth 0 to depth 4)
It is very helpful to make a function that draws star at any given center and size. The following code is the function that does the work. It also can draw filled stars given pen color and fill color.
def star(x,y,length,penc,fillc):
turtle.up()
turtle.goto(x,y)
turtle.seth(90)
turtle.fd(length)
turtle.seth(180+36/2)
L = length*math.sin(36*math.pi/180)/math.sin(54*math.pi/180)
turtle.seth(180+72)
turtle.down()
turtle.fillcolor(fillc)
turtle.pencolor(penc)
turtle.begin_fill()
for _ in range(5):
turtle.fd(L)
turtle.right(72)
turtle.fd(L)
turtle.left(144)
turtle.end_fill()
The next step is to define a recursive function that draws the star fractal. One way to stop the recursion function is to set the depth limit. In each recursive call decrement the depth limit by 1. The base condition of the recursive call is when the depth limit is 0. In this case, just draw a star by calling the function we defined above. It requires some trigonometry to figure out the centers and the size of 5 star fractals relative to the current center and size. The following code is the recursive function:
def star_fractal(x,y,length,penc,fillc,n):
if n==0:
star(x,y,length,penc,fillc)
return
length2 = length/(1+(math.sin(18*math.pi/180)+1)/math.sin(54*math.pi/180))
L = length-length2-length2*math.sin(18*math.pi/180)/math.sin(54*math.pi/180)
for i in range(5):
star_fractal(x+math.cos((90+i*72)*math.pi/180)*(length-length2),
y+math.sin((90+i*72)*math.pi/180)*(length-length2),
length2,penc,fillc,n-1)
The following is the complete code:
import turtle
import math
screen = turtle.Screen()
screen.title('Star Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.tracer(0,0)
turtle.hideturtle()
turtle.speed(0)
def star(x,y,length,penc,fillc):
turtle.up()
turtle.goto(x,y)
turtle.seth(90)
turtle.fd(length)
turtle.seth(180+36/2)
L = length*math.sin(36*math.pi/180)/math.sin(54*math.pi/180)
turtle.seth(180+72)
turtle.down()
turtle.fillcolor(fillc)
turtle.pencolor(penc)
turtle.begin_fill()
for _ in range(5):
turtle.fd(L)
turtle.right(72)
turtle.fd(L)
turtle.left(144)
turtle.end_fill()
def star_fractal(x,y,length,penc,fillc,n):
if n==0:
star(x,y,length,penc,fillc)
return
length2 = length/(1+(math.sin(18*math.pi/180)+1)/math.sin(54*math.pi/180))
L = length-length2-length2*math.sin(18*math.pi/180)/math.sin(54*math.pi/180)
for i in range(5):
star_fractal(x+math.cos((90+i*72)*math.pi/180)*(length-length2),
y+math.sin((90+i*72)*math.pi/180)*(length-length2),
length2,penc,fillc,n-1)
star_fractal(0,0,400,'blue','blue',3)
screen.update()
Moving averages can smooth out the stock prices and reduce the randomness. Based on the previous projects (stock price random walk with candlestick chart, and trending stock price random walk with candlestick chart), add 10 day and 25 day moving averages to the charts.
Draw all-sided hindu temple fractal. Refer to this Hindu Temple project.