Skip to content

Instantly share code, notes, and snippets.

Created August 24, 2014 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9d63da5f36c94605e285 to your computer and use it in GitHub Desktop.
Save anonymous/9d63da5f36c94605e285 to your computer and use it in GitHub Desktop.
Python class for basic vector operations
#!python
# basic vector operations
from __future__ import division
import math
class Vector(object):
''' Vector class '''
def __init__(self, x, y):
''' Create a Vector object from rectangular coordinates (x, y) '''
self.x = x
self.y = y
@staticmethod
def from_normalized(x, y):
''' Create a normalized Vector'''
return Vector(x, y).normalize()
@staticmethod
def from_polar(radius, angle):
''' Create a Vector object from polar coordinates (radius, angle) '''
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
return Vector(x, y)
def __str__(self):
''' Get string representation of the object '''
return '({:.02f}, {:.02f})'.format(self.x, self.y)
def __repr__(self):
''' Get string evalable representation of the object '''
return '{}({}, {})'.format(self.__class__.__name__, self.x, self.y)
def __add__(self, other):
''' Add two Vector objects '''
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
''' Subtract two Vector '''
return Vector(self.x - other.x, self.y - other.y)
def __mul__(self, other):
''' Multiply the Vector object with another object or scalar '''
if isinstance(other, Vector):
return Vector(self.x + other.x, self.y + other.y)
return Vector(self.x * other, self.y * other)
def __truediv__(self, other):
''' Divide the Vector object with by object or scalar '''
if isinstance(other, Vector):
return Vector(self.x / other.x, self.y / other.y)
return Vector(self.x / other, self.y / other)
def __iadd__(self, other):
''' Inplace add the vector with another vector '''
self.x += other.x
self.y += other.y
def __isub__(self, other):
''' Inplace subtract the vector with another vector '''
self.x -= other.x
self.y -= other.y
def __imul__(self, other):
''' Inplace multiply the vector with another vector or scalar '''
if isinstance(other, Vector):
self.x *= other.x
self.y *= other.y
self.x *= other
self.y *= other
def __itruediv__(self, other):
''' Inplace divide the vector with another vector or scalar '''
if isinstance(other, Vector):
self.x /= other.x
self.y /= other.y
self.x /= other
self.y /= other
def length(self):
''' Get the length of the vector '''
return math.sqrt(self.x * self.x + self.y * self.y)
def normalize(self):
''' Get a normalized vector '''
return Vector(self.x, self.y) / self.length()
def dot(self, other):
''' Dot product by another object '''
return self.x * other.x + self.y * other.y
if __name__ == '__main__':
''' Example of usage '''
v1 = Vector.from_polar(10, 90)
v2 = Vector(4, 5)
print('v1 = {}'.format(v1))
print('v2 = {}'.format(v2))
print('2 * v1 = {}'.format(v1 * 2))
print('2 * v2 = {}'.format(v2 * 2))
print('|v1| = {}'.format(v1.length()))
print('|v2| = {}'.format(v2.length()))
print('norm(v1) = {}'.format(v1.normalize()))
print('norm(v2) = {}'.format(v2.normalize()))
print('v1 . v2 = {}'.format(v1.dot(v2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment