Use Python and Turtle and Random library to draw the 50 random isosceles triangles as shown in the following figure.
Hints:
- Make a function that draws isosceles triangle given the center of the base, length of the base, height, and the direction from the base to the top of the triangle.
- Use random library’s uniform function to generate positions, size, direction, and color.
Source Code:
from turtle import *
import math
import random
title('Isosceles Triangle Madness - PythonTurtle.Academy')
setup(1000,1000)
setworldcoordinates(-500,-500,500,500)
hideturtle()
tracer(0,0)
# x,y is the center of the base, width: length of base, height: height of triangle from the top to base
# direction:direction from the center of base to top
def IsoscelesTriangle(x,y,width,height,direction,c):
up()
goto(x,y)
seth(direction-90)
fd(width/2)
p1x, p1y = xcor(), ycor() # first point: bottom right
back(width)
p2x, p2y = xcor(), ycor() # second point: bottom left
goto(x,y)
seth(direction)
fd(height)
p3x, p3y = xcor(), ycor() # third point: top
goto(p1x,p1y)
down()
fillcolor(c)
begin_fill()
goto(p2x,p2y)
goto(p3x,p3y)
goto(p1x,p1y)
end_fill()
for _ in range(50):
IsoscelesTriangle(random.uniform(-400,400),random.uniform(-400,400),
random.uniform(30,300), random.uniform(30,300),
random.uniform(0,360),
(random.uniform(0,1),random.uniform(0,1),random.uniform(0,1)))
update()