Added Debug, Display, and Default to Vectors and Quaternions.

This also adds an example for Vectors and Quaternions.
This commit is contained in:
Jason Travis Smith
2016-01-07 17:42:23 -05:00
parent 9306a73f9a
commit e64c3b0fbe
5 changed files with 128 additions and 2 deletions

View File

@ -3,6 +3,7 @@
//!
//! [1]: https://en.wikipedia.org/wiki/Quaternion
//! [2]: https://en.wikipedia.org/wiki/Dual_quaternion
use std::fmt::{Error, Formatter, Debug, Display};
use std::ops::{Add, Sub, Mul, Div, Neg};
use ::zero::Zero;
@ -275,6 +276,34 @@ impl<T> Quaternion<T> where T: Real
// TODO: Add binary operations for editing self.
}
// Implement the Debug trait for the Quaternion.
impl<T> Debug for Quaternion<T> where T: Real
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
write!(formatter, "[{:?}, {:?}]", self.scalar, self.vector)
}
}
// Implement the Display trait for the Quaternion.
impl<T> Display for Quaternion<T> where T: Real
{
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
{
write!(formatter, "[{}, {}]", self.scalar, self.vector)
}
}
// Implement a default value for Quaternions which
// returns the identity quaternion.
impl<T> Default for Quaternion<T> where T: Real
{
fn default() -> Quaternion<T>
{
Quaternion::identity()
}
}
// Implement the Zero and One traits for the Quaternion.
impl<T> Zero for Quaternion<T> where T: Real
{