//! An axis-aligned rectangle given as min/max corners (half-open bounds). //! Mirrors `image.Rectangle`. const ir = @import("root.zig"); const Size = ir.Size; const Rectangle = @This(); min_x: Size = 0, min_y: Size = 0, max_x: Size = 0, max_y: Size = 0, /// Sums (x + w) can exceed `Size`'s range on paper; compute in u32 so an /// overflow traps in Debug instead of wrapping the Size arithmetic. pub fn new(x: Size, y: Size, w: Size, h: Size) Rectangle { return .{ .min_x = x, .min_y = y, .max_x = @intCast(@as(u32, x) + w), .max_y = @intCast(@as(u32, y) + h), }; } pub fn width(self: Rectangle) Size { return self.max_x - self.min_x; } pub fn height(self: Rectangle) Size { return self.max_y - self.min_y; } pub fn empty(self: Rectangle) bool { return self.max_x <= self.min_x or self.max_y <= self.min_y; } pub fn overlaps(self: Rectangle, other: Rectangle) bool { return self.min_x < other.max_x and self.max_x > other.min_x and self.min_y < other.max_y and self.max_y > other.min_y; } pub fn inside(self: Rectangle, other: Rectangle) bool { return self.min_x >= other.min_x and self.min_y >= other.min_y and self.max_x <= other.max_x and self.max_y <= other.max_y; }