def calculate_surface_normal(p1: Point, p2: Point, p3: Point) -> Point: u = vector(p1, p2) v = vector(p1, p3) return Point( u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x )
def get_centroid(self, clear: bool = False) -> Point: """Get the centroid of the triangle""" if self.centroid and not clear: return self.centroid x = (self.points[0].x + self.points[1].x + self.points[2].x) / 3 y = (self.points[0].y + self.points[1].y + self.points[2].y) / 3 z = (self.points[0].z + self.points[1].z + self.points[2].z) / 3 centroid = Point(x, y, z) self.centroid = centroid return centroid
def normalize_vector(v: Point) -> Point: magnitude = math.sqrt( math.pow(v.x, 2) + math.pow(v.y, 2) + math.pow(v.z, 2) ) return Point( v.x / magnitude, v.y / magnitude, v.z / magnitude )
def vector(p1: Point, p2: Point) -> Point: return Point(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z)
def get_corners(self): tao = 1.61803399 size = 1000 return [ Point(size, tao * size, 0), Point(-size, tao * size, 0), Point(size, -tao * size, 0), Point(-size, -tao * size, 0), Point(0, size, tao * size), Point(0, -size, tao * size), Point(0, size, -tao * size), Point(0, -size, -tao * size), Point(tao * size, 0, size), Point(-tao * size, 0, size), Point(tao * size, 0, -size), Point(-tao * size, 0, -size) ]