gistools/geometry/tools/lines/
point_on_line.rs1use crate::geometry::orient2d;
2use libm::{fabs, fmax, fmin};
3use s2json::GetXY;
4
5pub fn point_on_line<P: GetXY, Q: GetXY>(line: &[P], point: &Q, eps: Option<f64>) -> bool {
15 let eps = eps.unwrap_or(0.0);
16
17 if line.len() < 2 {
18 return false;
19 }
20
21 let mut i = 0;
22 while i < line.len() - 1 {
23 let a = &line[i];
25 let b = &line[i + 1];
26 if point.x() >= fmin(a.x(), b.x())
27 && point.x() <= fmax(a.x(), b.x())
28 && point.y() >= fmin(a.y(), b.y())
29 && point.y() <= fmax(a.y(), b.y())
30 {
31 let cross = orient2d(a.x(), a.y(), b.x(), b.y(), point.x(), point.y());
33 if fabs(cross) <= eps {
34 return true;
35 }
36 }
37
38 i += 1;
39 }
40
41 false
42}