jagua_rs/geometry/primitives/
point.rs1use std::hash::{Hash, Hasher};
2
3use crate::geometry::Transformation;
4use crate::geometry::geo_traits::{CollidesWith, DistanceTo, Transformable, TransformableFrom};
5
6#[derive(Debug, Clone, PartialEq, Copy)]
8pub struct Point(pub f32, pub f32);
9
10impl Transformable for Point {
11 fn transform(&mut self, t: &Transformation) -> &mut Self {
12 let Point(x, y) = self;
13 (*x, *y) = TRANSFORM_FORMULA(*x, *y, t);
14 self
15 }
16}
17
18impl TransformableFrom for Point {
19 fn transform_from(&mut self, reference: &Self, t: &Transformation) -> &mut Self {
20 let Point(x, y) = self;
21 (*x, *y) = TRANSFORM_FORMULA(reference.0, reference.1, t);
22 self
23 }
24}
25
26const TRANSFORM_FORMULA: fn(f32, f32, &Transformation) -> (f32, f32) = |x, y, t| -> (f32, f32) {
27 let m = t.matrix();
28 let t_x = m[0][0].into_inner() * x + m[0][1].into_inner() * y + m[0][2].into_inner() * 1.0;
29 let t_y = m[1][0].into_inner() * x + m[1][1].into_inner() * y + m[1][2].into_inner() * 1.0;
30 (t_x, t_y)
31};
32
33impl Point {
34 pub fn x(&self) -> f32 {
35 self.0
36 }
37
38 pub fn y(&self) -> f32 {
39 self.1
40 }
41}
42
43impl DistanceTo<Point> for Point {
44 #[inline(always)]
45 fn distance_to(&self, other: &Point) -> f32 {
46 ((self.0 - other.0).powi(2) + (self.1 - other.1).powi(2)).sqrt()
47 }
48
49 #[inline(always)]
50 fn sq_distance_to(&self, other: &Point) -> f32 {
51 (self.0 - other.0).powi(2) + (self.1 - other.1).powi(2)
52 }
53}
54
55impl Eq for Point {}
56
57impl Hash for Point {
58 fn hash<H: Hasher>(&self, state: &mut H) {
59 let x = self.0.to_bits();
60 let y = self.1.to_bits();
61 x.hash(state);
62 y.hash(state);
63 }
64}
65
66impl From<Point> for (f32, f32) {
67 fn from(p: Point) -> Self {
68 (p.0, p.1)
69 }
70}
71
72impl From<(f32, f32)> for Point {
73 fn from((x, y): (f32, f32)) -> Self {
74 Point(x, y)
75 }
76}
77
78impl<T> CollidesWith<T> for Point
79where
80 T: CollidesWith<Point>,
81{
82 fn collides_with(&self, other: &T) -> bool {
83 other.collides_with(self)
84 }
85}