hadean-zig/src/geometry.zig

50 lines
1.0 KiB
Zig
Raw Normal View History

2024-08-21 15:57:18 -04:00
const Vec2i = @import("geometry/Vec2i.zig");
pub const Recti = struct {
x: i32,
y: i32,
w: i32,
h: i32,
a: Vec2i,
b: Vec2i,
pub fn from_xywh(x: i32, y: i32, w: i32, h: i32) Recti {
return .{
.x = x,
.y = y,
.w = w,
.h = h,
.a = Vec2i.new(x, y),
.b = Vec2i.new(x + w, y + h),
};
}
};
pub const Matrix4f = struct {
values: [16]f32 = identity,
const SIZE: i8 = 4 * 4;
const identity = from_values([16]f32 {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
});
pub fn from_values(values: [16]f32) Matrix4f {
return .{ .values = values };
}
pub fn orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) Matrix4f {
var ortho = Matrix4f.identity;
ortho.values[0 + 0 * 4] = 2.0 / (right - left);
ortho.values[1 + 1 * 4] = 2.0 / (top - bottom);
ortho.values[2 + 2 * 4] = 2.0 / (near - far);
ortho.values[0 + 3 * 4] = (left + right) / (left - right);
ortho.values[1 + 3 * 4] = (bottom + top) / (bottom - top);
ortho.values[2 + 3 * 4] = (far + near) / (far - near);
return ortho;
}
};