From 7e63cd4d625dce7fd4def511aa9bdb400ff4b3a9 Mon Sep 17 00:00:00 2001 From: Sergey G Date: Sun, 11 Feb 2024 14:33:24 +0100 Subject: [PATCH] 3 - add vector and color type --- Cargo.toml | 1 + src/main.rs | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8186852..3559e2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 038bb5f..cbf8186 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,15 @@ use std::fs::File; use std::io::prelude::*; +extern crate nalgebra as na; +use na::Vector3; + +type Color = Vector3; + +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; @@ -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; @@ -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");