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

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

impl Vec2d {
    pub fn new(x: f64, y: f64) -> Vec2d {
        Vec2d {x, y}
    }
    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 magnitude(&self) -> f64 {
        self.magnitude_squared().sqrt()
    }
    pub fn magnitude_squared(&self) -> f64 {
        self.x.powi(2) + self.y.powi(2)
    }

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

    pub fn unit(&self) -> Vec2d {
        let mag = self.magnitude();
        Vec2d {
            x: self.x / mag,
            y: self.y / mag
        }
    }

    pub fn dot(&self, other: Vec2d) -> f64 {
        (self.x * other.x) + (self.y * other.y)
    }
}

pub struct LineSegment {
    pub start: Vec2d,
    pub end: Vec2d
}

impl LineSegment {
    pub fn distance(&self, other: Vec2d) -> f64 {
        let length_squared = self.start.distance_squared(self.end);
        if length_squared == 0.0 {
            return self.start.distance(other);
        }

        let t = f64::max(0., f64::min(1., (other - self.start).dot(self.end - self.start) / length_squared));
        let projection = self.start + (self.end - self.start) * t;
        other.distance(projection)
    }
    
}

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
        }
    }
}

impl Mul<f64> for Vec2d {
    type Output = Vec2d;

    fn mul(self, rhs: f64) -> Self {
        Vec2d {
            x: self.x * rhs,
            y: self.y * rhs
        }
    }
}