From 39d1933943a506a0f1f2ce19fe7555f987e1752d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 27 Nov 2022 11:17:24 +0900 Subject: 3.3 --- src/main.zig | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 7373cb6..986a8f6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,6 +28,14 @@ fn vecNormalized(v: Vec3f) Vec3f { return v / vecNorm(v); } +fn writeColor(out: anytype, c: Color) !void { + try out.print("{} {} {}\n", .{ + @floatToInt(u8, 255.999 * c[0]), + @floatToInt(u8, 255.999 * c[1]), + @floatToInt(u8, 255.999 * c[2]), + }); +} + pub fn main() !void { // Image @@ -47,15 +55,11 @@ pub fn main() !void { std.debug.print("\rScanlines remaining: {}", .{j}); var i: i32 = 0; while (i < image_width) { - const r = @intToFloat(f64, i) / (image_width - 1); - const g = @intToFloat(f64, j) / (image_height - 1); - const b = 0.25; - - const ir = @floatToInt(u8, 255.999 * r); - const ig = @floatToInt(u8, 255.999 * g); - const ib = @floatToInt(u8, 255.999 * b); - - try stdout.print("{} {} {}\n", .{ ir, ig, ib }); + try writeColor(stdout, Color{ + @intToFloat(f64, i) / (image_width - 1), + @intToFloat(f64, j) / (image_height - 1), + 0.25, + }); i += 1; } j -= 1; -- cgit v1.2.3-70-g09d2