More idiomatic constructors.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f24a90b77b
commit
78a360ae89
@ -1,8 +1,8 @@
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
use rtchallenge::{
|
||||
canvas::{canvas, Canvas},
|
||||
tuples::{color, point, vector, Tuple},
|
||||
canvas::Canvas,
|
||||
tuples::{Color, Tuple},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -23,7 +23,7 @@ fn tick(env: &Environment, proj: &Projectile) -> Projectile {
|
||||
}
|
||||
|
||||
fn draw_dot(c: &mut Canvas, x: usize, y: usize) {
|
||||
let red = color(1., 0., 0.);
|
||||
let red = Color::new(1., 0., 0.);
|
||||
c.set(x.saturating_sub(1), y.saturating_sub(1), red);
|
||||
c.set(x, y.saturating_sub(1), red);
|
||||
c.set(x + 1, y.saturating_sub(1), red);
|
||||
@ -35,16 +35,16 @@ fn draw_dot(c: &mut Canvas, x: usize, y: usize) {
|
||||
c.set(x + 1, y + 1, red);
|
||||
}
|
||||
fn main() -> Result<()> {
|
||||
let position = point(0., 1., 0.);
|
||||
let velocity = vector(1., 1.8, 0.).normalize() * 11.25;
|
||||
let position = Tuple::point(0., 1., 0.);
|
||||
let velocity = Tuple::vector(1., 1.8, 0.).normalize() * 11.25;
|
||||
let mut p = Projectile { position, velocity };
|
||||
let gravity = vector(0., -0.1, 0.);
|
||||
let wind = vector(-0.01, 0., 0.);
|
||||
let gravity = Tuple::vector(0., -0.1, 0.);
|
||||
let wind = Tuple::vector(-0.01, 0., 0.);
|
||||
let e = Environment { gravity, wind };
|
||||
|
||||
let w = 800;
|
||||
let h = 600;
|
||||
let mut c = canvas(w, h);
|
||||
let mut c = Canvas::new(w, h);
|
||||
let mut i = 0;
|
||||
let w = w as f32;
|
||||
let h = h as f32;
|
||||
|
||||
@ -5,7 +5,7 @@ use std::path::Path;
|
||||
use png;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::tuples::{color, Color};
|
||||
use crate::tuples::Color;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum CanvasError {
|
||||
@ -20,16 +20,16 @@ pub struct Canvas {
|
||||
pub pixels: Vec<Color>,
|
||||
}
|
||||
|
||||
pub fn canvas(width: usize, height: usize) -> Canvas {
|
||||
let pixels = vec![color(0., 0., 0.); width * height];
|
||||
Canvas {
|
||||
width,
|
||||
height,
|
||||
pixels,
|
||||
}
|
||||
}
|
||||
|
||||
impl Canvas {
|
||||
pub fn new(width: usize, height: usize) -> Canvas {
|
||||
let pixels = vec![Color::new(0., 0., 0.); width * height];
|
||||
Canvas {
|
||||
width,
|
||||
height,
|
||||
pixels,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, x: usize, y: usize, c: Color) {
|
||||
if x > self.width {
|
||||
return;
|
||||
@ -74,14 +74,14 @@ impl Canvas {
|
||||
mod tests {
|
||||
use super::canvas;
|
||||
|
||||
use crate::tuples::color;
|
||||
use crate::tuples::Color;
|
||||
|
||||
#[test]
|
||||
fn create_canvas() {
|
||||
let c = canvas(10, 20);
|
||||
assert_eq!(c.width, 10);
|
||||
assert_eq!(c.height, 20);
|
||||
let black = color(0., 0., 0.);
|
||||
let black = Color::new(0., 0., 0.);
|
||||
for (i, p) in c.pixels.iter().enumerate() {
|
||||
assert_eq!(p, &black, "pixel {} not {:?}: {:?}", i, &black, p);
|
||||
}
|
||||
@ -90,7 +90,7 @@ mod tests {
|
||||
#[test]
|
||||
fn write_to_canvas() {
|
||||
let mut c = canvas(10, 20);
|
||||
let red = color(1., 0., 0.);
|
||||
let red = Color::new(1., 0., 0.);
|
||||
c.set(2, 3, red);
|
||||
assert_eq!(c.get(2, 3), red);
|
||||
}
|
||||
|
||||
@ -8,6 +8,18 @@ pub struct Tuple {
|
||||
}
|
||||
|
||||
impl Tuple {
|
||||
pub fn point(x: f32, y: f32, z: f32) -> Tuple {
|
||||
Tuple::new(x, y, z, 1.0)
|
||||
}
|
||||
|
||||
pub fn vector(x: f32, y: f32, z: f32) -> Tuple {
|
||||
Tuple::new(x, y, z, 0.0)
|
||||
}
|
||||
|
||||
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Tuple {
|
||||
Tuple { x, y, z, w }
|
||||
}
|
||||
|
||||
pub fn is_point(&self) -> bool {
|
||||
self.w == 1.0
|
||||
}
|
||||
@ -101,23 +113,11 @@ impl Sub for Tuple {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn point(x: f32, y: f32, z: f32) -> Tuple {
|
||||
tuple(x, y, z, 1.0)
|
||||
}
|
||||
|
||||
pub fn vector(x: f32, y: f32, z: f32) -> Tuple {
|
||||
tuple(x, y, z, 0.0)
|
||||
}
|
||||
|
||||
pub fn tuple(x: f32, y: f32, z: f32, w: f32) -> Tuple {
|
||||
Tuple { x, y, z, w }
|
||||
}
|
||||
|
||||
pub fn dot(a: Tuple, b: Tuple) -> f32 {
|
||||
a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
|
||||
}
|
||||
pub fn cross(a: Tuple, b: Tuple) -> Tuple {
|
||||
vector(
|
||||
Tuple::vector(
|
||||
a.y * b.z - a.z * b.y,
|
||||
a.z * b.x - a.x * b.z,
|
||||
a.x * b.y - a.y * b.x,
|
||||
@ -130,8 +130,10 @@ pub struct Color {
|
||||
pub green: f32,
|
||||
pub blue: f32,
|
||||
}
|
||||
pub fn color(red: f32, green: f32, blue: f32) -> Color {
|
||||
Color { red, green, blue }
|
||||
impl Color {
|
||||
pub fn new(red: f32, green: f32, blue: f32) -> Color {
|
||||
Color { red, green, blue }
|
||||
}
|
||||
}
|
||||
impl Add for Color {
|
||||
type Output = Self;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user