summaryrefslogtreecommitdiff
path: root/src/calculus.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/calculus.rs')
-rw-r--r--src/calculus.rs44
1 files changed, 27 insertions, 17 deletions
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<f64>,
- // mag_sql: Option<f64>,
- // unit: Option<Vec3>,
- // }
-
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