Compare commits

..

3 Commits

Author SHA1 Message Date
b159820bad Metadata.
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-24 11:10:29 -07:00
3952a8ba83 Implement point/vector constructors. 2021-06-24 11:10:11 -07:00
495c64249c Implement most basic tuple 2021-06-24 10:51:35 -07:00
6 changed files with 86 additions and 0 deletions

5
rtchallenge/Cargo.lock generated Normal file
View File

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "rtchallenge"
version = "0.1.0"

9
rtchallenge/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rtchallenge"
version = "0.1.0"
authors = ["Bill Thiede <git@xinu.tv>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

4
rtchallenge/README.md Normal file
View File

@ -0,0 +1,4 @@
# The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer
This is a rust implementation of https://pragprog.com/titles/jbtracer/the-ray-tracer-challenge/
It attempts to be a test-driven developed ray tracer.

1
rtchallenge/src/lib.rs Normal file
View File

@ -0,0 +1 @@
mod tuples;

3
rtchallenge/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

64
rtchallenge/src/tuples.rs Normal file
View File

@ -0,0 +1,64 @@
#[derive(Debug, PartialEq)]
struct Tuple {
x: f32,
y: f32,
z: f32,
w: f32,
}
impl Tuple {
fn is_point(&self) -> bool {
self.w == 1.0
}
fn is_vector(&self) -> bool {
self.w == 0.0
}
}
fn point(x: f32, y: f32, z: f32) -> Tuple {
tuple(x, y, z, 1.0)
}
fn vector(x: f32, y: f32, z: f32) -> Tuple {
tuple(x, y, z, 0.0)
}
fn tuple(x: f32, y: f32, z: f32, w: f32) -> Tuple {
Tuple { x, y, z, w }
}
#[cfg(test)]
mod tests {
use super::{point, tuple, vector};
#[test]
fn is_point() {
// A tuple with w = 1 is a point
let a = tuple(4.3, -4.2, 3.1, 1.0);
assert_eq!(a.x, 4.3);
assert_eq!(a.y, -4.2);
assert_eq!(a.z, 3.1);
assert_eq!(a.w, 1.0);
assert!(a.is_point());
assert!(!a.is_vector());
}
#[test]
fn is_vector() {
// A tuple with w = 0 is a point
let a = tuple(4.3, -4.2, 3.1, 0.0);
assert_eq!(a.x, 4.3);
assert_eq!(a.y, -4.2);
assert_eq!(a.z, 3.1);
assert_eq!(a.w, 0.0);
assert!(!a.is_point());
assert!(a.is_vector());
}
#[test]
fn point_tuple() {
assert_eq!(point(4., -4., 3.), tuple(4., -4., 3., 1.))
}
#[test]
fn vector_tuple() {
assert_eq!(vector(4., -4., 3.), tuple(4., -4., 3., 0.))
}
}