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, Hash)]
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 const 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 const fn pack_point(self, axis_value: f64, cross_value: f64) -> Point {
31 match self {
32 Self::Horizontal => Point::new(axis_value, cross_value),
33 Self::Vertical => Point::new(cross_value, axis_value),
34 }
35 }
36
37 /// Create a new [`Size`] by arranging the given magnitudes.
38 ///
39 /// The axis value is the one matching the axis (e.g. `height` for [`Self::Vertical`]).
40 /// The cross value is the other one.
41 #[inline]
42 pub const fn pack_size(self, axis_value: f64, cross_value: f64) -> Size {
43 match self {
44 Self::Horizontal => Size::new(axis_value, cross_value),
45 Self::Vertical => Size::new(cross_value, axis_value),
46 }
47 }
48
49 /// Create a new [`Vec2`] by arranging the given magnitudes.
50 ///
51 /// The axis value is the one matching the axis (e.g. `y` for [`Self::Vertical`]).
52 /// The cross value is the other one.
53 #[inline]
54 pub const fn pack_vec2(self, axis_value: f64, cross_value: f64) -> Vec2 {
55 match self {
56 Self::Horizontal => Vec2::new(axis_value, cross_value),
57 Self::Vertical => Vec2::new(cross_value, axis_value),
58 }
59 }
60
61 /// Create a new `(x, y)` pair by arranging the given magnitudes.
62 ///
63 /// The axis value is the one matching the axis (e.g. `y` for [`Self::Vertical`]).
64 /// The cross value is the other one.
65 #[inline]
66 pub const fn pack_xy(self, axis_value: f64, cross_value: f64) -> (f64, f64) {
67 match self {
68 Self::Horizontal => (axis_value, cross_value),
69 Self::Vertical => (cross_value, axis_value),
70 }
71 }
72}