Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' turtle_triangle_up.py
- use Python module turtle to draw a triangle pointing up
- draw three overlapping triangles and fill with different colors
- '''
- import turtle as tu
- tu.title("triangle up")
- def triangle_up(x, y, side, color='black'):
- """
- draw an equilateral triangle of size side starting at
- coordinates x, y (lower right corner of triangle)
- color is pen/fill color
- """
- tu.up() # pen up
- tu.goto(x, y)
- tu.down() # pen down
- tu.color(color)
- tu.begin_fill()
- for k in range(3):
- tu.left(360/3)
- tu.forward(side)
- tu.end_fill()
- # optional ...
- # speeds from 1 to 10 enforce increasingly faster animation
- tu.speed(speed=8)
- # center is dx=0, dy=0
- # so these are the starting coordinates
- dx = -150
- dy = 150
- # draw the 3 overlapping triangles
- side = 250
- x1 = dx
- y1 = dy
- # draw the upper triangle
- triangle_up(x1, y1, side, 'red')
- # use Pythagorean theorem to calculate offset
- h = (side**2 - (0.5*side)**2)**0.5
- w = ((0.5*side)**2 - (0.5*h)**2)**0.5
- x2 = w + dx
- y2 = -h/2 + dy
- # draw the lower triangle
- triangle_up(x2, y2, side, '#00d900') # mod-green
- # now draw the smaller yellow triangle
- # in the overlapping region
- triangle_up(x1, y1, side/2, 'yellow')
- # keep showing until window corner x is clicked
- tu.done()
Advertisement
Add Comment
Please, Sign In to add comment