Implement most basic tuple
This commit is contained in:
parent
a30a5a383c
commit
495c64249c
1
rtchallenge/src/lib.rs
Normal file
1
rtchallenge/src/lib.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
mod tuples;
|
||||||
3
rtchallenge/src/main.rs
Normal file
3
rtchallenge/src/main.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
47
rtchallenge/src/tuples.rs
Normal file
47
rtchallenge/src/tuples.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
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 tuple(x: f32, y: f32, z: f32, w: f32) -> Tuple {
|
||||||
|
Tuple { x, y, z, w }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::tuple;
|
||||||
|
#[test]
|
||||||
|
fn 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 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user