Created
August 8, 2017 14:41
-
-
Save jonaslsaa/2d3477f4110e4d50dc1f5bfca95b7dd1 to your computer and use it in GitHub Desktop.
Vector3 Class in python3
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
import math | |
class Vector3: | |
x = 0 | |
y = 0 | |
z = 0 | |
def __init__(self, _x, _y, _z): | |
self.x = _x | |
self.y = _y | |
self.z = _z | |
def __add__(self, other): | |
return Vector3(self.x+other.x, self.y+other.y, self.z+other.z) | |
def __sub__(self, other): | |
return Vector3(self.x-other.x, self.y-other.y, self.z-other.z) | |
def __mul__(self, other): | |
return Vector3(self.x*other.x, self.y*other.y, self.z*other.z) | |
def __truediv__(self, other): | |
return Vector3(self.x/other.x, self.y/other.y, self.z/other.z) | |
def __repr__(self): | |
return "("+str(self.x)+", "+str(self.y)+", "+str(self.z)+")" | |
def dist(self, other): | |
return math.sqrt(math.pow((self.x-other.x), 2)+math.pow((self.y-other.y), 2)+math.pow((self.z-other.z), 2)) | |
def magnitude(self): | |
return math.sqrt(math.pow(self.x, 2)+math.pow(self.y, 2)+math.pow(self.z, 2)) | |
def normalize(self): | |
self.x = self.x / self.magnitude() | |
self.y = self.y / self.magnitude() | |
self.z = self.z / self.magnitude() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment