rgeometry/data/point/
sub.rs

1use array_init::array_init;
2use std::ops::Index;
3use std::ops::Sub;
4
5use super::Point;
6use super::Vector;
7
8// Compiler bug: https://github.com/rust-lang/rust/issues/39959
9// When the bug has been fixed then we can get rid of the 'Clone' requirement.
10// point - point = vector
11impl<'a, 'b, T, const N: usize> Sub<&'a Point<T, N>> for &'b Point<T, N>
12where
13  T: Sub<T, Output = T> + Clone,
14{
15  type Output = Vector<T, N>;
16
17  fn sub(self: &'b Point<T, N>, other: &'a Point<T, N>) -> Self::Output {
18    // Vector(raw_arr_sub(&self.array, &other.array))
19    Vector(array_init(|i| {
20      self.array.index(i).clone() - other.array.index(i).clone()
21    }))
22  }
23}
24
25impl<T, const N: usize> Sub<Point<T, N>> for Point<T, N>
26where
27  T: Sub<T, Output = T> + Clone,
28{
29  type Output = Vector<T, N>;
30
31  fn sub(self: Point<T, N>, other: Point<T, N>) -> Self::Output {
32    Sub::sub(&self, &other)
33  }
34}