Use WHITE and BLACK constants where appropriate

This commit is contained in:
Bill Thiede 2021-07-17 16:41:22 -07:00
parent b37398ac40
commit 9f00485256
6 changed files with 26 additions and 25 deletions

View File

@ -7,6 +7,7 @@ use rtchallenge::{
rays::Ray,
spheres::{intersect, Sphere},
tuples::{Color, Tuple},
WHITE,
};
fn main() -> Result<()> {
@ -28,7 +29,7 @@ fn main() -> Result<()> {
..Material::default()
};
let light_position = Tuple::point(-10., 10., -10.);
let light_color = Color::new(1., 1., 1.);
let light_color = WHITE;
let light = PointLight::new(light_position, light_color);
for y in 0..h {

View File

@ -74,17 +74,16 @@ impl Canvas {
mod tests {
use super::Canvas;
use crate::tuples::Color;
use crate::{tuples::Color, BLACK};
#[test]
fn create_canvas() {
let bg = Color::new(0.0, 0.0, 0.0);
let bg = BLACK;
let c = Canvas::new(10, 20, bg);
assert_eq!(c.width, 10);
assert_eq!(c.height, 20);
let black = Color::new(0., 0., 0.);
for (i, p) in c.pixels.iter().enumerate() {
assert_eq!(p, &black, "pixel {} not {:?}: {:?}", i, &black, p);
assert_eq!(p, &BLACK, "pixel {} not {:?}: {:?}", i, &BLACK, p);
}
}

View File

@ -11,5 +11,5 @@ pub mod world;
/// Value considered close enough for PartialEq implementations.
pub const EPSILON: f32 = 0.00001;
const BLACK: tuples::Color = tuples::Color::new(0., 0., 0.);
const WHITE: tuples::Color = tuples::Color::new(1., 1., 1.);
pub const BLACK: tuples::Color = tuples::Color::new(0., 0., 0.);
pub const WHITE: tuples::Color = tuples::Color::new(1., 1., 1.);

View File

@ -15,9 +15,10 @@ impl PointLight {
/// use rtchallenge::{
/// lights::PointLight,
/// tuples::{Color, Tuple},
/// WHITE,
/// };
///
/// let intensity = Color::new(1., 1., 1.);
/// let intensity = WHITE;
/// let position = Tuple::point(0., 0., 0.);
/// let light = PointLight::new(position, intensity);
/// assert_eq!(light.position, position);

View File

@ -2,7 +2,7 @@ use crate::{
lights::PointLight,
tuples::Color,
tuples::{dot, reflect, Tuple},
BLACK,
BLACK, WHITE,
};
#[derive(Debug, PartialEq, Clone)]
pub struct Material {
@ -18,13 +18,13 @@ impl Default for Material {
///
/// # Examples
/// ```
/// use rtchallenge::{materials::Material, tuples::Color};
/// use rtchallenge::{materials::Material, tuples::Color, WHITE};
///
/// let m = Material::default();
/// assert_eq!(
/// m,
/// Material {
/// color: Color::new(1., 1., 1.),
/// color: WHITE,
/// ambient: 0.1,
/// diffuse: 0.9,
/// specular: 0.9,
@ -34,7 +34,7 @@ impl Default for Material {
/// ```
fn default() -> Material {
Material {
color: Color::new(1., 1., 1.),
color: WHITE,
ambient: 0.1,
diffuse: 0.9,
specular: 0.9,
@ -51,6 +51,7 @@ impl Default for Material {
/// lights::PointLight,
/// materials::{lighting, Material},
/// tuples::{Color, Tuple},
/// WHITE,
/// };
///
/// let m = Material::default();
@ -59,35 +60,35 @@ impl Default for Material {
/// // Lighting with the eye between the light and the surface.
/// let eyev = Tuple::vector(0., 0., -1.);
/// let normalv = Tuple::vector(0., 0., -1.);
/// let light = PointLight::new(Tuple::point(0., 0., -10.), Color::new(1., 1., 1.));
/// let light = PointLight::new(Tuple::point(0., 0., -10.), WHITE);
/// let result = lighting(&m, &light, position, eyev, normalv);
/// assert_eq!(result, Color::new(1.9, 1.9, 1.9));
///
/// // Lighting with the eye between the light and the surface, eye offset 45°.
/// let eyev = Tuple::vector(0., 2_f32.sqrt() / 2., -2_f32.sqrt() / 2.);
/// let normalv = Tuple::vector(0., 0., -1.);
/// let light = PointLight::new(Tuple::point(0., 0., -10.), Color::new(1., 1., 1.));
/// let light = PointLight::new(Tuple::point(0., 0., -10.), WHITE);
/// let result = lighting(&m, &light, position, eyev, normalv);
/// assert_eq!(result, Color::new(1.0, 1.0, 1.0));
/// assert_eq!(result, WHITE);
///
/// // Lighting with the eye opposite surface, light offset 45°.
/// let eyev = Tuple::vector(0., 0., -1.);
/// let normalv = Tuple::vector(0., 0., -1.);
/// let light = PointLight::new(Tuple::point(0., 10., -10.), Color::new(1., 1., 1.));
/// let light = PointLight::new(Tuple::point(0., 10., -10.), WHITE);
/// let result = lighting(&m, &light, position, eyev, normalv);
/// assert_eq!(result, Color::new(0.7364, 0.7364, 0.7364));
///
/// // Lighting with the eye in the path of the reflection vector.
/// let eyev = Tuple::vector(0., -2_f32.sqrt() / 2., -2_f32.sqrt() / 2.);
/// let normalv = Tuple::vector(0., 0., -1.);
/// let light = PointLight::new(Tuple::point(0., 10., -10.), Color::new(1., 1., 1.));
/// let light = PointLight::new(Tuple::point(0., 10., -10.), WHITE);
/// let result = lighting(&m, &light, position, eyev, normalv);
/// assert_eq!(result, Color::new(1.6363853, 1.6363853, 1.6363853));
///
/// // Lighting with the light behind the surface.
/// let eyev = Tuple::vector(0., 0., -1.);
/// let normalv = Tuple::vector(0., 0., -1.);
/// let light = PointLight::new(Tuple::point(0., 0., 10.), Color::new(1., 1., 1.));
/// let light = PointLight::new(Tuple::point(0., 0., 10.), WHITE);
/// let result = lighting(&m, &light, position, eyev, normalv);
/// assert_eq!(result, Color::new(0.1, 0.1, 0.1));
/// ```

View File

@ -6,7 +6,7 @@ use crate::{
rays::Ray,
spheres::{intersect, Sphere},
tuples::{Color, Tuple},
BLACK,
BLACK, WHITE,
};
/// World holds all drawable objects and the light(s) that illuminate them.
@ -39,7 +39,7 @@ impl World {
/// assert!(w.light.is_some());
/// ```
pub fn test_world() -> World {
let light = PointLight::new(Tuple::point(-10., 10., -10.), Color::new(1., 1., 1.));
let light = PointLight::new(Tuple::point(-10., 10., -10.), WHITE);
let mut s1 = Sphere::default();
s1.material = Material {
color: Color::new(0.8, 1., 0.6),
@ -94,6 +94,7 @@ impl World {
/// rays::Ray,
/// tuples::{Color, Tuple},
/// world::World,
/// WHITE,
/// };
///
/// // Shading an intersection.
@ -107,10 +108,7 @@ impl World {
///
/// // Shading an intersection from the inside.
/// let mut w = World::test_world();
/// w.light = Some(PointLight::new(
/// Tuple::point(0., 0.25, 0.),
/// Color::new(1., 1., 1.),
/// ));
/// w.light = Some(PointLight::new(Tuple::point(0., 0.25, 0.), WHITE));
/// let r = Ray::new(Tuple::point(0., 0., 0.), Tuple::vector(0., 0., 1.));
/// let s = &w.objects[1];
/// let i = Intersection::new(0.5, &s);
@ -139,13 +137,14 @@ impl World {
/// rays::Ray,
/// tuples::{Color, Tuple},
/// world::World,
/// BLACK,
/// };
///
/// // The color when a ray misses.
/// let w = World::test_world();
/// let r = Ray::new(Tuple::point(0., 0., -5.), Tuple::vector(0., 1., 0.));
/// let c = w.color_at(&r);
/// assert_eq!(c, Color::new(0., 0., 0.));
/// assert_eq!(c, BLACK);
///
/// // The color when a ray hits.
/// let w = World::test_world();