diff --git a/src/geometry.rs b/src/geometry.rs new file mode 100644 index 0000000..b059a78 --- /dev/null +++ b/src/geometry.rs @@ -0,0 +1,50 @@ +use crate::math::{Point3, Vector3}; + +pub struct Plane { + point: Point3, + normal: Vector3, +} + +impl Plane { + pub fn new(point: Point3, normal: Vector3) -> Plane { + Plane { point, normal } + } +} + +pub struct Line { + point: Point3, + direction: Vector3, +} + +pub struct Sphere { + point: Point3, + radius: f64, +} + +impl Sphere { + pub fn new(point: Point3, radius: f64) -> Sphere { + Sphere { point, radius } + } +} + +pub enum Geometry { + Sphere(Sphere), + Plane(Plane), + Line(Line), +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_plane() { + let p = Point3::new(1.0, 2.0, 3.0); + let n = Vector3::new(4.0, 5.0, 6.0); + + let plane = Plane::new(p, n); + + assert_eq!(plane.point, p); + assert_eq!(plane.normal, n); + } +} diff --git a/src/math.rs b/src/math.rs new file mode 100644 index 0000000..dd493d5 --- /dev/null +++ b/src/math.rs @@ -0,0 +1,124 @@ +use std::ops::{Add, Div, Mul, Sub}; + +#[derive(Copy, Clone, PartialEq, Debug)] +pub struct Vector3 { + x: f64, + y: f64, + z: f64, +} + +impl Vector3 { + pub fn new(x: f64, y: f64, z: f64) -> Vector3 { + Vector3 { x, y, z } + } +} + +impl Add for Vector3 { + type Output = Vector3; + + fn add(self, rhs: Vector3) -> Self::Output { + Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +impl Sub for Vector3 { + type Output = Vector3; + + fn sub(self, rhs: Vector3) -> Self::Output { + Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + +impl Mul for Vector3 { + type Output = Vector3; + + fn mul(self, rhs: f64) -> Self::Output { + Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs) + } +} + +impl Mul for f64 { + type Output = Vector3; + + fn mul(self, rhs: Vector3) -> Self::Output { + Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z) + } +} + +impl Mul for Vector3 { + type Output = f64; + + fn mul(self, rhs: Vector3) -> Self::Output { + self.x * rhs.x + self.y * rhs.y + self.z * rhs.z + } +} + +impl Div for Vector3 { + type Output = Vector3; + + fn div(self, rhs: f64) -> Self::Output { + Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs) + } +} + +#[derive(Copy, Clone, PartialEq, Debug)] +pub struct Point3 { + x: f64, + y: f64, + z: f64, +} + +impl Point3 { + pub fn new(x: f64, y: f64, z: f64) -> Point3 { + Point3 { x, y, z } + } +} + +impl Add for Point3 { + type Output = Point3; + + fn add(self, rhs: Vector3) -> Self::Output { + Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +impl Sub for Point3 { + type Output = Vector3; + + fn sub(self, rhs: Point3) -> Self::Output { + Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_vector3() { + let v = Vector3::new(1.0, 2.0, 3.0); + assert_eq!(v.x, 1.0); + assert_eq!(v.y, 2.0); + assert_eq!(v.z, 3.0); + } + + #[test] + fn add_vector3_to_vector3() { + let v1 = Vector3::new(1.0, 2.0, 3.0); + let v2 = Vector3::new(4.0, 5.0, 6.0); + + let r = v1 + v2; + + assert_eq!(r.x, 5.0); + assert_eq!(r.y, 7.0); + assert_eq!(r.z, 9.0); + } + + #[test] + fn new_point3() { + let p = Point3::new(1.0, 2.0, 3.0); + assert_eq!(p.x, 1.0); + assert_eq!(p.y, 2.0); + assert_eq!(p.z, 3.0); + } +}