Death Star: Difference between revisions
m
→{{header|Zig}}: formatting and change @floatToInt() becomes @intFromFloat(), @intToFloat() becomes @floatFromInt()
Recombinant (talk | contribs) No edit summary |
Recombinant (talk | contribs) 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 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 = @
var h: usize = @
var img = try ImageData().init(allocator, "deathStar", w, h);
var vec = Vector(T).zero();
const start_y: usize = @
const end_y: usize = @
const start_x: usize = @
const end_x: usize = @
for (start_y..end_y + 1) |j| {
for (start_x..end_x + 1) |i| {
const x: T = @
const y: T = @
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 = @
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 {
|