use parry2d::math::Vector;
use parry2d::shape::SegmentPointLocation;
use parry2d::utils::{segments_intersection2d, SegmentsIntersection};
#[test]
fn identical_segments_intersect_on_vertices() {
let a = Vector::new(10.0, 0.0);
let b = Vector::new(10.0, 10.0);
let intersection = segments_intersection2d(a, b, a, b, 0.0).unwrap();
let SegmentsIntersection::Segment {
first_loc1,
first_loc2,
second_loc1,
second_loc2,
} = intersection
else {
panic!("the intersection should be a Segment intersection");
};
assert_eq!(first_loc1, SegmentPointLocation::OnVertex(0));
assert_eq!(first_loc2, SegmentPointLocation::OnVertex(0));
assert_eq!(second_loc1, SegmentPointLocation::OnVertex(1));
assert_eq!(second_loc2, SegmentPointLocation::OnVertex(1));
}
#[test]
fn identical_horizontal_segments_intersect_on_vertices() {
let a = Vector::new(-3.0, 2.0);
let b = Vector::new(5.0, 2.0);
let intersection = segments_intersection2d(a, b, a, b, 0.0).unwrap();
let SegmentsIntersection::Segment {
first_loc1,
first_loc2,
second_loc1,
second_loc2,
} = intersection
else {
panic!("the intersection should be a Segment intersection");
};
assert_eq!(first_loc1, SegmentPointLocation::OnVertex(0));
assert_eq!(first_loc2, SegmentPointLocation::OnVertex(0));
assert_eq!(second_loc1, SegmentPointLocation::OnVertex(1));
assert_eq!(second_loc2, SegmentPointLocation::OnVertex(1));
}
#[test]
fn crossing_at_endpoint_is_on_vertex() {
let a = Vector::new(0.0, 0.0);
let b = Vector::new(1.0, 1.0);
let c = Vector::new(1.0, 1.0);
let d = Vector::new(2.0, 0.0);
let intersection = segments_intersection2d(a, b, c, d, 0.0).unwrap();
let SegmentsIntersection::Point { loc1, loc2 } = intersection else {
panic!("the intersection should be a Point intersection");
};
assert_eq!(loc1, SegmentPointLocation::OnVertex(1));
assert_eq!(loc2, SegmentPointLocation::OnVertex(0));
}
#[test]
fn crossing_at_interior_point_is_on_edge() {
let a = Vector::new(-1.0, 0.0);
let b = Vector::new(1.0, 0.0);
let c = Vector::new(0.0, -1.0);
let d = Vector::new(0.0, 1.0);
let intersection = segments_intersection2d(a, b, c, d, 0.0).unwrap();
let SegmentsIntersection::Point { loc1, loc2 } = intersection else {
panic!("the intersection should be a Point intersection");
};
assert_eq!(loc1, SegmentPointLocation::OnEdge([0.5, 0.5]));
assert_eq!(loc2, SegmentPointLocation::OnEdge([0.5, 0.5]));
}