diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 14:34:33 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 14:34:33 +0700 |
commit | 20be72c501397a45e3c8f4bc95a3451f103e3a28 (patch) | |
tree | 01d947b85f13f5913592b16570591cf6b731f8d6 | |
parent | 5bf01d83ebe89059dbd73554100efde6fb112727 (diff) |
upd: vector calculus module
-rw-r--r-- | src/calculus.rs | 71 | ||||
-rw-r--r-- | src/main.rs | 1 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/calculus.rs b/src/calculus.rs new file mode 100644 index 0000000..677230e --- /dev/null +++ b/src/calculus.rs @@ -0,0 +1,71 @@ +pub mod calculus { + // struct Vec3Cache { + // mag: Option<f64>, + // mag_sql: Option<f64>, + // unit: Option<Vec3>, + // } + + pub struct Vec3 { + pub x: f64, + pub y: f64, + pub z: f64, + // cached: Vec3Cache, + } + + impl Vec3 { + pub fn new(x: f64, y: f64, z: f64) -> Self { + Self { x, y, z } + } + + pub fn add(&self, other: &Self) -> Self { + Self { + x: self.x + other.x, + y: self.y + other.y, + z: self.z + other.z + } + } + + pub fn sub(&self, other: &Self) -> Self { + Self { + x: self.x - other.x, + y: self.y - other.y, + z: self.z - other.z + } + } + + pub fn scalar_mul(&mut self, multiplier: f64) { + self.x *= multiplier; + self.y *= multiplier; + self.z *= multiplier; + } + + pub fn mag_sqr(&self) -> f64 { + self.x * self.x + self.y * self.y + self.z * self.z + } + + pub fn mag(&self) -> f64 { + self.mag_sqr().sqrt() + } + + pub fn dot_prod(&self, other: &Self) -> f64 { + self.x * other.x + self.y * other.y + self.z * other.z + } + + pub fn cross_prod(&self, other: &Self) -> Self { + Self { + x: self.y * other.z - self.z - other.y, + y: self.z * other.x - self.x * other.z, + z: self.x * other.y - self.y * other.x, + } + } + + pub fn unit(&self) -> Self { + let mag = self.mag(); + Self { + x: self.x / mag, + y: self.y / mag, + z: self.z / mag, + } + } + } +}
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 05b4273..2c81847 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod view; mod global; +mod calculus; use crate::global::*; use crate::view::{render_viewer, View}; |