liveBook · Manning

Line segment

As explained in Geometry for Programmers

A finite portion of a line bounded by two endpoints.

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

How Geometry for Programmers covers it

How deep it goes
Works through projection cases, distance formulas, smooth joining, and implementation trade-offs; stops short of a fully general curve treatment.
Examples
Uses arc-segment joins, rectangle signed distance functions, triangle meshes, and pixel-derived contours split into short segments.
What it assumes
Assumes familiarity with points and tangent vectors, while introducing projection, orthogonality, and vector-product uses through geometric reasoning.
What you can do after
Apply via Python and SymPy to calculate segment distances, construct rectangle distance fields, and represent or smooth geometric contours.
How it connects
Connects segments to arcs, tangent vectors, dot products, signed distance functions, triangle meshes, and cubic polynomial curves.

Also explained in

Compare how 4 books explain line segment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Geometry for Programmers. The section links point to the parts of the book it draws on.