Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 1.08 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from __future__ import division
  2.  
  3. import math
  4.  
  5. class Vector(object):
  6.         def __init__(self, (x, y)=(0, 0)):
  7.                 self.coord = (x, y)
  8.                 self.length = math.sqrt(x**2 + y**2)
  9.                 if x == 0:
  10.                         if y < 0:
  11.                                 self.angle = 270
  12.                         else:
  13.                                 self.angle = 90
  14.                 else:
  15.                         self.angle = math.atan(y/x)
  16.  
  17.         def normalize(self, newlength=1):
  18.                 if self.length == 0: return
  19.  
  20.                 self.coord = (self.coord[0]*newlength/self.length, self.coord[1]*newlength/self.length)
  21.                 self.length = newlength
  22.  
  23.         def setangle(self, newangle=0):
  24.                 self.angle = newangle
  25.                 x = self.length*math.cos(self.angle)
  26.                 y = self.length*math.sin(self.angle)
  27.                 self.coord = (x, y)
  28.  
  29.         def setcoord(self, (x, y)=(0, 0)):
  30.                 self.coord = (x, y)
  31.                 self.angle = math.atan(y/x)
  32.                 self.length = math.sqrt(x**2 + y**2)
  33.  
  34.         def dotproduct(self, othervector):
  35.                 return self.coord[0]*othervector.coord[0] + self.coord[1]*othervector.coord[1]
  36.  
  37.         def add(self, othervector):
  38.                 return Vector((self.coord[0]+othervector.coord[0], self.coord[1]+othervector.coord[1]))
  39.  
  40.         def subtract(self, othervector):
  41.                 return Vector((self.coord[0]-othervector.coord[0], self.coord[1]-othervector.coord[1]))