1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use crate::Vector;
use serde::{Deserialize, Serialize};
use std::ops::{Add, Sub};

/// A point in space
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone, Default, Serialize, Deserialize)]
pub struct Point {
    /// The x coordinate of the Point
    pub x: f64,
    /// The y coordinate of the Point
    pub y: f64,
}

impl From<(f64, f64)> for Point {
    fn from((x, y): (f64, f64)) -> Self {
        Self { x, y }
    }
}

impl Add for Point {
    type Output = Point;

    fn add(self, other: Self::Output) -> Self::Output {
        Point {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

impl Sub for Point {
    type Output = Point;

    fn sub(self, other: Self::Output) -> Self::Output {
        Point {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

impl From<Vector> for Point {
    fn from(vector: Vector) -> Self {
        Self {
            x: vector.x,
            y: vector.y,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(clippy::eq_op)]
    fn is_equal_to_itself() {
        let point = Point { x: -12.9, y: 45.1 };
        assert_eq!(point, point);
    }

    #[test]
    #[allow(clippy::eq_op)]
    fn is_equal_to_itself_when_zero() {
        let point = Point { x: 0.0, y: 0.0 };
        assert_eq!(point, point);
    }

    #[test]
    fn is_no_equal_to_other_point() {
        let point = Point { x: 12.3, y: 89.0 };
        let different_point = Point { x: 12.4, y: 89.0 };
        assert!(point != different_point);
    }

    #[test]
    fn adds_zero_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_add = Point { x: 0.0, y: 0.0 };
        let expected_point = original_point;
        let added_point = original_point + point_to_add;
        assert_eq!(expected_point, added_point);
    }

    #[test]
    fn adds_other_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_add = Point { x: 3.0, y: 1.0 };
        let expected_point = Point { x: 15.0, y: 44.0 };
        let added_point = original_point + point_to_add;
        assert_eq!(expected_point, added_point);
    }

    #[test]
    fn adds_negative_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_add = Point { x: -10.0, y: -20.0 };
        let expected_point = Point { x: 2.0, y: 23.0 };
        let added_point = original_point + point_to_add;
        assert_eq!(expected_point, added_point);
    }

    #[test]
    fn adds_to_zero_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_add = Point { x: -12.0, y: -43.0 };
        let expected_point = Point { x: 0.0, y: 0.0 };
        let added_point = original_point + point_to_add;
        assert_eq!(expected_point, added_point);
    }

    #[test]
    fn adds_when_negative() {
        let original_point = Point { x: -12.0, y: -43.0 };
        let point_to_add = Point { x: -4.0, y: -2.0 };
        let expected_point = Point { x: -16.0, y: -45.0 };
        let added_point = original_point + point_to_add;
        assert_eq!(expected_point, added_point);
    }

    #[test]
    fn subtracts_zero_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_subtract = Point { x: 0.0, y: 0.0 };
        let expected_point = original_point;
        let substracted_point = original_point - point_to_subtract;
        assert_eq!(expected_point, substracted_point);
    }

    #[test]
    fn subtracts_other_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_subtract = Point { x: 3.0, y: 1.0 };
        let expected_point = Point { x: 9.0, y: 42.0 };
        let substracted_point = original_point - point_to_subtract;
        assert_eq!(expected_point, substracted_point);
    }

    #[test]
    fn subtracts_negative_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_subtract = Point { x: -10.0, y: -20.0 };
        let expected_point = Point { x: 22.0, y: 63.0 };
        let substracted_point = original_point - point_to_subtract;
        assert_eq!(expected_point, substracted_point);
    }

    #[test]
    fn subtracts_to_zero_point() {
        let original_point = Point { x: 12.0, y: 43.0 };
        let point_to_subtract = original_point;
        let expected_point = Point { x: 0.0, y: 0.0 };
        let point_to_subtract = original_point - point_to_subtract;
        assert_eq!(expected_point, point_to_subtract);
    }

    #[test]
    fn subtracts_when_negative() {
        let original_point = Point { x: -12.0, y: -43.0 };
        let point_to_subtract = Point { x: -4.0, y: -2.0 };
        let expected_point = Point { x: -8.0, y: -41.0 };
        let substracted_point = original_point - point_to_subtract;
        assert_eq!(expected_point, substracted_point);
    }
}