Skip to main content

shape_core/elements/points/point_2d/
mod.rs

1use super::*;
2mod arthmetic;
3mod constructors;
4mod euclidean;
5mod manhattan;
6
7/// A 2D points.
8#[cfg_attr(feature = "serde", repr(C), derive(Serialize, Deserialize))]
9#[derive(Copy, Clone, PartialEq, Eq, Hash)]
10pub struct Point<T> {
11    /// The x-coordinate of the point
12    pub x: T,
13    /// The y-coordinate of the point
14    pub y: T,
15}
16
17impl<T> Point<T> {
18    pub fn ref_inner(&self) -> Point<&T> {
19        Point { x: &self.x, y: &self.y }
20    }
21}
22
23impl<T> Point<T>
24where
25    T: Num + Clone,
26{
27    pub fn norm(&self) -> T
28    where
29        T: Float,
30    {
31        (self.x.clone() * self.x.clone() + self.y.clone() * self.y.clone()).sqrt()
32    }
33
34    /// ```math
35    /// \vec{PA}\times\vec{PB} = (a_x-b_x)*(p_y-b_y)-(p_x-b_x)*(a_y-b_y)
36    /// ```
37    pub fn cross_dot(&self, a: &Self, b: &Self) -> T {
38        let p = (b.x.clone() - a.x.clone()) * (self.y.clone() - b.y.clone());
39        let q = (b.y.clone() - a.y.clone()) * (self.x.clone() - b.x.clone());
40        p - q
41    }
42}