Skip to content

Instantly share code, notes, and snippets.

@TheVoxcraft
Created August 8, 2017 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheVoxcraft/2d3477f4110e4d50dc1f5bfca95b7dd1 to your computer and use it in GitHub Desktop.
Save TheVoxcraft/2d3477f4110e4d50dc1f5bfca95b7dd1 to your computer and use it in GitHub Desktop.
Vector3 Class in python3
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