Created
October 4, 2015 17:39
-
-
Save macndesign/0d778895524537530376 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt | |
class Vector(object): | |
def __init__(self, x=0, y=0): | |
self.x = x | |
self.y = y | |
def __repr__(self): | |
return 'Vector({x}, {y})'.format(x=self.x, y=self.y) | |
def distance(self, other): | |
dx = self.x - other.x | |
dy = self.y - other.y | |
return sqrt(dx**2 + dy**2) | |
def __abs__(self): | |
return self.distance(Vector()) | |
def __add__(self, other): | |
dx = self.x + other.x | |
dy = self.y + other.y | |
return Vector(dx, dy) | |
def __mul__(self, n): | |
return Vector(self.x*n, self.y*n) | |
if __name__ == '__main__': | |
v = Vector(3, 4) | |
# 5.0 | |
print(abs(v)) | |
v1 = Vector(2, 4) | |
v2 = Vector(2, 1) | |
# Vector(4, 5) | |
print(v1 + v2) | |
# Vector(6, 12) | |
print(v1 * 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment