lib & tuples: use crate specific EPSILON definition.
This commit is contained in:
parent
83799a02a9
commit
4b0d882b84
@ -1,3 +1,6 @@
|
|||||||
pub mod canvas;
|
pub mod canvas;
|
||||||
pub mod matrices;
|
pub mod matrices;
|
||||||
pub mod tuples;
|
pub mod tuples;
|
||||||
|
|
||||||
|
/// Value considered close enough for PartialEq implementations.
|
||||||
|
pub const EPSILON: f32 = 0.00001;
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
use std::ops::{Add, Div, Mul, Neg, Sub};
|
use std::ops::{Add, Div, Mul, Neg, Sub};
|
||||||
|
|
||||||
|
use crate::EPSILON;
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct Tuple {
|
pub struct Tuple {
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
@ -116,10 +118,10 @@ impl Sub for Tuple {
|
|||||||
|
|
||||||
impl PartialEq for Tuple {
|
impl PartialEq for Tuple {
|
||||||
fn eq(&self, rhs: &Tuple) -> bool {
|
fn eq(&self, rhs: &Tuple) -> bool {
|
||||||
((self.x - rhs.x).abs() < f32::EPSILON)
|
((self.x - rhs.x).abs() < EPSILON)
|
||||||
&& ((self.y - rhs.y).abs() < f32::EPSILON)
|
&& ((self.y - rhs.y).abs() < EPSILON)
|
||||||
&& ((self.z - rhs.z).abs() < f32::EPSILON)
|
&& ((self.z - rhs.z).abs() < EPSILON)
|
||||||
&& ((self.w - rhs.w).abs() < f32::EPSILON)
|
&& ((self.w - rhs.w).abs() < EPSILON)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn dot(a: Tuple, b: Tuple) -> f32 {
|
pub fn dot(a: Tuple, b: Tuple) -> f32 {
|
||||||
@ -221,7 +223,7 @@ impl Sub for Color {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{cross, dot, Color, Tuple};
|
use super::{cross, dot, Color, Tuple, EPSILON};
|
||||||
#[test]
|
#[test]
|
||||||
fn is_point() {
|
fn is_point() {
|
||||||
// A tuple with w = 1 is a point
|
// A tuple with w = 1 is a point
|
||||||
@ -326,7 +328,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn vector_normalize_magnitude() {
|
fn vector_normalize_magnitude() {
|
||||||
let len = Tuple::vector(1., 2., 3.).normalize().magnitude();
|
let len = Tuple::vector(1., 2., 3.).normalize().magnitude();
|
||||||
assert!((1. - len).abs() < f32::EPSILON);
|
assert!((1. - len).abs() < EPSILON);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn dot_two_tuples() {
|
fn dot_two_tuples() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user