Skip to content

Commit

Permalink
Add xy() and reverse() equivalents; Fixes #35 and #36
Browse files Browse the repository at this point in the history
  • Loading branch information
yoanlcq committed Sep 3, 2019
1 parent 1c58a03 commit bc185ba
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,14 @@ macro_rules! vec_impl_all_vecs {
vec_impl_spatial!(Vec2);
vec_impl_spatial_2d!(Vec2);

impl<T> Vec2<T> {
/// Returns a copy of this vector, with X and Y swapped.
pub fn yx(self) -> Self {
let Self { x, y } = self;
Self { x: y, y: x }
}
}

impl<T> From<Vec3<T>> for Vec2<T> {
fn from(v: Vec3<T>) -> Self {
Self::new(v.x, v.y)
Expand Down Expand Up @@ -2642,6 +2650,18 @@ macro_rules! vec_impl_all_vecs {
vec_impl_spatial!(Vec3);
vec_impl_spatial_3d!(Vec3);

impl<T> Vec3<T> {
/// Returns a copy of this vector, with X and Z swapped.
pub fn zyx(self) -> Self {
let Self { x, y, z } = self;
Self { x: z, y, z: x }
}
/// Same as Vec2::from(self), but shorter.
pub fn xy(self) -> Vec2<T> {
self.into()
}
}

impl<T: Zero> From<Vec2<T>> for Vec3<T> {
fn from(v: Vec2<T>) -> Self {
Self::new(v.x, v.y, T::zero())
Expand Down Expand Up @@ -2696,6 +2716,27 @@ macro_rules! vec_impl_all_vecs {
vec_impl_shuffle_4d!(Vec4 (x y z w));
vec_impl_mat2_via_vec4!(Vec4);

impl<T> Vec4<T> {
/// Returns a copy of this vector, with elements reversed.
pub fn wzyx(self) -> Self {
let Self { x, y, z, w } = self;
Self { x: w, y: z, z: y, w: x }
}
/// Returns a copy of this vector, with X and Z swapped.
pub fn zyxw(self) -> Self {
let Self { x, y, z, w } = self;
Self { x: z, y, z: x, w }
}
/// Same as Vec3::from(self), but shorter.
pub fn xyz(self) -> Vec3<T> {
self.into()
}
/// Same as Vec2::from(self), but shorter.
pub fn xy(self) -> Vec2<T> {
self.into()
}
}

impl<T: Zero> From<Vec3<T>> for Vec4<T> {
fn from(v: Vec3<T>) -> Self {
Self::new(v.x, v.y, v.z, T::zero())
Expand Down Expand Up @@ -2876,6 +2917,13 @@ macro_rules! vec_impl_all_vecs {
vec_impl_color_rgba!{Rgba}
vec_impl_shuffle_4d!(Rgba (r g b a));

#[cfg(feature="rgb")]
impl<T> Rgba<T> {
/// Same as Rgb::from(self), but more concise.
fn rgb(self) -> Rgb<T> {
self.into()
}
}
impl<T> From<Vec4<T>> for Rgba<T> {
fn from(v: Vec4<T>) -> Self {
Self::new(v.x, v.y, v.z, v.w)
Expand Down

0 comments on commit bc185ba

Please sign in to comment.