Syntax
t=crosstest(pa,pb,pc)
Input Parameter
pa : | | point to test |
pb : | | start point of line |
pc : | | end point of line |
Output Parameter
t : | | -1==cross; 0== on the line; +1 nothing known |
Examples
crosstest ([ 0 0], [5 -5], [5 +5])
crosstest ([10 0], [5 -5], [5 +5])
crosstest ([ 5 0], [5 -5], [5 +5])
Copyright 2012-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This algorithm determines the relative position of a point pa
with respect to a line segment defined by points pb
and pc
. It returns a value indicating whether the point is to the left, on, or to the right of the line segment.
Input Parameters
- pa: The point to test, represented as a 2D coordinate.
- pb: The start point of the line segment, represented as a 2D coordinate.
- pc: The end point of the line segment, represented as a 2D coordinate.
Output Result
- t: An integer indicating the position of
pa
relative to the line segment:
-1
: pa
is to the left of the line segment.
0
: pa
is on the line segment.
+1
: pa
is to the right of the line segment or outside the horizontal bounds of the line segment.
Algorithm Steps
- Initialize a threshold
thr
for rounding stability to 1e-12
.
- Check if all points
pa
, pb
, and pc
are on the same horizontal line (same y-coordinate):
- If
pa
is between pb
and pc
horizontally, return 0
.
- Otherwise, return
1
.
- Ensure
pb
is below pc
by swapping them if necessary.
- Check if
pa
is outside the vertical bounds of the line segment:
- If
pa
is below pb
or above pc
, return 1
.
- Calculate the cross product
S
to determine the relative position of pa
:
- If
S
is within the threshold, set S
to 0
for stability.
- If
S
is positive, return 1
.
- If
S
is negative, return -1
.
- If
S
is zero, return 0
.
Algorithm explaination created using ChatGPT on 2025-08-19 07:11. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21