vegaseat

draw turtle overlapping triangles + color fill

Mar 13th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. ''' turtle_triangle_up.py
  2. use Python module turtle to draw a triangle pointing up
  3. draw three overlapping triangles and fill with different colors
  4. '''
  5.  
  6. import turtle as tu
  7.  
  8. tu.title("triangle up")
  9.  
  10. def triangle_up(x, y, side, color='black'):
  11.     """
  12.    draw an equilateral triangle of size side starting at
  13.    coordinates x, y (lower right corner of triangle)
  14.    color is pen/fill color
  15.    """
  16.     tu.up()  # pen up
  17.     tu.goto(x, y)
  18.     tu.down()  # pen down
  19.     tu.color(color)
  20.     tu.begin_fill()
  21.     for k in range(3):
  22.         tu.left(360/3)
  23.         tu.forward(side)
  24.     tu.end_fill()
  25.  
  26.  
  27. # optional ...
  28. # speeds from 1 to 10 enforce increasingly faster animation
  29. tu.speed(speed=8)
  30.  
  31. # center is dx=0, dy=0
  32. # so these are the starting coordinates
  33. dx = -150
  34. dy = 150
  35.  
  36. # draw the 3 overlapping triangles
  37. side = 250
  38. x1 = dx
  39. y1 = dy
  40. # draw the upper triangle
  41. triangle_up(x1, y1, side, 'red')
  42.  
  43. # use Pythagorean theorem to calculate offset
  44. h = (side**2 - (0.5*side)**2)**0.5
  45. w = ((0.5*side)**2 - (0.5*h)**2)**0.5
  46. x2 = w + dx
  47. y2 = -h/2 + dy
  48. # draw the lower triangle
  49. triangle_up(x2, y2, side, '#00d900')  # mod-green
  50.  
  51. # now draw the smaller yellow triangle
  52. # in the overlapping region
  53. triangle_up(x1, y1, side/2, 'yellow')
  54.  
  55.  
  56. # keep showing until window corner x is clicked
  57. tu.done()
Advertisement
Add Comment
Please, Sign In to add comment