abbarnes

Clock Hands [codeskulptor]

Dec 12th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import simplegui
  2. import random
  3. import math
  4. #http://www.codeskulptor.org/#user43_xK6bwFYD2Mgvyy4_0.py
  5.  
  6. # math helper function
  7. def dot(v, w):
  8.     return v[0] * w[0] + v[1] * w[1]
  9.  
  10.  
  11. WIDTH = 500
  12. HEIGHT = 500
  13.  
  14. #The two vectors that are being added
  15. vector1 = [0,100]
  16. vector2 = [100,0]
  17.  
  18.  
  19. #vector is drawn from the center of the canvas.
  20. class vector:
  21.     def __init__ (self, xy, color, rot):
  22.         self.xy = xy
  23.         self.color = color
  24.         self.border = 2
  25.         #initial x/y coords converted to polar coords (length, theta)
  26.         self.length = math.sqrt(self.xy[0]*self.xy[0] + self.xy[1]*self.xy[1])
  27.         self.theta = math.atan2(self.xy[1], self.xy[0])
  28.         self.rot = rot
  29.      
  30.     def __str__ (self):
  31.         print "Vector with coordinates "
  32.         print self.xy
  33.         print "Color is " + self.color
  34.         return ""
  35.    
  36.     def update(self):
  37.         #polar coords updtated by adding 3.14/rot to theta, then x/y coords updated
  38.         self.theta += math.pi/self.rot
  39.         self.xy[0] = self.length * math.cos(self.theta)
  40.         self.xy[1] = self.length * math.sin(self.theta)
  41.  
  42.        
  43.    
  44.     def draw(self, canvas):
  45.         canvas.draw_line((WIDTH/2,HEIGHT/2),
  46.                          (WIDTH/2 + self.xy[0], HEIGHT/2 + self.xy[1]),
  47.                          self.border, self.color)
  48.  
  49. class resultant:
  50.     def __init__(self, vectorA, vectorB, color):
  51.         self.coords = [vectorA.xy[0] + vectorB.xy[0], vectorA.xy[1] + vectorB.xy[1]]
  52.         self.color = color
  53.         self.border = 2
  54.        
  55.     def __str__ (self):
  56.         print "Vector with coordinates "
  57.         print self.coords
  58.         print "Color is " + self.color
  59.         return ""
  60.          
  61.     def update(self, vectorA, vectorB):
  62.         #update function has to be passed both of the vectors to get most recent values for x/y
  63.         self.coords = [vectorA.xy[0] + vectorB.xy[0], vectorA.xy[1] + vectorB.xy[1]]
  64.        
  65.     def draw(self,canvas):
  66.         canvas.draw_line((WIDTH/2,HEIGHT/2),
  67.                          (WIDTH/2 + self.coords[0], HEIGHT/2 + self.coords[1]),
  68.                          self.border, self.color)
  69.  
  70. def draw(canvas):
  71.     v1.update()
  72.     v2.update()
  73.     v3.update(v1, v2)                  
  74.     v1.draw(canvas)
  75.     v2.draw(canvas)
  76.     v3.draw(canvas)
  77.    
  78.  
  79.    
  80. v1 = vector(vector1, "Red", 60)
  81. v2 = vector(vector2, "White", -600)
  82. v3 = resultant(v1, v2, "Green")
  83.  
  84.  
  85. frame = simplegui.create_frame("Dot Product", WIDTH, HEIGHT)
  86.  
  87. frame.set_draw_handler(draw)
  88.  
  89. frame.start()
Advertisement
Add Comment
Please, Sign In to add comment