use super::{Point2, PointSegmentRelation, Segment2};
use crate::predicates::{Orientation, orient2d};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Triangle2 {
a: Point2,
b: Point2,
c: Point2,
}
impl Triangle2 {
pub fn new(a: Point2, b: Point2, c: Point2) -> Self {
Triangle2 { a, b, c }
}
#[inline]
pub fn a(&self) -> Point2 {
self.a
}
#[inline]
pub fn b(&self) -> Point2 {
self.b
}
#[inline]
pub fn c(&self) -> Point2 {
self.c
}
pub fn orientation(&self) -> Orientation {
orient2d(self.a, self.b, self.c)
}
pub fn relation_to(&self, p: Point2) -> PointTriangleRelation {
if self.orientation() == Orientation::Collinear {
let on_any_edge = [
Segment2::new(self.a, self.b),
Segment2::new(self.b, self.c),
Segment2::new(self.c, self.a),
]
.iter()
.any(|edge| edge.relation_to(p) != PointSegmentRelation::NotOnSegment);
return if on_any_edge {
PointTriangleRelation::OnBoundary
} else {
PointTriangleRelation::Outside
};
}
let edges = [
orient2d(self.a, self.b, p),
orient2d(self.b, self.c, p),
orient2d(self.c, self.a, p),
];
let has_ccw = edges.contains(&Orientation::CounterClockwise);
let has_cw = edges.contains(&Orientation::Clockwise);
if has_ccw && has_cw {
PointTriangleRelation::Outside
} else if edges.contains(&Orientation::Collinear) {
PointTriangleRelation::OnBoundary
} else {
PointTriangleRelation::Inside
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PointTriangleRelation {
Inside,
OnBoundary,
Outside,
}
#[cfg(test)]
mod tests {
use super::*;
fn p(x: f64, y: f64) -> Point2 {
Point2::new(x, y).unwrap()
}
#[test]
fn orientation_matches_orient2d() {
let t = Triangle2::new(p(0.0, 0.0), p(1.0, 0.0), p(0.0, 1.0));
assert_eq!(t.orientation(), Orientation::CounterClockwise);
}
#[test]
fn degenerate_triangle_is_collinear() {
let t = Triangle2::new(p(0.0, 0.0), p(1.0, 0.0), p(2.0, 0.0));
assert_eq!(t.orientation(), Orientation::Collinear);
}
#[test]
fn relation_to_ccw_triangle() {
let t = Triangle2::new(p(0.0, 0.0), p(4.0, 0.0), p(0.0, 4.0));
assert_eq!(t.relation_to(p(1.0, 1.0)), PointTriangleRelation::Inside);
assert_eq!(
t.relation_to(p(0.0, 0.0)),
PointTriangleRelation::OnBoundary
);
assert_eq!(
t.relation_to(p(2.0, 0.0)),
PointTriangleRelation::OnBoundary
);
assert_eq!(
t.relation_to(p(2.0, 2.0)),
PointTriangleRelation::OnBoundary
);
assert_eq!(t.relation_to(p(-1.0, -1.0)), PointTriangleRelation::Outside);
assert_eq!(t.relation_to(p(5.0, 5.0)), PointTriangleRelation::Outside);
assert_eq!(t.relation_to(p(3.0, 3.0)), PointTriangleRelation::Outside);
assert_eq!(t.relation_to(p(2.0, -1.0)), PointTriangleRelation::Outside);
}
#[test]
fn relation_to_cw_triangle_same_as_ccw() {
let t = Triangle2::new(p(0.0, 0.0), p(0.0, 4.0), p(4.0, 0.0));
assert_eq!(t.relation_to(p(1.0, 1.0)), PointTriangleRelation::Inside);
assert_eq!(t.relation_to(p(5.0, 5.0)), PointTriangleRelation::Outside);
}
#[test]
fn degenerate_triangle_never_contains_off_line_point() {
let t = Triangle2::new(p(0.0, 0.0), p(1.0, 0.0), p(2.0, 0.0));
assert_eq!(
t.relation_to(p(0.5, 0.0)),
PointTriangleRelation::OnBoundary
);
assert_eq!(t.relation_to(p(0.5, 1.0)), PointTriangleRelation::Outside);
assert_eq!(t.relation_to(p(10.0, 0.0)), PointTriangleRelation::Outside);
}
}