summaryrefslogtreecommitdiff
path: root/src/point.rs
blob: d7bd577c6b8e4ea843d39a6ab41024a7d36142b9 (plain) (blame)
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
use crate::*;
use std::fmt;
use std::ops::{Add, Sub, AddAssign, SubAssign};

#[derive(Copy, Clone, PartialEq)]
pub struct Point<O: Internal> {
    pub x: O,
    pub y: O,
}

pub trait HasPosition<O: Internal> {
    fn position(&self) -> Point<O>;

    fn x(&self) -> O {
        self.position().x
    }
    fn y(&self) -> O {
        self.position().y
    }
}

impl<O: Internal> Point<O> {
    pub const ZERO: Self = Self { x: O::ZERO, y: O::ZERO };

    pub const fn new(x: O, y: O) -> Point<O> {
        Point { x, y }
    }

    pub fn is_zero(&self) -> bool {
        self.x == O::ZERO && self.y == O::ZERO
    }
}

impl<O: Internal> HasPosition<O> for Point<O> {
    fn position(&self) -> Point<O> {
        *self
    }
}

impl<O: Internal> Add<Point<O>> for Point<O> {
    type Output = Self;
    fn add(self, point: Point<O>) -> Self {
        Point::new(self.x + point.x, self.y + point.y)
    }
}

impl<O: Internal> Sub<Point<O>> for Point<O> {
    type Output = Self;
    fn sub(self, point: Point<O>) -> Self {
        Point::new(self.x - point.x, self.y - point.y)
    }
}

impl<O: Internal> AddAssign<Point<O>> for Point<O> {
    fn add_assign(&mut self, point: Point<O>) {
        self.x += point.x;
        self.y += point.y;
    }
}

impl<O: Internal> SubAssign<Point<O>> for Point<O> {
    fn sub_assign(&mut self, point: Point<O>) {
        self.x -= point.x;
        self.y -= point.y;
    }
}

impl<T: Internal> fmt::Debug for Point<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Point {{ x:{:?} y:{:?} }}", self.x, self.y)
    }
}