use geometry_algorithm::{distance, distance_with};
use geometry_cs::Cartesian;
use geometry_model::{Point2D, Segment};
use geometry_strategy::cartesian::{PointToSegment, Pythagoras};
use geometry_strategy::distance::Reversed;
#[test]
#[allow(
clippy::float_cmp,
reason = "Reversed<S>::distance(b, a) literally forwards to S::distance(a, b) — \
the two values are bit-identical by construction, not by numerical \
proximity, so strict equality is the right assertion."
)]
fn distance_segment_to_point_matches_point_to_segment() {
let s = Segment::new(
Point2D::<f64, Cartesian>::new(0.0, 0.0),
Point2D::<f64, Cartesian>::new(4.0, 0.0),
);
let p = Point2D::<f64, Cartesian>::new(2.0, 2.0);
let pt_to_seg = distance_with(&p, &s, PointToSegment::<Pythagoras>::default());
let seg_to_pt = distance_with(&s, &p, Reversed(PointToSegment::<Pythagoras>::default()));
assert_eq!(pt_to_seg, seg_to_pt);
assert!((pt_to_seg - 2.0).abs() < 1e-12);
}
#[test]
#[allow(
clippy::float_cmp,
reason = "Reversed<S>::distance(b, a) literally forwards to S::distance(a, b) — \
the two values are bit-identical by construction, not by numerical \
proximity, so strict equality is the right assertion."
)]
fn reversed_proj_case() {
let s = Segment::new(
Point2D::<f64, Cartesian>::new(1.0, 4.0),
Point2D::<f64, Cartesian>::new(4.0, 1.0),
);
let p = Point2D::<f64, Cartesian>::new(6.0, 1.0);
let pt_to_seg = distance_with(&p, &s, PointToSegment::<Pythagoras>::default());
let seg_to_pt = distance_with(&s, &p, Reversed(PointToSegment::<Pythagoras>::default()));
assert_eq!(pt_to_seg, seg_to_pt);
assert!((pt_to_seg - 2.0).abs() < 1e-12);
}
#[test]
fn explicit_reversed_strategy_works() {
let s = Segment::new(
Point2D::<f64, Cartesian>::new(0.0, 0.0),
Point2D::<f64, Cartesian>::new(10.0, 0.0),
);
let p = Point2D::<f64, Cartesian>::new(5.0, 7.0);
let strat = Reversed(PointToSegment::<Pythagoras>::default());
let d = distance_with(&s, &p, strat);
assert!((d - 7.0).abs() < 1e-12);
}
#[test]
#[allow(
clippy::float_cmp,
reason = "Pythagoras is symmetric by construction over IEEE-754 floats: \
the sum of squared differences is commutative in the inputs."
)]
fn point_point_is_self_symmetric() {
let a = Point2D::<f64, Cartesian>::new(1.0, 2.0);
let b = Point2D::<f64, Cartesian>::new(3.0, 4.0);
assert_eq!(distance(&a, &b), distance(&b, &a));
}