Skip to main content

gpui_liveplot/
geom.rs

1//! Geometric primitives used by the plotting pipeline.
2//!
3//! Public types in this module represent data-space coordinates. Screen-space
4//! types are internal to render backends.
5
6/// A point in data space.
7///
8/// Use this when providing explicit X/Y values.
9#[derive(Debug, Clone, Copy, PartialEq)]
10pub struct Point {
11    /// X value in data coordinates.
12    pub x: f64,
13    /// Y value in data coordinates.
14    pub y: f64,
15}
16
17impl Point {
18    /// Create a new data point.
19    pub fn new(x: f64, y: f64) -> Self {
20        Self { x, y }
21    }
22}
23
24/// A point in screen space (pixel coordinates).
25#[derive(Debug, Clone, Copy, PartialEq)]
26pub(crate) struct ScreenPoint {
27    /// X value in screen pixels.
28    pub(crate) x: f32,
29    /// Y value in screen pixels.
30    pub(crate) y: f32,
31}
32
33impl ScreenPoint {
34    /// Create a new screen point.
35    pub(crate) fn new(x: f32, y: f32) -> Self {
36        Self { x, y }
37    }
38}
39
40/// A rectangle in screen space (pixel coordinates).
41#[derive(Debug, Clone, Copy, PartialEq)]
42pub(crate) struct ScreenRect {
43    /// Top-left corner.
44    pub(crate) min: ScreenPoint,
45    /// Bottom-right corner.
46    pub(crate) max: ScreenPoint,
47}
48
49impl ScreenRect {
50    /// Create a new screen rectangle from corners.
51    pub(crate) fn new(min: ScreenPoint, max: ScreenPoint) -> Self {
52        Self { min, max }
53    }
54
55    /// Rectangle width in pixels.
56    pub(crate) fn width(&self) -> f32 {
57        self.max.x - self.min.x
58    }
59
60    /// Rectangle height in pixels.
61    pub(crate) fn height(&self) -> f32 {
62        self.max.y - self.min.y
63    }
64
65    /// Check whether the rectangle has positive area.
66    pub(crate) fn is_valid(&self) -> bool {
67        self.width() > 0.0 && self.height() > 0.0
68    }
69}