72 lines
1.7 KiB
Python
72 lines
1.7 KiB
Python
|
import math
|
||
|
|
||
|
|
||
|
class Vec2:
|
||
|
def __init__(self, x=0.0, y=0.0):
|
||
|
self.x = x
|
||
|
self.y = y
|
||
|
|
||
|
def __str__(self):
|
||
|
return "[%s,%s]" % (self.x, self.y)
|
||
|
|
||
|
|
||
|
class Vec3:
|
||
|
def __init__(self, x=0.0, y=0.0, z=0.0):
|
||
|
self.x = x
|
||
|
self.y = y
|
||
|
self.z = z
|
||
|
|
||
|
def __add__(self, other):
|
||
|
return Vec3(self.x + other.x, self.y + other.y, self.z + other.z)
|
||
|
|
||
|
def __sub__(self, other):
|
||
|
return self + -other
|
||
|
|
||
|
def __radd__(self, other):
|
||
|
return Vec3(self.x + other.x, self.y + other.y, self.z + other.z)
|
||
|
|
||
|
def __mul__(self, scalar):
|
||
|
return Vec3(self.x * scalar, self.y * scalar, self.z * scalar)
|
||
|
|
||
|
def __rmul__(self, scalar):
|
||
|
return Vec3(self.x * scalar, self.y * scalar, self.z * scalar)
|
||
|
|
||
|
def __neg__(self):
|
||
|
return Vec3(-self.x, -self.y, -self.z)
|
||
|
|
||
|
def __pos__(self):
|
||
|
return Vec3(self.x, self.y, self.z)
|
||
|
|
||
|
def __xor__(self, other):
|
||
|
cx = self.y * other.z - self.z * other.y
|
||
|
cy = self.z * other.x - self.x * other.z
|
||
|
cz = self.x * other.y - self.y * other.x
|
||
|
return Vec3(cx, cy, cz)
|
||
|
|
||
|
def cross(self, other):
|
||
|
return self ^ other
|
||
|
|
||
|
def normalize(self):
|
||
|
w = math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
|
||
|
if w == 0:
|
||
|
return Vec3()
|
||
|
else:
|
||
|
return Vec3(self.x / w, self.y / w, self.z / w)
|
||
|
|
||
|
def __str__(self):
|
||
|
return "[%s,%s,%s]" % (self.x, self.y, self.z)
|
||
|
|
||
|
|
||
|
class VecRGB:
|
||
|
def __init__(self, r=0, g=0, b=0):
|
||
|
self.r = r
|
||
|
self.g = g
|
||
|
self.b = b
|
||
|
|
||
|
def __str__(self):
|
||
|
return "[%s,%s,%s]" % (self.r, self.g, self.b)
|
||
|
|
||
|
|
||
|
def cross(v1: Vec3, v2: Vec3):
|
||
|
return v1.cross(v2)
|