diff options
| author | brian cully <bjc@spork.org> | 2025-12-27 12:58:44 -0500 |
|---|---|---|
| committer | brian cully <bjc@spork.org> | 2025-12-27 12:58:44 -0500 |
| commit | 69591cc5483d36bc819c75dce9347b08b04e33bf (patch) | |
| tree | 93d0f158621c6caea2f54e449a942f45ec829395 /src/point.rs | |
| parent | 0eaa19448a85473e85d4679faa4ab30108dbf4b5 (diff) | |
| download | polyring-69591cc5483d36bc819c75dce9347b08b04e33bf.tar.gz polyring-69591cc5483d36bc819c75dce9347b08b04e33bf.zip | |
add rust/wasm impl
Diffstat (limited to 'src/point.rs')
| -rw-r--r-- | src/point.rs | 50 |
1 files changed, 50 insertions, 0 deletions
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() + } +} |
