engine/geom.rs
1// SPDX-FileCopyrightText: 2025 Jens Pitkänen <jens.pitkanen@helsinki.fi>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5/// A floating-point axis-aligned 2D rectangle.
6#[derive(Debug, Clone, Copy)]
7pub struct Rect {
8 /// The horizontal coordinate of the top-left corner of the rectangle.
9 pub x: f32,
10 /// The vertical coordinate of the top-left corner of the rectangle.
11 pub y: f32,
12 /// The width of the rectangle.
13 pub w: f32,
14 /// The height of the rectangle.
15 pub h: f32,
16}
17
18impl Rect {
19 /// Creates a new [`Rect`] from a given top-left corner and dimensions.
20 pub const fn xywh(x: f32, y: f32, w: f32, h: f32) -> Rect {
21 Rect { x, y, w, h }
22 }
23
24 /// Creates a new [`Rect`] from a given center coordinate and dimensions.
25 pub const fn around(x: f32, y: f32, w: f32, h: f32) -> Rect {
26 Rect {
27 x: x - w / 2.0,
28 y: y - h / 2.0,
29 w,
30 h,
31 }
32 }
33}