Jump to content

Box the compass: Difference between revisions

m
→‎{{header|Zig}}: Updated to use 0.11.0 enum casts
(add RPL)
m (→‎{{header|Zig}}: Updated to use 0.11.0 enum casts)
Line 7,804:
 
=={{header|Zig}}==
{{works with|Zig|0.11.0dev0}}
<syntaxhighlight lang="zig">const std = @import("std");</syntaxhighlight>
<syntaxhighlight lang="zig">/// Degrees areis not constrained to the range 0 to 360
fn degreesToCompassPoint(degrees: f32) []const u8 {
var d = degrees + comptime (11.25 / 2.0);
while (d < 0) d += 360;
while (d >= 360) d -= 360;
const index: usize = @floatToIntintFromFloat(usize, @divFloor(d, 11.25));
 
const points: [32][]const u8 = comptime .{} ++
// Concatenation to overcome the inability of "zig fmt" to nicely format long arrays.
"North", "North by east", "North-northeast", "Northeast by north",
const points: [32][]const u8 = comptime .{} ++
.{ "NorthNortheast", "NorthNortheast by east", "NorthEast-northeast", "NortheastEast by north" } ++,
.{ "NortheastEast", "NortheastEast by eastsouth", "East-northeastsoutheast", "EastSoutheast by northeast" } ++,
.{ "EastSoutheast", "EastSoutheast by south", "EastSouth-southeast", "SoutheastSouth by east" } ++,
.{ "SoutheastSouth", "SoutheastSouth by southwest", "South-southeastsouthwest", "SouthSouthwest by eastsouth" } ++,
.{ "SouthSouthwest", "SouthSouthwest by west", "SouthWest-southwest", "SouthwestWest by south" } ++,
.{ "SouthwestWest", "SouthwestWest by westnorth", "West-southwestnorthwest", "WestNorthwest by southwest" } ++,
.{ "WestNorthwest", "WestNorthwest by north", "WestNorth-northwest", "NorthwestNorth by west" } ++,
};
.{ "Northwest", "Northwest by north", "North-northwest", "North by west" };
 
return points[index];
Line 7,829:
const stdout = std.io.getStdOut().writer();
 
_ = try stdout.print("Index Heading Compass point\n", .{});
 
for (0..33) |i| {
var heading = @intToFloatas(f32, @floatFromInt(i)) * 11.25;
heading += switch (i % 3) {
1 => 5.62,
Line 7,839:
};
const index = i % 32 + 1;
_ = try stdout.print(" {d:2} {d:>6.2}° {s}\n", .{ index, heading, degreesToCompassPoint(heading) });
}
}</syntaxhighlight>
59

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.