From 2961d488078fd3e8297fa5c98232f1d998069fa7 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Wed, 26 Feb 2025 15:50:04 +0700 Subject: upd: constants --- src/calculus.rs | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'src/calculus.rs') diff --git a/src/calculus.rs b/src/calculus.rs index 677230e..40caff8 100644 --- a/src/calculus.rs +++ b/src/calculus.rs @@ -1,19 +1,15 @@ pub mod calculus { - // struct Vec3Cache { - // mag: Option, - // mag_sql: Option, - // unit: Option, - // } - pub struct Vec3 { - pub x: f64, - pub y: f64, - pub z: f64, + pub x: f32, + pub y: f32, + pub z: f32, // cached: Vec3Cache, } + pub type Point3 = Vec3; + impl Vec3 { - pub fn new(x: f64, y: f64, z: f64) -> Self { + pub fn new(x: f32, y: f32, z: f32) -> Self { Self { x, y, z } } @@ -33,21 +29,23 @@ pub mod calculus { } } - pub fn scalar_mul(&mut self, multiplier: f64) { - self.x *= multiplier; - self.y *= multiplier; - self.z *= multiplier; + pub fn scalar_mul(&self, multiplier: f32) -> Self { + Self { + x: self.x * multiplier, + y: self.y * multiplier, + z: self.z * multiplier, + } } - pub fn mag_sqr(&self) -> f64 { + pub fn mag_sqr(&self) -> f32 { self.x * self.x + self.y * self.y + self.z * self.z } - pub fn mag(&self) -> f64 { + pub fn mag(&self) -> f32 { self.mag_sqr().sqrt() } - pub fn dot_prod(&self, other: &Self) -> f64 { + pub fn dot_prod(&self, other: &Self) -> f32 { self.x * other.x + self.y * other.y + self.z * other.z } @@ -68,4 +66,16 @@ pub mod calculus { } } } + + pub struct Ray { + pub origin: Point3, + pub direction: Vec3, + } + + impl Ray { + pub fn at(&self, t: f32) -> Point3 { + // Get parametric location + self.origin.add(&self.direction.scalar_mul(t)) + } + } } \ No newline at end of file -- cgit v1.2.3-70-g09d2