Skip to content

Commit

Permalink
Add to_angle method to 2D vectors (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-blackbird authored Oct 14, 2023
1 parent 5fd61e9 commit 58e7eac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
8 changes: 8 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,14 @@ impl {{ self_t }} {
}
}

/// Returns the angle (in radians) of this vector in the range `[-π, +π]`.
///
/// The input does not need to be a unit vector however it must be non-zero.
#[inline]
pub fn to_angle(self) -> {{ scalar_t }} {
math::atan2(self.y, self.x)
}

/// Returns the angle (in radians) between `self` and `rhs` in the range `[-π, +π]`.
///
/// The inputs do not need to be unit vectors however they must be non-zero.
Expand Down
8 changes: 8 additions & 0 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ impl Vec2 {
Self { x: cos, y: sin }
}

/// Returns the angle (in radians) of this vector in the range `[-π, +π]`.
///
/// The input does not need to be a unit vector however it must be non-zero.
#[inline]
pub fn to_angle(self) -> f32 {
math::atan2(self.y, self.x)
}

/// Returns the angle (in radians) between `self` and `rhs` in the range `[-π, +π]`.
///
/// The inputs do not need to be unit vectors however they must be non-zero.
Expand Down
8 changes: 8 additions & 0 deletions src/f64/dvec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ impl DVec2 {
Self { x: cos, y: sin }
}

/// Returns the angle (in radians) of this vector in the range `[-π, +π]`.
///
/// The input does not need to be a unit vector however it must be non-zero.
#[inline]
pub fn to_angle(self) -> f64 {
math::atan2(self.y, self.x)
}

/// Returns the angle (in radians) between `self` and `rhs` in the range `[-π, +π]`.
///
/// The inputs do not need to be unit vectors however they must be non-zero.
Expand Down
35 changes: 21 additions & 14 deletions tests/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,20 +896,27 @@ macro_rules! impl_vec2_float_tests {
);
});

glam_test!(test_from_angle, {
assert_approx_eq!($vec2::from_angle(0.0), $vec2::new(1.0, 0.0));
assert_approx_eq!(
$vec2::from_angle(core::$t::consts::FRAC_PI_2),
$vec2::new(0.0, 1.0)
);
assert_approx_eq!(
$vec2::from_angle(core::$t::consts::PI),
$vec2::new(-1.0, 0.0)
);
assert_approx_eq!(
$vec2::from_angle(-core::$t::consts::FRAC_PI_2),
$vec2::new(0.0, -1.0)
);
glam_test!(test_angle_conversion, {
let angle = 0.;
let vec = $vec2::from_angle(angle);
assert_approx_eq!(vec, $vec2::new(1.0, 0.0));
assert_approx_eq!(vec.to_angle(), angle);

let angle = core::$t::consts::FRAC_PI_2;
let vec = $vec2::from_angle(angle);
assert_approx_eq!(vec, $vec2::new(0.0, 1.0));
assert_approx_eq!(vec.to_angle(), angle);

let angle = core::$t::consts::PI;
let vec = $vec2::from_angle(angle);
assert_approx_eq!(vec, $vec2::new(-1.0, 0.0));
// The sign of the angle PI gets flipped and is slightly less precise but correct
assert_approx_eq!(vec.to_angle().abs(), angle, 1e-6);

let angle = -core::$t::consts::FRAC_PI_2;
let vec = $vec2::from_angle(angle);
assert_approx_eq!(vec, $vec2::new(0.0, -1.0));
assert_approx_eq!(vec.to_angle(), angle);
});
};
}
Expand Down

0 comments on commit 58e7eac

Please sign in to comment.