Home

onsegment

Onsegment is a geometric predicate used to determine whether a point lies on a closed line segment AB. A point P is on segment AB if P lies on the line through A and B and is between A and B, with endpoints included. Equivalently, there exists a parameter t in [0,1] such that P = A + t(B − A). This condition applies in both two- and three-dimensional space.

A practical way to test on-segment is to first check collinearity of A, B, and P, and

In practice, on-segment tests appear in polygon processing, segment-intersection algorithms, computer graphics, and geographic information systems.

then
verify
that
P
lies
within
the
inclusive
bounding
box
determined
by
A
and
B.
In
coordinates,
this
means
min(ax,
bx)
≤
px
≤
max(ax,
bx)
and
min(ay,
by)
≤
py
≤
max(ay,
by)
in
2D
(and
similarly
for
additional
dimensions).
A
common
numerical
criterion
uses
the
cross
product
(or
the
area
of
triangle
ABP)
to
test
collinearity
and
the
dot
product
condition
(P
−
A)
·
(P
−
B)
≤
0
to
ensure
P
lies
between
A
and
B.
A
degenerate
segment
where
A
=
B
contains
only
that
single
point;
then
P
lies
on
the
segment
if
and
only
if
P
=
A.
Robust
implementations
may
separate
the
collinearity
check
from
range
checks
and
often
employ
exact
arithmetic
or
tolerance
thresholds
to
handle
floating-point
coordinates
and
numerical
errors.
Related
concepts
include
on_line
(P
lies
on
the
infinite
line
AB)
and
between
(P
lies
between
A
and
B
along
a
line,
without
necessarily
requiring
collinearity).