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.
This commit is contained in:
Myrddin Dundragon 2016-12-10 16:09:09 -05:00
parent 40f5c0f462
commit cbde731b74

View File

@ -268,11 +268,10 @@ pub trait Vector<T>: Debug + Display + Clone + Default + Zero + One
///
/// let vector: Vector3<i32> = Vector3::<i32>::from_value(3i32);
/// let vector_two: Vector3<i32> = Vector3::<i32>::from_value(3i32);
/// let dotProduct: Radian<f64> = vector.dot(&vector_two);
/// # assert_eq!(*dotProduct, 27f64);
/// let dotProduct: i64 = vector.dot(&vector_two);
/// # assert_eq!(dotProduct, 27i64);
///```
fn dot<R>(&self, vector: &Self) -> Radian<R>
where R: Real + ::std::convert::From<T>;
fn dot(&self, vector: &Self) -> T;
}
/// Defines the [EuclideanVector][1] trait.
@ -292,14 +291,12 @@ pub trait EuclideanVector<T> : Vector<T> where T: Real
///```
fn get_length(&self) -> T
{
let sq_rt: T;
let length: Radian<T>;
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<T> : Vector<T> where T: Real
///```
fn get_length_squared(&self) -> T
{
let radians: Radian<T>;
radians = self.dot(self);
*radians
self.dot(self)
}
/// Normalizes the Vector by multplying all the
@ -369,12 +363,9 @@ pub trait EuclideanVector<T> : Vector<T> 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<R>(&self, vector: &$structName<T>) -> Radian<R>
where R: Real + ::std::convert::From<T>
fn dot(&self, vector: &$structName<T>) -> 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<T> Vector2<T> where T: Number
/// Calculate the perpendicular dot product.
// TODO: Add an example.
pub fn perpendicular_dot<R>(&self, vector: &Vector2<T>) -> Radian<R>
where R: Real + ::std::convert::From<T>
pub fn perpendicular_dot(&self, vector: &Vector2<T>) -> 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<T> EuclideanVector<T> for Vector2<T> where T: Real
{
fn angle(&self, vector: &Vector2<T>) -> Radian<T>
{
Radian::atan2((*self.perpendicular_dot(vector)), (*self.dot(vector)))
// Shortcut.
Radian::atan2(self.perpendicular_dot(vector), self.dot(vector))
}
}
@ -936,7 +921,8 @@ impl<T> EuclideanVector<T> for Vector3<T> where T: Real
{
fn angle(&self, vector: &Vector3<T>) -> Radian<T>
{
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<T> EuclideanVector<T> for Vector4<T> where T: Real
{
fn angle(&self, vector: &Vector4<T>) -> Radian<T>
{
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()))
}
}