aboutsummaryrefslogtreecommitdiffstats
path: root/src/line.rs
blob: 17191ce7fa8aca0b78e6d132f011757e9d8c2fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use crate::point::Point;

pub struct Line<'a> {
    p1: &'a Point,
    p2: &'a Point,
}

impl<'a> Line<'a> {
    pub fn new(p1: &'a Point, p2: &'a Point) -> Self {
        Self { p1, p2 }
    }

    pub fn vec(&self) -> Point {
        Point::new(self.p2.x - self.p1.x, self.p2.y - self.p1.y)
    }

    pub fn angle_between(&self, other: &Self) -> f64 {
        let v1 = self.vec();
        let v2 = other.vec();
        (v1.dot(&v2) / (v1.mag() * v2.mag())).acos()
    }
}