ailloli_ui_core/geometry/
point.rs

1//! Positions in a two-dimensional logical coordinate space.
2
3/// A point in logical 2D space.
4///
5/// # Examples
6///
7/// ```
8/// use ailloli_ui_core::Point;
9/// assert_eq!(Point::new(2.0, 3.0), Point { x: 2.0, y: 3.0 });
10/// ```
11#[derive(Debug, Clone, Copy, Default, PartialEq)]
12pub struct Point {
13    /// Horizontal logical coordinate; increasing values point right.
14    pub x: f32,
15    /// Vertical logical coordinate; increasing values point down.
16    pub y: f32,
17}
18
19impl Point {
20    /// Creates a point at `(x, y)` without normalizing non-finite values.
21    ///
22    /// # Examples
23    ///
24    /// ```
25    /// use ailloli_ui_core::Point;
26    /// assert_eq!(Point::new(2.0, 3.0).x, 2.0);
27    /// ```
28    pub const fn new(x: f32, y: f32) -> Self {
29        Self { x, y }
30    }
31
32    /// Floors both coordinates independently.
33    ///
34    /// # Examples
35    ///
36    /// ```
37    /// use ailloli_ui_core::Point;
38    /// assert_eq!(Point::new(2.9, -2.1).floor(), Point::new(2.0, -3.0));
39    /// ```
40    pub fn floor(self) -> Self {
41        Self::new(self.x.floor(), self.y.floor())
42    }
43
44    /// Rounds both coordinates independently to the nearest whole value.
45    ///
46    /// # Examples
47    ///
48    /// ```
49    /// use ailloli_ui_core::Point;
50    /// assert_eq!(Point::new(2.6, -2.4).round(), Point::new(3.0, -2.0));
51    /// ```
52    pub fn round(self) -> Self {
53        Self::new(self.x.round(), self.y.round())
54    }
55}