Advertisement
beezing

Drawing heart using equation - Pythonista

Feb 17th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. # draw a heart using equation
  2. import canvas, sys, random
  3. from console import clear
  4. from datetime import datetime
  5. from math import sin, cos, pi
  6.  
  7. w = h = 600 # canvas size
  8. detail = random.random() * 100 # larger is slower
  9. # almost perfect : 12.485 75.05 125.3
  10. # half left : 12.525
  11. # half right : 12.605
  12. scale = 15 # larger is bigger
  13. origin = w/2 # plot origin on canvas
  14.  
  15. def draw_heart(outline = False):
  16.     first = True
  17.     for t in xrange(int(2*pi * detail)):
  18.         t = t * detail
  19.         # heart equation
  20.         x = 16*(sin(t) ** 3)
  21.         y = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t)
  22.         # scale result
  23.         x = origin + x * scale
  24.         y = origin + y * scale + scale*2
  25.         # hide first line
  26.         if first:
  27.             canvas.move_to(x, y)
  28.             first = False
  29.         else:
  30.             canvas.add_line(x, y)
  31.     # set color
  32.     canvas.set_fill_color(1,0.5,0.5)
  33.     canvas.set_stroke_color(0.5,0,0)
  34.     canvas.set_line_width(detail/2)
  35.     # draw heart
  36.     if outline:
  37.         canvas.draw_path()
  38.     else:
  39.         canvas.close_path()
  40.         canvas.fill_path()
  41.  
  42. clear()
  43. print 'Calculating... d =',detail
  44. start = datetime.now()
  45. canvas.set_size(w,h)
  46. canvas.draw_rect(0,0, w,h)
  47. #canvas.draw_line(0,h/2,w,h/2)
  48. #canvas.draw_line(w/2,0,w/2,h)
  49. draw_heart(True) # outlined
  50. draw_heart() # filled
  51. stop = datetime.now()
  52. print(stop-start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement