Add Direction2d::from_xy and Direction3d::from_xyz (#10882)

# Objective

Make direction construction a bit more ergonomic.

## Solution

Add `Direction2d::from_xy` and `Direction3d::from_xyz`, similar to
`Transform::from_xyz`:

```rust
let dir2 = Direction2d::from_xy(0.5, 0.5).unwrap();
let dir3 = Direction3d::from_xyz(0.5, 0.5, 0.5).unwrap();
```

This can be a bit cleaner than using `new`:

```rust
let dir2 = Direction2d::new(Vec2::new(0.5, 0.5)).unwrap();
let dir3 = Direction3d::new(Vec3::new(0.5, 0.5, 0.5)).unwrap();
```
This commit is contained in:
Joona Aalto 2023-12-14 16:56:07 +02:00 committed by GitHub
parent fe28e0ec32
commit 029dd06f7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -28,7 +28,15 @@ impl Direction2d {
)
}
/// Create a direction from a [`Vec2`] that is already normalized
/// Create a direction from its `x` and `y` components.
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
/// of the vector formed by the components is zero (or very close to zero), infinite, or `NaN`.
pub fn from_xy(x: f32, y: f32) -> Result<Self, InvalidDirectionError> {
Self::new(Vec2::new(x, y))
}
/// Create a direction from a [`Vec2`] that is already normalized.
pub fn from_normalized(value: Vec2) -> Self {
debug_assert!(value.is_normalized());
Self(value)

View File

@ -28,7 +28,15 @@ impl Direction3d {
)
}
/// Create a direction from a [`Vec3`] that is already normalized
/// Create a direction from its `x`, `y`, and `z` components.
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
/// of the vector formed by the components is zero (or very close to zero), infinite, or `NaN`.
pub fn from_xyz(x: f32, y: f32, z: f32) -> Result<Self, InvalidDirectionError> {
Self::new(Vec3::new(x, y, z))
}
/// Create a direction from a [`Vec3`] that is already normalized.
pub fn from_normalized(value: Vec3) -> Self {
debug_assert!(value.is_normalized());
Self(value)