Made methods pub so I could use them
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate approx;
|
extern crate approx;
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
use std::ops;
|
use std::ops;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
struct Tuple {
|
pub struct Tuple {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
z: f32,
|
z: f32,
|
||||||
@@ -12,7 +13,7 @@ struct Tuple {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Tuple {
|
||||||
x,
|
x,
|
||||||
y,
|
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 {
|
Tuple {
|
||||||
x,
|
x,
|
||||||
y,
|
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 {
|
Tuple {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
@@ -42,27 +43,27 @@ impl Tuple {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Tuple {
|
impl Tuple {
|
||||||
fn x(&self) -> f32 {
|
pub fn x(&self) -> f32 {
|
||||||
self.x
|
self.x
|
||||||
}
|
}
|
||||||
|
|
||||||
fn y(&self) -> f32 {
|
pub fn y(&self) -> f32 {
|
||||||
self.y
|
self.y
|
||||||
}
|
}
|
||||||
|
|
||||||
fn z(&self) -> f32 {
|
pub fn z(&self) -> f32 {
|
||||||
self.z
|
self.z
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_point(&self) -> bool {
|
pub fn is_point(&self) -> bool {
|
||||||
self.w == 1.0
|
self.w == 1.0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_vector(&self) -> bool {
|
pub fn is_vector(&self) -> bool {
|
||||||
self.w == 0.0
|
self.w == 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn magnitude(&self) -> f32 {
|
pub fn magnitude(&self) -> f32 {
|
||||||
(
|
(
|
||||||
(self.x * self.x) +
|
(self.x * self.x) +
|
||||||
(self.y * self.y) +
|
(self.y * self.y) +
|
||||||
@@ -71,7 +72,7 @@ impl Tuple {
|
|||||||
).sqrt()
|
).sqrt()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn normalize(&self) -> Tuple {
|
pub fn normalize(&self) -> Tuple {
|
||||||
let m = self.magnitude();
|
let m = self.magnitude();
|
||||||
Tuple::new(
|
Tuple::new(
|
||||||
self.x / m,
|
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.x * rhs.x +
|
||||||
self.y * rhs.y +
|
self.y * rhs.y +
|
||||||
self.z * rhs.z +
|
self.z * rhs.z +
|
||||||
self.w * rhs.w
|
self.w * rhs.w
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cross(&self, rhs: &Tuple) -> Tuple {
|
pub fn cross(&self, rhs: &Tuple) -> Tuple {
|
||||||
Tuple::vector(
|
Tuple::vector(
|
||||||
self.y * rhs.z - self.z * rhs.y,
|
self.y * rhs.z - self.z * rhs.y,
|
||||||
self.z * rhs.x - self.x * rhs.z,
|
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 {
|
impl PartialEq for Tuple {
|
||||||
fn eq(&self, _rhs: &Self) -> bool {
|
fn eq(&self, _rhs: &Self) -> bool {
|
||||||
relative_eq!(self.x, _rhs.x)
|
relative_eq!(self.x, _rhs.x)
|
||||||
@@ -184,6 +191,7 @@ impl ops::Div<f32> for Tuple {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user