Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some swizzling methods to vectors and points. #220

Merged
merged 1 commit into from Jul 3, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add some swizzling methods to vectors and points.

  • Loading branch information
nical committed Jul 3, 2017
commit dbb6ceeee321b2a571d0d4815ef306aca0ed9dd3
@@ -90,6 +90,12 @@ impl<T: Copy, U> TypedPoint2D<T, U> {
vec2(self.x, self.y)
}

/// Swap x and y.
#[inline]
pub fn yx(&self) -> Self {
point2(self.y, self.x)
}

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
@@ -439,6 +445,24 @@ impl<T: Copy, U> TypedPoint3D<T, U> {
vec3(self.x, self.y, self.z)
}

/// Returns a 2d point using this point's x and y coordinates
#[inline]
pub fn xy(&self) -> TypedPoint2D<T, U> {
point2(self.x, self.y)
}

/// Returns a 2d point using this point's x and z coordinates
#[inline]
pub fn xz(&self) -> TypedPoint2D<T, U> {
point2(self.x, self.z)
}

/// Returns a 2d point using this point's x and z coordinates
#[inline]
pub fn yz(&self) -> TypedPoint2D<T, U> {
point2(self.y, self.z)
}

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
@@ -469,7 +493,7 @@ impl<T: Copy, U> TypedPoint3D<T, U> {
/// Convert into a 2d point.
#[inline]
pub fn to_2d(&self) -> TypedPoint2D<T, U> {
point2(self.x, self.y)
self.xy()
}
}

@@ -705,7 +729,7 @@ mod point2d {

#[cfg(test)]
mod typedpoint2d {
use super::TypedPoint2D;
use super::{TypedPoint2D, Point2D, point2};
use scale_factor::ScaleFactor;
use vector::vec2;

@@ -755,11 +779,17 @@ mod typedpoint2d {
assert_eq!(p.to_vector().to_point(), p);
}
}

#[test]
pub fn test_swizzling() {
let p: Point2D<i32> = point2(1, 2);
assert_eq!(p.yx(), point2(2, 1));
}
}

#[cfg(test)]
mod point3d {
use super::Point3D;
use super::{Point3D, point2, point3};

#[test]
pub fn test_min() {
@@ -793,4 +823,12 @@ mod point3d {
assert_eq!(p.to_vector().to_point(), p);
}
}

#[test]
pub fn test_swizzling() {
let p: Point3D<i32> = point3(1, 2, 3);
assert_eq!(p.xy(), point2(1, 2));
assert_eq!(p.xz(), point2(1, 3));
assert_eq!(p.yz(), point2(2, 3));
}
}
@@ -85,6 +85,12 @@ impl<T: Copy, U> TypedVector2D<T, U> {
point2(self.x, self.y)
}

/// Swap x and y.
#[inline]
pub fn yx(&self) -> Self {
vec2(self.y, self.x)
}

/// Cast this vector into a size.
#[inline]
pub fn to_size(&self) -> TypedSize2D<T, U> {
@@ -438,6 +444,24 @@ impl<T: Copy, U> TypedVector3D<T, U> {
point3(self.x, self.y, self.z)
}

/// Returns a 2d vector using this vector's x and y coordinates
#[inline]
pub fn xy(&self) -> TypedVector2D<T, U> {
vec2(self.x, self.y)
}

/// Returns a 2d vector using this vector's x and z coordinates
#[inline]
pub fn xz(&self) -> TypedVector2D<T, U> {
vec2(self.x, self.z)
}

/// Returns a 2d vector using this vector's x and z coordinates
#[inline]
pub fn yz(&self) -> TypedVector2D<T, U> {
vec2(self.y, self.z)
}

/// Returns self.x as a Length carrying the unit.
#[inline]
pub fn x_typed(&self) -> Length<T, U> { Length::new(self.x) }
@@ -468,7 +492,7 @@ impl<T: Copy, U> TypedVector3D<T, U> {
/// Convert into a 2d vector.
#[inline]
pub fn to_2d(&self) -> TypedVector2D<T, U> {
vec2(self.x, self.y)
self.xy()
}
}

@@ -804,7 +828,7 @@ mod vector2d {

#[cfg(test)]
mod typedvector2d {
use super::{TypedVector2D, vec2};
use super::{TypedVector2D, Vector2D, vec2};
use scale_factor::ScaleFactor;

pub enum Mm {}
@@ -840,11 +864,17 @@ mod typedvector2d {

assert_eq!(result, vec2(0.1, 0.2));
}

#[test]
pub fn test_swizzling() {
let p: Vector2D<i32> = vec2(1, 2);
assert_eq!(p.yx(), vec2(2, 1));
}
}

#[cfg(test)]
mod vector3d {
use super::{Vector3D, vec3};
use super::{Vector3D, vec2, vec3};
type Vec3 = Vector3D<f32>;

#[test]
@@ -891,4 +921,12 @@ mod vector3d {

assert_eq!(result, vec3(2.0, 3.0, 5.0));
}

#[test]
pub fn test_swizzling() {
let p: Vector3D<i32> = vec3(1, 2, 3);
assert_eq!(p.xy(), vec2(1, 2));
assert_eq!(p.xz(), vec2(1, 3));
assert_eq!(p.yz(), vec2(2, 3));
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.