A line segment is a finite portion of a line bounded by two endpoints. It is a practical geometry primitive: combine it with an arc for a smooth mixed curve, measure distances to it, or use it as a boundary in a mesh or contour. section 5.3.2 section 11.2.3
Useful operations and representations include:
- Join a segment to an arc and enforce a smooth connection by making the radius-to-intersection vector orthogonal to the segment. section 5.3.2
- Compute point-to-segment distance by projecting onto the supporting line, then choosing the line distance or the nearer endpoint distance. section 10.1.4
- Use segments as simple geometric data in triangle meshes, trading approximation accuracy against model size, performance, and reliability. section 11.2.3
- Approximate curved contours with many short linear segments when plotting simplicity matters, while retaining cubic curves for smoothing. section 12.4.5
Rectangle distance field
A rectangle can be treated as four segments. For each query point, calculate its distance to every edge, select the smallest value, and attach the sign determined by the corresponding supporting line. section 10.1.4
def distance_p2e(x, y, x1, y1, x2, y2): #1
projection_normalized = (dot2d(x-x1, y-y1, x2-x1, y2-y1)
/ distance_p2p(x1, y1, x2, y2) ** 2)
if projection_normalized < 0:
return distance_p2p(x, y, x1, y1)
elif projection_normalized > 1:
return distance_p2p(x, y, x2, y2)
else:
return distance_p2l(x, y, x1, y1, x2, y2)
Inspect the Python implementation of projection-based point-to-segment distance, including endpoint and interior-projection cases. section 10.1.4



