The Point
Point3d(X, Y, Z):
A point is the fundamental building block of all geometry. It has no physical dimensions (no length, width, or depth); instead, it is purely a coordinate location in space.
1class Point3d:2 def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0):3 self.X = float(x)4 self.Y = float(y)5 self.Z = float(z)67pt = Point3d(-2.0, 1.5, 0.0)
Coordinates (X, Y, Z):
While a vector is a collection of numbers representing direction and magnitude [X, Y, Z] floating freely without a fixed location, a Point is strictly anchored to the Origin (0, 0, 0).
When you evaluate the coordinates (X, Y, Z), you are measuring the position vector required to travel from the Origin to that absolute location. At its core, a point is stored in memory as an ordered tuple of three 64-bit floats (X, Y, Z).
1class Point3d:2 def __init__(self, x: float, y: float, z: float):3 self.X = float(x)4 self.Y = float(y)5 self.Z = float(z)67 def __str__(self):8 return f"Point3d({self.X}, {self.Y}, {self.Z})"910 @classmethod11 def Origin(cls):12 """Gets the point at (0,0,0)."""13 return cls(0.0, 0.0, 0.0)
Subtract(Point3d):
A from target point B (B - A, or B.Subtract(A)) yields the Vector3d pointing from A to B. Remember: Target minus Start.1class Point3d:2 def Subtract(self, other: 'Point3d') -> 'Vector3d':3 """B.Subtract(A) (or B - A) yields the Vector pointing from A to B."""4 return Vector3d(5 self.X - other.X,6 self.Y - other.Y,7 self.Z - other.Z8 )910# Target minus Start: B - A points from A to B11vec_ab = point_b.Subtract(point_a)
DistanceTo(Point3d):
1import math23class Point3d:4 def DistanceTo(self, other: 'Point3d') -> float:5 """Returns the distance between two points."""6 dx, dy, dz = other.X - self.X, other.Y - self.Y, other.Z - self.Z7 return math.sqrt(dx**2 + dy**2 + dz**2)
DistanceToSquared(Point3d):
1import math23class Point3d:4 def DistanceTo(self, other: 'Point3d') -> float:5 # EXPENSIVE: math.sqrt is a heavy operation6 dx = self.X - other.X7 dy = self.Y - other.Y8 dz = self.Z - other.Z9 return math.sqrt(dx**2 + dy**2 + dz**2)1011 def DistanceToSquared(self, other: 'Point3d') -> float:12 # CHEAP: No square root required13 dx = self.X - other.X14 dy = self.Y - other.Y15 dz = self.Z - other.Z16 return dx**2 + dy**2 + dz**2
Square roots (math.sqrt) are computationally expensive. When writing algorithms likeClosestPoint that compare thousands of distances to find the smallest one, we don't need the actual distance, we just need to know which one is relatively smaller.
If A < B, then A² < B². Using DistanceToSquared inside loops saves significant processing time.
Interpolate(Point3d, float):
t.1class Point3d:2 def Interpolate(self, other: 'Point3d', t: float) -> 'Point3d':3 """Finds a point between self and other based on parameter t."""4 # t = 0.0 returns self5 # t = 1.0 returns other6 # t = 0.5 returns the midpoint78 return Point3d(9 self.X + (other.X - self.X) * t,10 self.Y + (other.Y - self.Y) * t,11 self.Z + (other.Z - self.Z) * t12 )
Centroid(list[Point3d]):
1def Centroid(points: list[Point3d]) -> Point3d:2 """Calculates the arithmetic average of a list of points."""3 n = len(points)4 if n == 0:5 return None67 cx = sum(p.X for p in points) / n8 cy = sum(p.Y for p in points) / n9 cz = sum(p.Z for p in points) / n1011 return Point3d(cx, cy, cz)
WeightedCenter(list[Point3d], list[float]):
1def WeightedCenter(points: list[Point3d], weights: list[float]) -> Point3d:2 """Calculates the center of mass based on point weights."""3 total_weight = sum(weights)45 if total_weight == 0:6 return None78 cx = sum(p.X * w for p, w in zip(points, weights)) / total_weight9 cy = sum(p.Y * w for p, w in zip(points, weights)) / total_weight10 cz = sum(p.Z * w for p, w in zip(points, weights)) / total_weight1112 return Point3d(cx, cy, cz)
Move(Vector3d):
1class Point3d:2 def Move(self, vector: 'Vector3d') -> 'Point3d':3 """Translates the point by adding a vector."""4 return Point3d(self.X + vector.X, self.Y + vector.Y, self.Z + vector.Z)
Scale(float):
2.0 doubles the length of the vector connecting the Origin to the Point.1class Point3d:2 def Scale(self, scale_factor: float) -> 'Point3d':3 """Scales the point relative to the origin."""4 return Point3d(self.X * scale_factor, self.Y * scale_factor, self.Z * scale_factor)
Rotate(Axis3d, float):
1import math23class Point3d:4 def Rotate(self, axis: 'Vector3d', angle_degrees: float) -> 'Point3d':5 """Rotates the point around the given unit axis."""6 angle_rad = math.radians(angle_degrees)7 cos_a = math.cos(angle_rad)8 sin_a = math.sin(angle_rad)910 ux, uy, uz = axis.X, axis.Y, axis.Z1112 # Rodrigues' rotation formula applied to the position vector13 x_new = self.X * (cos_a + ux*ux*(1-cos_a)) + self.Y * (ux*uy*(1-cos_a) - uz*sin_a) + self.Z * (ux*uz*(1-cos_a) + uy*sin_a)14 y_new = self.X * (uy*ux*(1-cos_a) + uz*sin_a) + self.Y * (cos_a + uy*uy*(1-cos_a)) + self.Z * (uy*uz*(1-cos_a) - ux*sin_a)15 z_new = self.X * (uz*ux*(1-cos_a) - uy*sin_a) + self.Y * (uz*uy*(1-cos_a) + ux*sin_a) + self.Z * (cos_a + uz*uz*(1-cos_a))1617 return Point3d(x_new, y_new, z_new)
FindClosestPoint(Point3d, list[Point3d]):
1def FindClosestPoint(test_pt: Point3d, cloud: list[Point3d]) -> Point3d:2 """Brute force search for the closest point."""3 closest = cloud[0]4 min_dist = test_pt.DistanceTo(closest)56 for pt in cloud:7 d = test_pt.DistanceTo(pt)8 if d < min_dist:9 min_dist = d10 closest = pt11 return closest
DistanceToPlane(Plane):
1def DistanceToPlane(point: Point3d, plane_origin: Point3d, plane_normal: Vector3d) -> float:2 """Calculates the signed distance from a point to a plane."""3 # Vector from plane origin to the test point4 v = point - plane_origin56 # Project the vector onto the plane normal7 # Ensure normal is unitized8 n = Vector3d(plane_normal)9 n.Unitize()1011 # Signed distance is the dot product12 return v * n
ProjectTo(Plane):
1def ProjectToPlane(point: Point3d, plane_origin: Point3d, plane_normal: Vector3d) -> Point3d:2 # Get signed distance to plane3 d = DistanceToPlane(point, plane_origin, plane_normal)45 # Shift point back along normal6 return point - plane_normal * d
IsOnPlane(Plane):
1def IsOnPlane(point: Point3d, plane_origin: Point3d, plane_normal: Vector3d, tolerance: float = 1e-6) -> bool:2 """Checks if a point lies on a plane within a tolerance."""3 dist = DistanceToPlane(point, plane_origin, plane_normal)4 return abs(dist) <= tolerance
Transform(Transform):
1class Point3d:2 def Transform(self, m: list[list[float]]) -> 'Point3d':3 """Applies a 4x4 homogeneous transformation matrix (w = 1.0)."""4 x, y, z = self.X, self.Y, self.Z5 new_x = m[0][0] * x + m[0][1] * y + m[0][2] * z + m[0][3] * 1.06 new_y = m[1][0] * x + m[1][1] * y + m[1][2] * z + m[1][3] * 1.07 new_z = m[2][0] * x + m[2][1] * y + m[2][2] * z + m[2][3] * 1.08 return Point3d(new_x, new_y, new_z)
Translate, Scale, and Rotate are convenient, under the hood they all generate a 4x4 Matrix and pass it to this master Transform method.Attractor:
1# Attractor Logic2for obj_pt in grid:3 dist = attractor.DistanceTo(obj_pt)4 if dist < threshold:5 # Scale based on proximity6 obj.Scale(1.0 / dist)