Files
terrain_generator/vectors.py
T

65 lines
1.3 KiB
Python

class Vector2d:
def __init__(self, x, y):
self.points = [x, y]
@property
def x(self):
return self.points[0]
@x.setter
def x(self, value):
self.points[0] = value
@property
def y(self):
return self.points[1]
@y.setter
def y(self, value):
self.points[1] = value
def __getitem__(self, key):
return self.points[key]
def __setitem__(self, key, value):
self.points[key] = value
class Vector3d(Vector2d):
def __init__(self, x, y, z):
self.points = [x, y, z]
def cross_product(self, vector3d):
# Returns a line that is perpendicular to the parameter
# and self.
# Returns the 'normal'.
x = self.y * vector3d.z - self.z * vector3d.y
y = self.z * vector3d.x - self.x * vector3d.z
z = self.x * vector3d.y - self.y * vector3d.x
return Vector3d(x, y, z)
def dot_product(self, vector3d):
# Returns a scalar that defines how similar two
# vectors are to one another.
return self.x * vector3d.x + self.y * vector3d.y + self.z * vector3d.z
@property
def z(self):
return self.points[2]
@z.setter
def z(self, value):
self.points[2] = value
class Vector4d(Vector3d):
def __init__(self, x, y, z, w):
self.points = [x, y, z, w]
@property
def w(self):
return self.points[3]
@w.setter
def w(self, value):
self.points[3] = value