Skip to content

Commit

Permalink
3 - add vector and color type
Browse files Browse the repository at this point in the history
  • Loading branch information
grishy committed Feb 11, 2024
1 parent a381383 commit 7e63cd4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nalgebra = "0.32.3"
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use std::fs::File;
use std::io::prelude::*;

extern crate nalgebra as na;
use na::Vector3;

type Color = Vector3<i32>;

fn write_color(dst: &mut dyn Write, color: Color) {
writeln!(dst, "{} {} {}", color[0], color[1], color[2]).unwrap();
}

fn main() {
// Image params
let image_width = 256;
Expand All @@ -13,9 +22,8 @@ fn main() {
writeln!(image_file, "P3\n{} {}\n255", image_width, image_height).unwrap();

for j in 0..image_height {
println!("\rScanlines remaining: {}", (image_height-j));
println!("\rScanlines remaining: {}", (image_height - j));
for i in 0..image_width {

let r = i as f64 / (image_width - 1) as f64;
let g = j as f64 / (image_height - 1) as f64;
let b = 0 as f64;
Expand All @@ -24,7 +32,8 @@ fn main() {
let ig = (255.999 * g) as i32;
let ib = (255.999 * b) as i32;

writeln!(image_file, "{} {} {}", ir, ig, ib).unwrap();
let pixel = Color::new(ir, ig, ib);
write_color(&mut image_file, pixel);
}
}
println!("Done");
Expand Down

0 comments on commit 7e63cd4

Please sign in to comment.