MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / segments_intersect

Function segments_intersect

nodedb-spatial/src/predicates/edge.rs:44–70  ·  view source on GitHub ↗

Whether two segments (p1-q1) and (p2-q2) intersect. Uses the standard orientation-based algorithm. Handles collinear overlapping segments correctly.

(p1: [f64; 2], q1: [f64; 2], p2: [f64; 2], q2: [f64; 2])

Source from the content-addressed store, hash-verified

42/// Uses the standard orientation-based algorithm. Handles collinear
43/// overlapping segments correctly.
44pub fn segments_intersect(p1: [f64; 2], q1: [f64; 2], p2: [f64; 2], q2: [f64; 2]) -> bool {
45 let o1 = orientation(p1, q1, p2);
46 let o2 = orientation(p1, q1, q2);
47 let o3 = orientation(p2, q2, p1);
48 let o4 = orientation(p2, q2, q1);
49
50 // General case: different orientations mean crossing.
51 if o1 != o2 && o3 != o4 {
52 return true;
53 }
54
55 // Collinear special cases: check if endpoints lie on the other segment.
56 if o1 == Orientation::Collinear && on_segment(p1, p2, q1) {
57 return true;
58 }
59 if o2 == Orientation::Collinear && on_segment(p1, q2, q1) {
60 return true;
61 }
62 if o3 == Orientation::Collinear && on_segment(p2, p1, q2) {
63 return true;
64 }
65 if o4 == Orientation::Collinear && on_segment(p2, q1, q2) {
66 return true;
67 }
68
69 false
70}
71
72/// Whether a point lies exactly on a line segment (within epsilon tolerance).
73pub fn point_on_segment(pt: [f64; 2], seg_a: [f64; 2], seg_b: [f64; 2]) -> bool {

Callers 5

linestrings_intersectFunction · 0.85
polygons_intersectFunction · 0.85

Calls 2

orientationFunction · 0.85
on_segmentFunction · 0.85

Tested by

no test coverage detected