rsmath/linspace/
point.rs

1use num::Num;
2use num::pow;
3use std::fmt;
4use std::fmt::Display;
5
6/// Point of 3 dimensions with a defined coordinates
7///
8/// # Remarks
9///
10/// This struct is implemented to be used with numerical types, not tested
11/// for strings, bools, or other types.
12#[derive(Clone, Copy)]
13pub struct Point3D<N: Copy> {
14    x: N,
15    y: N,
16    z: N,
17}
18
19////////////////////////////////////////////////////////////////////////////////
20// Inherent methods
21////////////////////////////////////////////////////////////////////////////////
22
23impl<N: Copy + Num> Point3D<N> {
24
25    /// Returns the `x` coordinate
26    #[inline]
27    pub fn x(&self) -> N {
28        self.x
29    }
30
31    /// Returns the `y` coordinate
32    #[inline]
33    pub fn y(&self) -> N {
34        self.y
35    }
36
37    /// Returns the `z` coordinate
38    #[inline]
39    pub fn z(&self) -> N {
40        self.z
41    }
42
43    /// Initializes a Point3D with default coordinates' values
44    #[inline]
45    pub fn new() -> Point3D<N> where N: Default {
46        Point3D {x: N::default(), y: N::default(), z: N::default()}
47    }
48
49    /// Initializes a Point3D with specified coordinates' values
50    ///
51    /// # Arguments
52    ///
53    /// * `x`: X coordinate
54    /// * `y`: Y coordinate
55    /// * `z`: Z coordinate
56    #[inline]
57    pub fn init(x: N, y: N, z: N) -> Point3D<N> {
58        Point3D {x: x, y: y, z: z}
59    }
60
61    /// Modifies the `X` coordinate
62    ///
63    /// # Arguments
64    ///
65    /// * `new_x`: new X value
66    #[inline]
67    pub fn set_x(&mut self, new_x: N) {
68        self.x = new_x;
69    }
70
71    /// Modifies the `Y` coordinate
72    ///
73    /// # Arguments
74    ///
75    /// * `new_y`: new Y value
76    #[inline]
77    pub fn set_y(&mut self, new_y: N) {
78        self.y = new_y;
79    }
80
81    // Modifies the `Z` coordinate
82    ///
83    /// # Arguments
84    ///
85    /// * `new_z`: new Z value
86    #[inline]
87    pub fn set_z(&mut self, new_z: N) {
88        self.z = new_z;
89    }
90
91    /// Modifies all coordinates
92    ///
93    /// # Arguments
94    ///
95    /// * `new_x`: new X value
96    /// * `new_y`: new Y value
97    /// * `new_z`: new Z value
98    #[inline]
99    pub fn set(&mut self, new_x: N, new_y: N, new_z: N) {
100        self.x = new_x;
101        self.y = new_y;
102        self.z = new_z;
103    }
104
105    /// Returns the Euclidean Distance between two points
106    ///
107    /// # Arguments
108    ///
109    /// * `a`: first point
110    /// * `b`: second point
111    #[inline]
112    pub fn eucl_distance(a: &Point3D<N>, b: &Point3D<N>) -> f64 where N: Into<f64> {
113        let val: f64 = (pow((a.x() - b.x()), 2) +
114                        pow((a.y() - b.y()), 2) +
115                        pow((a.z() - b.z()), 2)).into();
116        val.sqrt()
117    }
118}
119
120////////////////////////////////////////////////////////////////////////////////
121// Traits
122////////////////////////////////////////////////////////////////////////////////
123
124impl<N: Copy + PartialEq> PartialEq for Point3D<N> {
125    fn eq(&self, other: &Point3D<N>) -> bool {
126        if (self.x == other.x) && (self.y == other.y) && (self.z == other.z) {
127            return true;
128        }
129        false
130    }
131}
132
133/// Display implementation for Point3D
134impl<N: Copy + Num> fmt::Display for Point3D<N> where N: Display {
135    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136        write!(f, "[ {x}, {y}, {z} ]", x = self.x, y = self.y, z = self.z)
137    }
138}