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 get_point_if_exists(point: Point) -> Point: """Check if a suitable point already exists and use it otherwise create one""" for p in points.keys(): if point.common_point(p): print("Point already exists!") return p else: print("Create point") points[point] = point return point
def __init__(self, point1: Point, point2: Point, point3: Point, register: bool = True): self.id = next(_id) self.points = [point1, point2, point3] self.centroid: Point = self.get_centroid(True) if register: point1.register_face(self) point2.register_face(self) point3.register_face(self)
def __init__(self, centre_point: Point, hex_size: float = 1): hex_size = max(0.01, min(1.0, hex_size)) self.centre_point = centre_point self.faces: List[Face] = centre_point.get_ordered_faces() self.boundary: List[Point] = [] self.neighbour_ids: List[Point] = [] self.neighbours: List[Tile] = [] neighbour_hash: Dict[Point, int] = {} for f in self.faces: centroid_point = f.get_centroid() self.boundary.append( centroid_point.segment(self.centre_point, hex_size)) other_points = f.get_other_points(self.centre_point) for other in other_points: neighbour_hash[other] = 1 self.neighbour_ids = list(neighbour_hash.keys()) normal = calculate_surface_normal(*self.boundary[:3]) if not pointing_away_from_origin(self.centre_point, normal): self.boundary.reverse()
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) ]