Skip to main content

cranpose_ui_graphics/
point.rs

1use std::ops::Sub;
2
3/// A position in two dimensions, in logical pixels. Compose's `Offset`.
4#[derive(Clone, Copy, Debug, PartialEq, Default)]
5pub struct Point {
6    /// Horizontal coordinate, in logical pixels, growing to the right.
7    pub x: f32,
8    /// Vertical coordinate, in logical pixels, growing downward.
9    pub y: f32,
10}
11
12impl Point {
13    /// Creates a point from its coordinates.
14    pub const fn new(x: f32, y: f32) -> Self {
15        Self { x, y }
16    }
17
18    /// The origin point `(0.0, 0.0)`.
19    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
20
21    /// The distance from the origin to the point, in logical pixels, as Compose's
22    /// `Offset.getDistance()`. The distance between two points is `(a - b).distance()`.
23    #[inline]
24    pub fn distance(self) -> f32 {
25        (self.x * self.x + self.y * self.y).sqrt()
26    }
27}
28
29impl Sub for Point {
30    type Output = Self;
31
32    fn sub(self, other: Self) -> Self {
33        Self {
34            x: self.x - other.x,
35            y: self.y - other.y,
36        }
37    }
38}