From cbde731b7407f55407f8b581be6d0e3cee6e74e0 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Sat, 10 Dec 2016 16:09:09 -0500 Subject: [PATCH] Dot product is not a calculation of angle. In my sleepy and sick state, I did not check on the actual return of the dot product. As such, I relied on what I had remembered it being. I had thought that it was the angle between two vectors, but this is wrong and that is a separate formula that uses the dot product to determine the angle in Euclidean space. So the change to Radians, and all the messy extra type definitions, were unneeded. The only operation that really needed the addition of the Radian type was the angle operation. --- src/vector.rs | 54 ++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/src/vector.rs b/src/vector.rs index 0cd97b9..22b9b5e 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -268,11 +268,10 @@ pub trait Vector: Debug + Display + Clone + Default + Zero + One /// /// let vector: Vector3 = Vector3::::from_value(3i32); /// let vector_two: Vector3 = Vector3::::from_value(3i32); - /// let dotProduct: Radian = vector.dot(&vector_two); - /// # assert_eq!(*dotProduct, 27f64); + /// let dotProduct: i64 = vector.dot(&vector_two); + /// # assert_eq!(dotProduct, 27i64); ///``` - fn dot(&self, vector: &Self) -> Radian - where R: Real + ::std::convert::From; + fn dot(&self, vector: &Self) -> T; } /// Defines the [EuclideanVector][1] trait. @@ -292,14 +291,12 @@ pub trait EuclideanVector : Vector where T: Real ///``` fn get_length(&self) -> T { - let sq_rt: T; - let length: Radian; + let length: T; - length = self.dot(self); - sq_rt = (*length).sqrt(); - assert!(sq_rt.is_finite()); + length = self.dot(self).sqrt(); + assert!(length.is_finite()); - sq_rt + length } /// Get the squared length of the Vector. @@ -314,10 +311,7 @@ pub trait EuclideanVector : Vector where T: Real ///``` fn get_length_squared(&self) -> T { - let radians: Radian; - - radians = self.dot(self); - *radians + self.dot(self) } /// Normalizes the Vector by multplying all the @@ -369,12 +363,9 @@ pub trait EuclideanVector : Vector where T: Real // TODO: Add an example here. fn is_perpendicular_to(&self, vector: &Self) -> bool { - let dot: T; - // TODO: Make this work with a fudge factor since floats // are tricky. - dot = *self.dot(vector); - dot == T::zero() + self.dot(vector) == T::zero() } /// Calculates the angle between this vector and @@ -538,12 +529,9 @@ macro_rules! define_vector perform_method_on_components!(mul, {$(self.$field),+}) } - fn dot(&self, vector: &$structName) -> Radian - where R: Real + ::std::convert::From + fn dot(&self, vector: &$structName) -> T { - let dot: R; - dot = R::from(self.mul(vector).get_sum()); - Radian::new(dot) + self.mul(vector).get_sum() } } @@ -699,13 +687,9 @@ impl Vector2 where T: Number /// Calculate the perpendicular dot product. // TODO: Add an example. - pub fn perpendicular_dot(&self, vector: &Vector2) -> Radian - where R: Real + ::std::convert::From + pub fn perpendicular_dot(&self, vector: &Vector2) -> T { - let val: R; - - val = R::from((self.x * vector.y) - (self.y * vector.x)); - Radian::new(val) + (self.x * vector.y) - (self.y * vector.x) } /// Adds a z component with the given value to the Vector. @@ -928,7 +912,8 @@ impl EuclideanVector for Vector2 where T: Real { fn angle(&self, vector: &Vector2) -> Radian { - Radian::atan2((*self.perpendicular_dot(vector)), (*self.dot(vector))) + // Shortcut. + Radian::atan2(self.perpendicular_dot(vector), self.dot(vector)) } } @@ -936,7 +921,8 @@ impl EuclideanVector for Vector3 where T: Real { fn angle(&self, vector: &Vector3) -> Radian { - Radian::atan2(self.cross(vector).get_length(), (*self.dot(vector))) + // Shortcut. + Radian::atan2(self.cross(vector).get_length(), self.dot(vector)) } } @@ -944,9 +930,7 @@ impl EuclideanVector for Vector4 where T: Real { fn angle(&self, vector: &Vector4) -> Radian { - let dot: T; - - dot = *self.dot(vector); - Radian::acos(dot / self.get_length() * vector.get_length()) + // The basic equation, this would work for any size Vector. + Radian::acos(self.dot(vector) / (self.get_length()*vector.get_length())) } }