use crate::state::{MAX_SPEED, TAU}; const POINT_RADIUS: f64 = 0.01; fn rand_color() -> String { let [r, g, b] = [ fastrand::u8(0..255), fastrand::u8(0..255), fastrand::u8(0..255), ]; format!("rgb({r} {g} {b})",) } #[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() } }