Open
Description
Issue
Using a fairly up to date version of Zig (0.14.0-dev.2989+bf6ee7cb3
) results in error: type 'root.Angle' does not support array initialization syntax
for a trivial example such as:
const ff = @import("firefly");
pub export fn render() void {
_ = ff.Angle.from_radians(0);
_ = ff.Angle.from_degrees(0);
}
Note
Requires adjustments to build.zig
due to the use of deprecated std.zig.CrossTarget
error: deprecated; use std.Target.Query pub const CrossTarget = @compileError("deprecated; use std.Target.Query");
Suggested change
Something like this, where the .radians
field is explicitly assigned to:
pub const Angle = struct {
radians: f32 = 0,
/// The 360° angle.
pub const full_circle: Angle = Angle{ .radians = tau };
/// The 180° angle.
pub const half_circle: Angle = Angle{ .radians = pi };
/// The 90° angle.
pub const quarter_circle: Angle = Angle{ .radians = pi / 2.0 };
/// The 0° angle.
pub const zero: Angle = Angle{};
/// An Angle in radians where `tau` (doubled `pi`) is the full circle.
pub fn from_radians(r: f32) Angle {
return Angle{ .radians = r };
}
/// An Angle in degrees where **360.0** is the full circle.
pub fn from_degrees(d: f32) Angle {
return Angle{ .radians = d * pi / 180.0 };
}
};
A few test cases
test "Angle.full_circle returns expected value" {
try std.testing.expectEqual(Angle{ .radians = tau }, Angle.full_circle);
}
test "Angle.half_circle returns expected value" {
try std.testing.expectEqual(Angle{ .radians = pi }, Angle.half_circle);
}
test "Angle.quarter_circle returns expected value" {
try std.testing.expectEqual(Angle{ .radians = pi / 2.0 }, Angle.quarter_circle);
}
test "Angle.zero returns expected value" {
try std.testing.expectEqual(Angle{}, Angle.zero);
try std.testing.expectEqual(Angle{ .radians = 0 }, Angle.zero);
}
test "Angle.from_radians returns expected value" {
try std.testing.expectEqual(Angle{ .radians = pi }, Angle.from_radians(pi));
try std.testing.expectEqual(Angle{ .radians = tau }, Angle.from_radians(tau));
}
test "Angle.from_degrees returns expected value" {
try std.testing.expectEqual(Angle{ .radians = tau }, Angle.from_degrees(360.0));
try std.testing.expectEqual(Angle{ .radians = pi }, Angle.from_degrees(180.0));
try std.testing.expectEqual(Angle{ .radians = -pi }, Angle.from_degrees(-180.0));
try std.testing.expectEqual(Angle{ .radians = 0 }, Angle.from_degrees(0.0));
}
Metadata
Metadata
Assignees
Labels
No labels