The Line
1 min read•1 page
The First Dimension
A point has no dimensions. But when you connect two points, you create a path between them. This path has exactly one dimension: Length.
In computational geometry, we strictly deal with Line Segments—finite stretches of space strictly bounded by two specific endpoints.
Segment vs Infinite Line: In pure mathematics, a "Line" goes on forever in both directions. In code, what we call a "Line" is almost always a "Line Segment". It has a definitive start and a definitive end.
1 min read•1 page
The Pure Data Structure
To define a Line Segment, you just need to define the end points, the translate, move, scale of the Line is based on the translate, move, scale of the Point.
python
1class Line3d:2 """3 Represents a finite straight segment between two points.4 """5 def __init__(self, start: Point3d, end: Point3d):6 self.Start = start7 self.End = end89 def __str__(self):10 return f"Line({self.Start}, {self.End})"
1 min read•1 page
PointAt(t):
This is arguably the most used method of any curve. By feeding it a parameter t from 0.0 to 1.0, it slides along the line.
t = 0.0returns theStartpoint.t = 1.0returns theEndpoint.t = 0.5returns the exact midpoint.
python
1class Line3d:2 def PointAt(self, t: float) -> Point3d:3 """Evaluates the line at parameter t (0.0 to 1.0)."""4 vec = self.Start.VectorTo(self.End)5 scaled_vec = vec.Scale(t)6 return self.Start.Move(scaled_vec)
Parameter (t)
0.50Length & Direction:
Since a Line is defined by two points, its length is simply the distance between them, and its direction is the normalized unit vector pointing from Start to End.
python
1class Line3d:2 @property3 def Length(self) -> float:4 return self.Start.DistanceTo(self.End)56 @property7 def Direction(self) -> Vector3d:8 vec = self.Start.VectorTo(self.End)9 return vec.Unitize()
1 min read•1 page
Flip():
Swaps the Start and End points of a line segment. This reverses its Direction vector and flips the parameter domain (what was
t=0 becomes t=1).python
1class Line:2 def Flip(self):3 """Swaps the start and end points, reversing the direction."""4 temp = self.Start5 self.Start = self.End6 self.End = temp78 # Note: This means PointAt(0) is now the old end point!
Flip
1 min read•1 page
Extend(L0, L1):
Changes the length of a line segment from its start or end point while maintaining its original direction. Negative values will shorten the line.
python
1class Line:2 def Extend(self, start_length: float, end_length: float):3 """Lengthens or shortens the line while keeping its direction."""4 dir = self.Direction56 # Move Start point backwards by start_length7 self.Start = self.Start - (dir * start_length)89 # Move End point forwards by end_length10 self.End = self.End + (dir * end_length)
Extend Start
1.00Extend End
2.002 min read•1 page
ClosestPoint(Point3d):
Finds the spot on the line that is closest to a random external point. This involves projecting a vector onto the line's direction vector.
python
1class Line3d:2 def ClosestPoint(self, test_point: Point3d) -> Point3d:3 """Projects a point orthogonally onto the segment."""4 ab = self.end - self.start5 ap = test_point - self.start67 # Calculate projection parameter t along segment8 t = ap.DotProduct(ab) / ab.SquareLength()9 t = max(0.0, min(1.0, t))1011 return self.start + ab * t
This finds the coordinates of a Point on the line segment.
Test Point X
2.30Test Point Y
5.001 min read•1 page
Move(Vector3d):
Translates the line by moving its start and end points.
python
1class Line3d:2 def Move(self, vector: Vector3d) -> 'Line3d':3 """Moves the line by translating its underlying points."""4 return Line3d(5 self.Start.Move(vector),6 self.End.Move(vector)7 )
Move X
0.00Move Y
3.00Because a Line is structurally just two points, transforming a line simply means passing the transformation command down to its
Start and End points.1 min read•1 page
Scale(factor, center_point):
To scale a line, you explicitly scale its
Start point and End point relative to a specific center of scaling.python
1class Line3d:2 def Scale(self, factor: float, center: Point3d) -> 'Line3d':3 """Scales the line from a specific center point."""4 return Line3d(5 self.Start.Scale(factor, center),6 self.End.Scale(factor, center)7 )
Scale Factor
1.50Center X
1.70Center Y
-3.901 min read•1 page
Rotate(angle, axis, center_point):
Rotates the line around a center point and axis by transforming its start and end points.
python
1class Line3d:2 def Rotate(self, angle: float, axis: Vector3d, center: Point3d) -> 'Line3d':3 """Rotates the line's points around an axis and center."""4 return Line3d(5 self.Start.Rotate(angle, axis, center),6 self.End.Rotate(angle, axis, center)7 )
Angle
45.003 min read•1 page
Intersection(Line3d):
Calculates the point of intersection between two lines. If the lines are skew (do not intersect in 3D), it returns the closest point approach.
python
1def line_line_intersection(lineA, lineB):2 d1 = lineA.end - lineA.start3 d2 = lineB.end - lineB.start4 r = lineA.start - lineB.start56 a = d1.DotProduct(d1)7 b = d1.DotProduct(d2)8 c = d1.DotProduct(r)9 e = d2.DotProduct(d2)10 f = d2.DotProduct(r)1112 denom = a * e - b * b13 s, t = 0.0, 0.01415 if denom != 0.0:16 s = max(0.0, min(1.0, (b * f - c * e) / denom))1718 t = max(0.0, min(1.0, (b * s + f) / e))1920 if denom != 0.0:21 s = max(0.0, min(1.0, (b * t - c) / a))2223 ptA = lineA.start + d1 * s24 ptB = lineB.start + d2 * t25 return ptA, ptB
Move Line B (X)
0.30Move Line B (Y)
-2.602 min read•1 page
Project:
Drops a point perfectly perpendicular onto the infinite axis defined by a line segment.
python
1def ProjectOntoInfiniteLine(line: Line, point: Point3d) -> Point3d:2 """Projects a point orthogonally onto an infinite line."""34 # Calculate the t-parameter of the closest point5 t = line.ClosestParameter(point)67 # Evaluate the line at that exact t-parameter8 # (Because t can be > 1 or < 0, this extends beyond the segment)9 return line.PointAt(t)
Point X
4.00Point Y
3.00Unlike "Closest Point", which clamps the result to the physical start and end of the segment, projection treats the line as an infinite rail. This is extremely useful for aligning objects to a grid or finding coordinates relative to a custom axis.