Skip to content

Commit

Permalink
Normalization for Point2D and Point3D
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Apr 20, 2017
1 parent 6d5dd25 commit 6aaa5de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "euclid"
version = "0.11.1"
version = "0.11.2"
authors = ["The Servo Project Developers"]
description = "Geometry primitives"
documentation = "https://docs.rs/euclid/"
Expand Down
56 changes: 56 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ where T: Copy + Mul<T, Output=T> + Add<T, Output=T> + Sub<T, Output=T> {
pub fn cross(self, other: TypedPoint2D<T, U>) -> T {
self.x * other.y - self.y * other.x
}

#[inline]
pub fn normalize(self) -> Self where T: Float + ApproxEq<T> {
let dot = self.dot(self);
if dot.approx_eq(&T::zero()) {
self
} else {
self / dot.sqrt()
}
}
}

impl<T: Copy + Add<T, Output=T>, U> Add for TypedPoint2D<T, U> {
Expand Down Expand Up @@ -386,6 +396,16 @@ impl<T: Mul<T, Output=T> +
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x)
}

#[inline]
pub fn normalize(self) -> Self where T: Float + ApproxEq<T> {
let dot = self.dot(self);
if dot.approx_eq(&T::zero()) {
self
} else {
self / dot.sqrt()
}
}
}

impl<T: Copy + Add<T, Output=T>, U> Add for TypedPoint3D<T, U> {
Expand Down Expand Up @@ -414,6 +434,22 @@ impl <T: Copy + Neg<Output=T>, U> Neg for TypedPoint3D<T, U> {
}
}

impl<T: Copy + Mul<T, Output=T>, U> Mul<T> for TypedPoint3D<T, U> {
type Output = Self;
#[inline]
fn mul(self, scale: T) -> Self {
Self::new(self.x * scale, self.y * scale, self.z * scale)
}
}

impl<T: Copy + Div<T, Output=T>, U> Div<T> for TypedPoint3D<T, U> {
type Output = Self;
#[inline]
fn div(self, scale: T) -> Self {
Self::new(self.x / scale, self.y / scale, self.z / scale)
}
}

impl<T: Float, U> TypedPoint3D<T, U> {
pub fn min(self, other: TypedPoint3D<T, U>) -> TypedPoint3D<T, U> {
TypedPoint3D::new(self.x.min(other.x),
Expand Down Expand Up @@ -800,6 +836,16 @@ mod point2d {
assert_eq!(r, -59.0);
}

#[test]
pub fn test_normalize() {
let p0: Point2D<f32> = Point2D::zero();
let p1: Point2D<f32> = Point2D::new(4.0, 0.0);
let p2: Point2D<f32> = Point2D::new(3.0, -4.0);
assert_eq!(p0.normalize(), p0);
assert_eq!(p1.normalize(), Point2D::new(1.0, 0.0));
assert_eq!(p2.normalize(), Point2D::new(0.6, -0.8));
}

#[test]
pub fn test_min() {
let p1 = Point2D::new(1.0, 3.0);
Expand Down Expand Up @@ -872,6 +918,16 @@ mod point3d {
assert_eq!(p3, Point3D::new(-51.0, 105.0, -59.0));
}

#[test]
pub fn test_normalize() {
let p0: Point3D<f32> = Point3D::zero();
let p1: Point3D<f32> = Point3D::new(0.0, -6.0, 0.0);
let p2: Point3D<f32> = Point3D::new(1.0, 2.0, -2.0);
assert_eq!(p0.normalize(), p0);
assert_eq!(p1.normalize(), Point3D::new(0.0, -1.0, 0.0));
assert_eq!(p2.normalize(), Point3D::new(1.0/3.0, 2.0/3.0, -2.0/3.0));
}

#[test]
pub fn test_min() {
let p1 = Point3D::new(1.0, 3.0, 5.0);
Expand Down

0 comments on commit 6aaa5de

Please sign in to comment.