Add some format methods
This commit is contained in:
@@ -66,6 +66,10 @@ pub const Color = extern struct {
|
||||
pub inline fn asArray(self: Color) Array {
|
||||
return @bitCast(self);
|
||||
}
|
||||
|
||||
pub fn format(self: Color, w: *std.io.Writer) !void {
|
||||
try w.print("#{X:0>2}{X:0>2}{X:0>2}{X:0>2}", .{ self.r, self.g, self.b, self.a });
|
||||
}
|
||||
};
|
||||
|
||||
test "l" {
|
||||
|
||||
@@ -122,4 +122,8 @@ pub const Complex = extern struct {
|
||||
.im = @mulAdd(f32, t, b.im, @mulAdd(f32, -t, a.im, a.im)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(self: Complex, w: *std.io.Writer) !void {
|
||||
try w.print("Complex[{d:.3}, {d:.3}]", .{ self.re, self.im });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,4 +178,8 @@ pub const Quaternion = extern struct {
|
||||
.w = @mulAdd(f32, t, b.w, @mulAdd(f32, -t, a.w, a.w)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(self: Quaternion, w: *std.io.Writer) !void {
|
||||
try w.print("Quaternion[{d:.3}, {d:.3}, {d:.3}, {d:.3}]", .{ self.x, self.y, self.z, self.w });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -162,4 +162,8 @@ pub const Vector2 = extern struct {
|
||||
.y = vm.ps(self.x) * m.iy + vm.ps(self.y) * m.jy,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(self: Vector2, w: *std.io.Writer) !void {
|
||||
try w.print("[{d}, {d}]", .{ self.x, self.y });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -105,4 +105,11 @@ pub const Vector2Int = extern struct {
|
||||
pub inline fn cross(self: Vector2Int, other: Vector2Int) i32 {
|
||||
return self.x * other.y - self.y * other.x;
|
||||
}
|
||||
|
||||
pub fn format(self: Vector2Int, w: *std.io.Writer) !void {
|
||||
try w.print("[{X:0>8}, {X:0>8}]", .{
|
||||
@as(u32, @bitCast(self.x)),
|
||||
@as(u32, @bitCast(self.y)),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -187,4 +187,8 @@ pub const Vector3 = extern struct {
|
||||
.z = vm.ps(self.x) * m.iz + vm.ps(self.y) * m.jz + vm.ps(self.z) * m.kz,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(self: Vector3, w: *std.io.Writer) !void {
|
||||
try w.print("[{d}, {d}, {d}]", .{ self.x, self.y, self.z });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -112,4 +112,12 @@ pub const Vector3Int = extern struct {
|
||||
.z = self.x * other.y - self.y * other.x,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(self: Vector3Int, w: *std.io.Writer) !void {
|
||||
try w.print("[{X:0>8}, {X:0>8}, {X:0>8}]", .{
|
||||
@as(u32, @bitCast(self.x)),
|
||||
@as(u32, @bitCast(self.y)),
|
||||
@as(u32, @bitCast(self.z)),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -142,4 +142,8 @@ pub const Vector4 = extern struct {
|
||||
.w = vm.ps(self.x) * m.iw + vm.ps(self.y) * m.jw + vm.ps(self.z) * m.kw + vm.ps(self.w) * m.tw,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn format(self: Vector4, w: *std.io.Writer) !void {
|
||||
try w.print("[{d}, {d}, {d}, {d}]", .{ self.x, self.y, self.z, self.w });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -107,4 +107,13 @@ pub const Vector4Int = extern struct {
|
||||
pub inline fn dot(self: Vector4Int, other: Vector4Int) i32 {
|
||||
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w;
|
||||
}
|
||||
|
||||
pub fn format(self: Vector4Int, w: *std.io.Writer) !void {
|
||||
try w.print("[{X:0>8}, {X:0>8}, {X:0>8}, {X:0>8}]", .{
|
||||
@as(u32, @bitCast(self.x)),
|
||||
@as(u32, @bitCast(self.y)),
|
||||
@as(u32, @bitCast(self.z)),
|
||||
@as(u32, @bitCast(self.w)),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user