Death Star: Difference between revisions

340 bytes added ,  11 months ago
m
→‎{{header|Zig}}: formatting and change @floatToInt() becomes @intFromFloat(), @intToFloat() becomes @floatFromInt()
No edit summary
m (→‎{{header|Zig}}: formatting and change @floatToInt() becomes @intFromFloat(), @intToFloat() becomes @floatFromInt())
Line 2,951:
{{trans|C}}
Primitive ray tracing. Writes a PGM to stdout.
<syntaxhighlight lang="zig">const std = @import("std");
const std = @import("std");
const Allocator = std.mem.Allocator;
</syntaxhighlight>
 
<syntaxhighlight lang="zig">pub fn main() !void {
// buffer stdout --------------------------------------
const stdout_file = std.io.getStdOut().writer();
Line 2,982 ⟶ 2,983:
// ----------------------------------------------------
try bw.flush();
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">fn Vector(comptime T: type) type {
 
fn Vector(comptime T: type) type {
return struct {
const Self = @This();
Line 3,010:
}
};
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
fn SphereHit(comptime T: type) type {
return struct { z1: T, z2: T };
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
fn Sphere(comptime T: type) type {
return struct {
Line 3,040:
}
};
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
fn DeathStar(comptime T: type) type {
return struct {
Line 3,062:
const amb: T = 0.2;
 
var w: usize = @floatToIntintFromFloat(usize, pos.r * 4);
var h: usize = @floatToIntintFromFloat(usize, pos.r * 3);
var img = try ImageData().init(allocator, "deathStar", w, h);
 
var vec = Vector(T).zero();
 
const start_y: usize = @floatToIntintFromFloat(usize, pos.cy - pos.r);
const end_y: usize = @floatToIntintFromFloat(usize, pos.cy + pos.r);
const start_x: usize = @floatToIntintFromFloat(usize, pos.cx - pos.r);
const end_x: usize = @floatToIntintFromFloat(usize, pos.cx + pos.r);
 
for (start_y..end_y + 1) |j| {
for (start_x..end_x + 1) |i| {
const x: T = @intToFloatfloatFromInt(T, i);
const y: T = @intToFloatfloatFromInt(T, j);
 
const result_pos = pos.hit(x, y);
Line 3,105:
if (s < 0) s = 0;
const lum = 255 * (std.math.pow(T, s, k) + amb) / (1 + amb);
const lumi: u8 = @floatToIntintFromFloat(u8, std.math.clamp(lum, 0, 255));
img.pset(i, j, Gray{ .w = lumi });
}
Line 3,142:
}
};
}</syntaxhighlight>
}
<syntaxhighlight lang="zig">
 
const Gray = struct {
w: u8,
const black = Gray{ .w = 0 };
};</syntaxhighlight>
};
<syntaxhighlight lang="zig">
 
fn ImageData() type {
return struct {
59

edits