aboutsummaryrefslogtreecommitdiffstats
path: root/src/point.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/point.rs')
-rw-r--r--src/point.rs50
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()
+ }
+}