Compare commits

...

2 Commits

Author SHA1 Message Date
Jon Janzen
1e57661ace Can make a clock face 2021-10-02 19:22:27 -06:00
Jon Janzen
ed3773a299 refactored main to make room for clock 2021-09-11 20:16:07 -06:00
3 changed files with 75 additions and 15 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
target target
rusty-tags.vi rusty-tags.vi
out.ppm *.ppm

View File

@@ -19,6 +19,14 @@ impl Tuple {
} }
} }
pub fn point_zero() -> Tuple {
Tuple {
x: 0.0,
y: 0.0,
z: 0.0,
w: 1.0
}
}
pub fn point(x: f32, y: f32, z: f32) -> Tuple { pub fn point(x: f32, y: f32, z: f32) -> Tuple {
Tuple { Tuple {

View File

@@ -3,6 +3,7 @@ use features::color::Color;
use features::structs::Tuple; use features::structs::Tuple;
use features::matrix::Matrix; use features::matrix::Matrix;
use std::f32::consts::PI;
use std::fmt; use std::fmt;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
@@ -57,7 +58,23 @@ fn init_env() -> Environment {
} }
} }
fn main() { fn write_canvas_to_file(canvas: Canvas, file_path: &str) {
let ppm = canvas.to_ppm();
let mut f = match File::create(file_path) {
Ok(f) => f,
Err(e) => panic!("file error. {}", e),
};
let _ = match f.write_all(ppm.as_bytes()) {
Ok(w) => w,
Err(e) => panic!("did not write. {}", e),
};
}
fn before_clock() {
let env = init_env(); let env = init_env();
let mut ball = Projectile::new( let mut ball = Projectile::new(
@@ -95,19 +112,7 @@ fn main() {
} }
} }
let ppm = canvas.to_ppm(); write_canvas_to_file(canvas, "out.ppm");
let mut f = match File::create("out.ppm") {
Ok(f) => f,
Err(e) => panic!("file error. {}", e),
};
let _ = match f.write_all(ppm.as_bytes()) {
Ok(w) => w,
Err(e) => panic!("did not write. {}", e),
};
let i = Matrix::identity(4); let i = Matrix::identity(4);
println!("The identity matrix is: {:#?}", i); println!("The identity matrix is: {:#?}", i);
let inverse_i = i.inverse(); let inverse_i = i.inverse();
@@ -140,3 +145,50 @@ fn main() {
println!("{:?}", id); println!("{:?}", id);
println!("{:?}", q); println!("{:?}", q);
} }
fn main() {
before_clock();
clock();
}
fn clock() {
println!("Starting clock!");
let mut canvas = Canvas::new(1024, 1024);
let color = Color::new(1.0, 0.0, 0.0);
let middle = 1024.0 / 2.0;
let three_fourths_middle = middle / 4.0 * 3.0;
let center = &Tuple::point_zero() * &Matrix::translation(0.0, 1.0, 0.0);
// canvas.write_pixel(center.x() as usize, center.y() as usize, color);
canvas.write_pixel(middle as usize, middle as usize, Color::new(0.0, 1.0, 0.0));
for i in 1..13 {
let twelve = Tuple::point_zero();
let twelve = &Matrix::translation(0.0, -12.0, 0.0) * &twelve;
let twelve = &Matrix::scaling(0.0, 10.0, 0.0) * &twelve;
let twelve = &Matrix::rotation_z((i as f32 / 12.0) * (2.0 * PI)) * &twelve;
let twelve = &Matrix::translation(middle, middle, 0.0) * &twelve;
println!("{}", twelve);
canvas.write_pixel(twelve.x() as usize, twelve.y() as usize, color);
}
//for i in 0..=-1 {
// let position = ((2.0 * PI) / 12.0) * (i as f32);
// println!("i: {} pos: {}", i, position);
// let transform = &(&(&Matrix::identity(4)
// * &Matrix::translation(0.0, 1.0 , 0.0))
// * &Matrix::rotation_z(position))
// //* &Matrix::identity(4))
// * &Matrix::translation(middle, middle, 0.0);
// let point = &Tuple::point_zero() * &(&transform * &Matrix::scaling(three_fourths_middle, three_fourths_middle, 0.0));
// //let point = &Tuple::point_zero() * &(&Matrix::translation(i as f32 * 6.0, 0.0, 0.0) * &Matrix::translation(middle, middle, 0.0));
// println!("point rotated: {}", &point);
// canvas.write_pixel(point.x() as usize, point.y() as usize, color);
//}
write_canvas_to_file(canvas, "clock.ppm");
println!("Ending clock!");
}