use super::Point2;
use crate::predicates::{Orientation, orient2d};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Segment2 {
a: Point2,
b: Point2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PointSegmentRelation {
Endpoint,
Interior,
NotOnSegment,
}
impl Segment2 {
pub fn new(a: Point2, b: Point2) -> Self {
Segment2 { a, b }
}
#[inline]
pub fn a(&self) -> Point2 {
self.a
}
#[inline]
pub fn b(&self) -> Point2 {
self.b
}
pub fn is_zero_length(&self) -> bool {
self.a == self.b
}
pub fn relation_to(&self, p: Point2) -> PointSegmentRelation {
if self.is_zero_length() {
return if p == self.a {
PointSegmentRelation::Endpoint
} else {
PointSegmentRelation::NotOnSegment
};
}
if orient2d(self.a, self.b, p) != Orientation::Collinear {
return PointSegmentRelation::NotOnSegment;
}
if p == self.a || p == self.b {
return PointSegmentRelation::Endpoint;
}
if point_in_collinear_range(self.a, self.b, p) {
PointSegmentRelation::Interior
} else {
PointSegmentRelation::NotOnSegment
}
}
}
pub(crate) fn point_in_collinear_range(a: Point2, b: Point2, p: Point2) -> bool {
if a.x() != b.x() {
let (lo, hi) = (a.x().min(b.x()), a.x().max(b.x()));
lo <= p.x() && p.x() <= hi
} else {
let (lo, hi) = (a.y().min(b.y()), a.y().max(b.y()));
lo <= p.y() && p.y() <= hi
}
}
#[cfg(test)]
mod tests {
use super::*;
fn p(x: f64, y: f64) -> Point2 {
Point2::new(x, y).unwrap()
}
#[test]
fn accessors() {
let s = Segment2::new(p(0.0, 0.0), p(1.0, 1.0));
assert_eq!(s.a(), p(0.0, 0.0));
assert_eq!(s.b(), p(1.0, 1.0));
}
#[test]
fn zero_length_detection() {
assert!(Segment2::new(p(1.0, 1.0), p(1.0, 1.0)).is_zero_length());
assert!(!Segment2::new(p(0.0, 0.0), p(1.0, 1.0)).is_zero_length());
}
#[test]
fn relation_to_basic_cases() {
let s = Segment2::new(p(0.0, 0.0), p(4.0, 0.0));
assert_eq!(s.relation_to(p(0.0, 0.0)), PointSegmentRelation::Endpoint);
assert_eq!(s.relation_to(p(4.0, 0.0)), PointSegmentRelation::Endpoint);
assert_eq!(s.relation_to(p(2.0, 0.0)), PointSegmentRelation::Interior);
assert_eq!(
s.relation_to(p(5.0, 0.0)),
PointSegmentRelation::NotOnSegment
);
assert_eq!(
s.relation_to(p(-1.0, 0.0)),
PointSegmentRelation::NotOnSegment
);
assert_eq!(
s.relation_to(p(2.0, 1.0)),
PointSegmentRelation::NotOnSegment
);
}
#[test]
fn relation_to_vertical_segment() {
let s = Segment2::new(p(3.0, -2.0), p(3.0, 5.0));
assert_eq!(s.relation_to(p(3.0, 0.0)), PointSegmentRelation::Interior);
assert_eq!(
s.relation_to(p(3.0, 10.0)),
PointSegmentRelation::NotOnSegment
);
assert_eq!(
s.relation_to(p(4.0, 0.0)),
PointSegmentRelation::NotOnSegment
);
}
#[test]
fn relation_to_zero_length_segment() {
let s = Segment2::new(p(2.0, 2.0), p(2.0, 2.0));
assert_eq!(s.relation_to(p(2.0, 2.0)), PointSegmentRelation::Endpoint);
assert_eq!(
s.relation_to(p(2.0, 3.0)),
PointSegmentRelation::NotOnSegment
);
}
#[test]
fn relation_to_diagonal_and_extreme_scale() {
for &scale in &[1.0_f64, 1e-100, 1e100] {
let s = Segment2::new(p(0.0, 0.0), p(scale, scale));
assert_eq!(
s.relation_to(p(scale * 0.5, scale * 0.5)),
PointSegmentRelation::Interior,
"scale {scale}"
);
assert_eq!(
s.relation_to(p(scale * 2.0, scale * 2.0)),
PointSegmentRelation::NotOnSegment,
"scale {scale}"
);
}
}
}