aboutsummaryrefslogtreecommitdiffstats
path: root/src/point.rs
blob: 76f9cedf012c3bdbd97185a0aae82fbe6f3bfa96 (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
use crate::state::{MAX_SPEED, TAU};

const POINT_RADIUS: f64 = 0.01;

fn rand_color() -> String {
    format!("rgb({} {} {})", fastrand::u8(0..255), fastrand::u8(0..255), fastrand::u8(0..255))
}

#[derive(Clone, Debug)]
pub struct Point {
    pub x: f64,
    pub y: f64,
    pub radius: f64,
    pub heading: f64,
    pub speed: f64,
    pub color: String,
}

impl Point {
    pub fn new(x: f64, y: f64) -> Self {
        Self {
            x,
            y,
            radius: POINT_RADIUS,
            heading: fastrand::f64() * TAU,
            speed: fastrand::f64() * MAX_SPEED,
            color: rand_color(),
        }
    }

    pub fn equal_pos(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }

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

    pub fn mag(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }

    pub fn speed_x(&self) -> f64 {
        self.speed * self.heading.cos()
    }

    pub fn speed_y(&self) -> f64 {
        self.speed * self.heading.sin()
    }
}