`impl Sub for Point`
+use core::ops::Sub;
+
// todo: documentation
#[derive(Copy, Clone, Default, Debug, PartialEq)]
+
pub struct Point {
pub x: f64,
pub y: f64,
@@ -16,6 +19,17 @@ impl Point {
}
}
+impl Sub for Point {
+ type Output = Self;
+
+ fn sub(self, other: Self) -> Self {
+ Self {
+ x: self.x - other.x,
+ y: self.y - other.y,
+ }
+ }
+}
+
impl From<f64> for Point {
fn from(t: f64) -> Self {
Point::new(t, t)
@@ -51,3 +65,17 @@ fn test_distance() {
assert!(((point_positive.distance(point_negative) - EXPECTED_RESULT).abs() < ERROR_MARGIN));
assert!(((point_negative.distance(point_positive) - EXPECTED_RESULT).abs() < ERROR_MARGIN));
}
+
+#[test]
+fn test_sub() {
+ const EXPECTED_RESULT: Point = Point { x: -3., y: 5. };
+ const ERROR_MARGIN: f64 = 0.00001;
+
+ let left_side = Point::new(5., 7.);
+ let right_side = Point::new(8., 2.);
+
+ let result = left_side - right_side;
+
+ assert!((result.x - EXPECTED_RESULT.x).abs() < ERROR_MARGIN);
+ assert!((result.y - EXPECTED_RESULT.y).abs() < ERROR_MARGIN);
+}
