From 69591cc5483d36bc819c75dce9347b08b04e33bf Mon Sep 17 00:00:00 2001 From: brian cully Date: Sat, 27 Dec 2025 12:58:44 -0500 Subject: add rust/wasm impl --- src/point.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/point.rs (limited to 'src/point.rs') diff --git a/src/point.rs b/src/point.rs new file mode 100644 index 0000000..76f9ced --- /dev/null +++ b/src/point.rs @@ -0,0 +1,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() + } +} -- cgit v1.3