summaryrefslogtreecommitdiff
path: root/src/geometry.rs
blob: c49fe2a022269037a19cdd6eb4f72f906c8863ef (plain)
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
use std::ops::*;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vec2d {
    pub x: f64,
    pub y: f64
}

impl Vec2d {
    pub fn distance(&self, other: Vec2d) -> f64 {
        self.distance_squared(other).sqrt()
    }
    pub fn distance_squared(&self, other: Vec2d) -> f64 {
        ((other.x-self.x).powi(2) + (other.y-self.y).powi(2))
    }

    pub fn angle(&self) -> f64 {
        self.y.atan2(self.x)
    }
}

impl Add for Vec2d {
    type Output = Vec2d;

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

impl Sub for Vec2d {
    type Output = Vec2d;

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

impl Neg for Vec2d {
    type Output = Vec2d;

    fn neg(self) -> Self {
        Vec2d {
            x: -self.x,
            y: -self.y
        }
    }
}