kurbo/
axis.rs

1// Copyright 2025 the Kurbo Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use crate::{Point, Size, Vec2};
5
6/// An axis in the plane.
7#[derive(Copy, Clone, PartialEq, Eq, Debug)]
8pub enum Axis {
9    /// The x axis.
10    Horizontal,
11    /// The y axis.
12    Vertical,
13}
14
15impl Axis {
16    /// Get the axis perpendicular to this one.
17    #[inline]
18    pub fn cross(self) -> Self {
19        match self {
20            Self::Horizontal => Self::Vertical,
21            Self::Vertical => Self::Horizontal,
22        }
23    }
24
25    /// Create a new [`Point`] by arranging the given magnitudes.
26    ///
27    /// The axis value is the one matching the axis (e.g. `y` for [`Self::Vertical`]).
28    /// The cross value is the other one.
29    #[inline]
30    pub fn pack_point(self, axis_value: f64, cross_value: f64) -> Point {
31        self.pack_xy(axis_value, cross_value).into()
32    }
33
34    /// Create a new [`Size`] by arranging the given magnitudes.
35    ///
36    /// The axis value is the one matching the axis (e.g. `height` for [`Self::Vertical`]).
37    /// The cross value is the other one.
38    #[inline]
39    pub fn pack_size(self, axis_value: f64, cross_value: f64) -> Size {
40        self.pack_xy(axis_value, cross_value).into()
41    }
42
43    /// Create a new [`Vec2`] by arranging the given magnitudes.
44    ///
45    /// The axis value is the one matching the axis (e.g. `y` for [`Self::Vertical`]).
46    /// The cross value is the other one.
47    #[inline]
48    pub fn pack_vec2(self, axis_value: f64, cross_value: f64) -> Vec2 {
49        self.pack_xy(axis_value, cross_value).into()
50    }
51
52    /// Create a new `(x, y)` pair by arranging the given magnitudes.
53    ///
54    /// The axis value is the one matching the axis (e.g. `y` for [`Self::Vertical`]).
55    /// The cross value is the other one.
56    #[inline]
57    pub fn pack_xy(self, axis_value: f64, cross_value: f64) -> (f64, f64) {
58        match self {
59            Self::Horizontal => (axis_value, cross_value),
60            Self::Vertical => (cross_value, axis_value),
61        }
62    }
63}