hadean-zig/src/geometry/Recti.zig

60 lines
1.1 KiB
Zig
Raw Normal View History

2024-08-21 15:57:18 -04:00
const Vec2i = @import("./Vec2i.zig");
const Vec2f = @import("./Vec2f.zig");
const Rectf = @import("./Rectf.zig");
2024-09-03 13:13:28 -04:00
const std = @import("std");
2024-08-21 15:57:18 -04:00
const Recti = @This();
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,
2024-09-03 13:13:28 -04:00
.a = Vec2i.create(x, y),
.b = Vec2i.create(x + w, y + h),
};
}
pub fn from_ab(a: Vec2i, b: Vec2i) Recti {
const tlx = @min(a.x, b.x);
const tly = @min(a.y, b.y);
const brx = @max(a.x, b.x);
const bry = @max(a.y, b.y);
const width = brx - tlx;
const height = bry - tly;
return .{
.x = tlx,
.y = tly,
.w = width,
.h = height,
.a = Vec2i.create(tlx, tly),
.b = Vec2i.create(brx, bry),
2024-08-21 15:57:18 -04:00
};
}
pub fn to_rectf(self: *const Recti) Rectf {
return Rectf.from_xywh(
@intCast(self.x),
@intCast(self.y),
@intCast(self.w),
@intCast(self.h),
);
}
pub fn scalef(self: *const Recti, scale: Vec2f) Rectf {
return Rectf.from_xywh(
@as(f32, @floatFromInt(self.x)) / scale.x,
@as(f32, @floatFromInt(self.y)) / scale.y,
@as(f32, @floatFromInt(self.w)) / scale.x,
@as(f32, @floatFromInt(self.h)) / scale.y,
);
}