Skip to main content

valo_geometry/
point.rs

1/// `Point` is a position or vector in two-dimensional coordinates.
2#[derive(Clone, Copy, Debug, Default, PartialEq)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4pub struct Point {
5    /// `x` is the horizontal component.
6    pub x: f32,
7    /// `y` is the vertical component.
8    pub y: f32,
9}
10
11impl Point {
12    /// `ZERO` is the origin.
13    pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
14
15    /// `new` creates a point from its components.
16    pub const fn new(x: f32, y: f32) -> Self {
17        Self { x, y }
18    }
19}
20
21impl From<(f32, f32)> for Point {
22    fn from((x, y): (f32, f32)) -> Self {
23        Self { x, y }
24    }
25}
26
27impl std::ops::Add for Point {
28    type Output = Point;
29    fn add(self, o: Point) -> Point {
30        Point::new(self.x + o.x, self.y + o.y)
31    }
32}
33
34impl std::ops::Sub for Point {
35    type Output = Point;
36    fn sub(self, o: Point) -> Point {
37        Point::new(self.x - o.x, self.y - o.y)
38    }
39}
40
41/// `Size` is a width and height in two-dimensional coordinates.
42#[derive(Clone, Copy, Debug, Default, PartialEq)]
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44pub struct Size {
45    /// `width` is the horizontal extent.
46    pub width: f32,
47    /// `height` is the vertical extent.
48    pub height: f32,
49}
50
51impl Size {
52    /// `new` creates a size from its extents.
53    pub const fn new(width: f32, height: f32) -> Self {
54        Self { width, height }
55    }
56
57    /// `is_empty` reports whether either extent is nonpositive.
58    pub fn is_empty(&self) -> bool {
59        self.width <= 0.0 || self.height <= 0.0
60    }
61}