Made methods pub so I could use them

This commit is contained in:
Jon Janzen
2021-02-07 14:48:45 -07:00
parent 4b0515f4b1
commit c863cf5a8b

View File

@@ -1,10 +1,11 @@
#[macro_use]
extern crate approx;
use std::fmt;
use std::ops;
#[derive(Debug)]
struct Tuple {
#[derive(Debug, Copy, Clone)]
pub struct Tuple {
x: f32,
y: f32,
z: f32,
@@ -12,7 +13,7 @@ struct Tuple {
}
impl Tuple {
fn new(x: f32, y: f32, z: f32, w: f32) -> Tuple {
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Tuple {
Tuple {
x,
y,
@@ -22,7 +23,7 @@ impl Tuple {
}
fn point(x: f32, y: f32, z: f32) -> Tuple {
pub fn point(x: f32, y: f32, z: f32) -> Tuple {
Tuple {
x,
y,
@@ -31,7 +32,7 @@ impl Tuple {
}
}
fn vector(x: f32, y: f32, z: f32) -> Tuple {
pub fn vector(x: f32, y: f32, z: f32) -> Tuple {
Tuple {
x,
y,
@@ -42,27 +43,27 @@ impl Tuple {
}
impl Tuple {
fn x(&self) -> f32 {
pub fn x(&self) -> f32 {
self.x
}
fn y(&self) -> f32 {
pub fn y(&self) -> f32 {
self.y
}
fn z(&self) -> f32 {
pub fn z(&self) -> f32 {
self.z
}
fn is_point(&self) -> bool {
pub fn is_point(&self) -> bool {
self.w == 1.0
}
fn is_vector(&self) -> bool {
pub fn is_vector(&self) -> bool {
self.w == 0.0
}
fn magnitude(&self) -> f32 {
pub fn magnitude(&self) -> f32 {
(
(self.x * self.x) +
(self.y * self.y) +
@@ -71,7 +72,7 @@ impl Tuple {
).sqrt()
}
fn normalize(&self) -> Tuple {
pub fn normalize(&self) -> Tuple {
let m = self.magnitude();
Tuple::new(
self.x / m,
@@ -81,14 +82,14 @@ impl Tuple {
)
}
fn dot(&self, rhs: &Tuple) -> f32 {
pub fn dot(&self, rhs: &Tuple) -> f32 {
self.x * rhs.x +
self.y * rhs.y +
self.z * rhs.z +
self.w * rhs.w
}
fn cross(&self, rhs: &Tuple) -> Tuple {
pub fn cross(&self, rhs: &Tuple) -> Tuple {
Tuple::vector(
self.y * rhs.z - self.z * rhs.y,
self.z * rhs.x - self.x * rhs.z,
@@ -97,6 +98,12 @@ impl Tuple {
}
}
impl fmt::Display for Tuple {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {}) {}", self.x, self.y, self.z, if self.is_point() { "p" } else { "v" } )
}
}
impl PartialEq for Tuple {
fn eq(&self, _rhs: &Self) -> bool {
relative_eq!(self.x, _rhs.x)
@@ -184,6 +191,7 @@ impl ops::Div<f32> for Tuple {
}
}
#[cfg(test)]
mod tests {
use super::*;