Add constants for Direction2d and Direction3d (#11180)

# Objective

I often need a direction along one of the cartesian XYZ axes, and it
currently requires e.g. `Direction2d::from_normalized(Vec2::X)`, which
isn't ideal.

## Solution

Add direction constants that are the same as the ones on Glam types. I
also copied the doc comment format "A unit vector pointing along the ...
axis", but I can change it if there's a better wording for directions.
This commit is contained in:
Joona Aalto 2024-01-02 19:00:23 +02:00 committed by GitHub
parent 6086d4193e
commit 2c5439b25e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 8 deletions

View file

@ -7,6 +7,15 @@ use crate::Vec2;
pub struct Direction2d(Vec2);
impl Direction2d {
/// A unit vector pointing along the positive X axis.
pub const X: Self = Self(Vec2::X);
/// A unit vector pointing along the positive Y axis.
pub const Y: Self = Self(Vec2::Y);
/// A unit vector pointing along the negative X axis.
pub const NEG_X: Self = Self(Vec2::NEG_X);
/// A unit vector pointing along the negative Y axis.
pub const NEG_Y: Self = Self(Vec2::NEG_Y);
/// Create a direction from a finite, nonzero [`Vec2`].
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
@ -378,10 +387,7 @@ mod tests {
#[test]
fn direction_creation() {
assert_eq!(
Direction2d::new(Vec2::X * 12.5),
Ok(Direction2d::from_normalized(Vec2::X))
);
assert_eq!(Direction2d::new(Vec2::X * 12.5), Ok(Direction2d::X));
assert_eq!(
Direction2d::new(Vec2::new(0.0, 0.0)),
Err(InvalidDirectionError::Zero)

View file

@ -7,6 +7,19 @@ use crate::Vec3;
pub struct Direction3d(Vec3);
impl Direction3d {
/// A unit vector pointing along the positive X axis.
pub const X: Self = Self(Vec3::X);
/// A unit vector pointing along the positive Y axis.
pub const Y: Self = Self(Vec3::Y);
/// A unit vector pointing along the positive Z axis.
pub const Z: Self = Self(Vec3::Z);
/// A unit vector pointing along the negative X axis.
pub const NEG_X: Self = Self(Vec3::NEG_X);
/// A unit vector pointing along the negative Y axis.
pub const NEG_Y: Self = Self(Vec3::NEG_Y);
/// A unit vector pointing along the negative Z axis.
pub const NEG_Z: Self = Self(Vec3::NEG_Z);
/// Create a direction from a finite, nonzero [`Vec3`].
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
@ -391,10 +404,7 @@ mod test {
#[test]
fn direction_creation() {
assert_eq!(
Direction3d::new(Vec3::X * 12.5),
Ok(Direction3d::from_normalized(Vec3::X))
);
assert_eq!(Direction3d::new(Vec3::X * 12.5), Ok(Direction3d::X));
assert_eq!(
Direction3d::new(Vec3::new(0.0, 0.0, 0.0)),
Err(InvalidDirectionError::Zero)