Skip to content
CoreSource checked

Point-to-Segment Distance

Computes the shortest distance from a point to a finite line segment.

Quick Facts

Compact metadata for implementation and discovery.

Category
Geometry
Difficulty
Intermediate
Family
Distance Queries
Tier
Core
Status
Verified
Review
Source Checked
Sources
2
Formulas
1
Relationships
5
Code example
Ready
Visualization
Ready

Overview

Point-to-segment distance projects the point onto the segment's supporting line, clamps the projection parameter to the segment endpoints, and measures the resulting Euclidean distance.

Core Concepts

point distance querydistance queriesgeometric primitivesgeometry

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(1)

Average

O(1)

Worst

O(1)

Space

O(1)

When to Use

  • Use when the closest point must lie on a finite segment, not an infinite line.
  • Use in collision, snapping, simplification, or geometry query code.

When Not to Use

  • Avoid when the segment may be degenerate unless endpoint behavior is defined.
  • Avoid when a different metric than Euclidean distance is required.

What It Does

  • Point-to-Segment Distance uses projecting a point onto a segment and clamping the parameter for point distance query
  • Organizes the work around point distance query and distance queries.

Use Cases

  • Production and research workloads requiring distance queries via Computes the shortest distance from a point to a finite line segment.
  • Distance Queries pipelines where O(1) time and O(1) space fit system constraints.
  • Engineering comparisons against Circle-Line Intersection and Orthogonal Range Search when tuning distance queries.

Inputs

  • Points, segments, polygons, or geometric primitives
  • Query or construction parameters

Outputs

  • Geometric decision, measurement, or constructed structure

Key Formulas (1)

Clamped projection

t = clamp(dot(p−a, b−a) / dot(b−a, b−a), 0, 1)
t = clamp(dot(p-a, b-a) / dot(b-a, b-a), 0, 1)

Finds the closest point on a segment to the query point.

Pseudocode

  1. 1.Let AB = B - A and AP = P - A
  2. 2.If AB has zero length, return distance from P to A
  3. 3.Compute t = dot(AP, AB) / dot(AB, AB)
  4. 4.Clamp t between 0 and 1
  5. 5.Closest point is A + t * AB
  6. 6.Return distance from P to closest point

Canonical Example

Input
point P(2,1), segment A(0,0) to B(3,0)
Output
distance 1
Explanation
The projection falls inside the segment at (2,0), one unit below P.

Implementation Notes

  • Compute t = dot(P-A, B-A) / dot(B-A, B-A).
  • Clamp t to [0, 1] before computing the closest point.
  • Return squared distance when only comparisons are needed.

Common Pitfalls

  • Forgetting to clamp the projection parameter gives line distance instead of segment distance.
  • Zero-length segments require a point-to-point fallback.
  • Squared distance comparisons should not mix with unsquared distances.

Interactive Mechanism Walkthrough

Spatial Primitive & Sweep-Line Canvas

Show projecting a point onto a segment and clamping the parameter on a small point distance query example.

Point-to-Segment Distance mechanism walkthrough step 1 of 6
Step 1 of 6: Let AB = B - A and AP = P - A
geometry-mechanism

Notes

The clamping step is the difference between point-to-line and point-to-segment distance.

Sources (2)

What each reference supports for this entry.

Computational Geometry: Algorithms and Applications

Book

Computational geometry textbook support for Point-to-Segment Distance's geometric model, data structures, and robustness concerns.

Open source

The Algorithm Design Manual

Book

Algorithm catalog support for Point-to-Segment Distance's problem context, known alternatives, and practical selection tradeoffs.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Conceptual neighborCircle-Line Intersection

Both use distance queries ideas for related point distance query problems.

Conceptual neighborOrthogonal Range Search

Both use distance queries ideas for related point distance query problems.

Conceptual neighborPoint-Line Distance

Both use distance queries ideas for related point distance query problems.

Tags

GeometryComputational Geometry