aboutsummaryrefslogtreecommitdiff
path: root/src/core/vector3.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/vector3.rs')
-rw-r--r--src/core/vector3.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/core/vector3.rs b/src/core/vector3.rs
index 915765d..6f4d6ab 100644
--- a/src/core/vector3.rs
+++ b/src/core/vector3.rs
@@ -2,7 +2,8 @@
//!
//! Also add more 3d math things needed for shading and 3d calculations.
use crate::{Float, Number};
-use std::ops::{Sub, Add, DivAssign};
+use std::ops::{Mul, Sub, Add, DivAssign};
+use std::fmt;
#[derive(Clone, Copy)]
pub struct Vector3<T: Number> {
@@ -49,6 +50,17 @@ impl<T: Number> Add for Vector3<T> {
}
}
+impl<T: Number> Mul<T> for Vector3<T> {
+ type Output = Self;
+ fn mul(self, op: T) -> Self::Output {
+ Self::Output::new_xyz(
+ self.x * op,
+ self.y * op,
+ self.z * op,
+ )
+ }
+}
+
impl<T: Number> DivAssign<T> for Vector3<T> {
fn div_assign(&mut self, op: T) {
self.x /= op;
@@ -57,6 +69,12 @@ impl<T: Number> DivAssign<T> for Vector3<T> {
}
}
+impl<T: Number> fmt::Display for Vector3<T> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_fmt(format_args!("[{}, {}, {}]", self.x, self.y, self.z))
+ }
+}
+
impl Vector3f {
/// Calculates the length times itself
///
@@ -97,4 +115,13 @@ impl Vector3f {
new.norm_in();
new
}
+
+ pub fn cross(&self, op: &Self) -> Self {
+ Self::new_xyz(
+ self.y * op.z - self.z * op.y,
+ self.z * op.x - self.x * op.z,
+ self.x * op.y - self.y * op.x,
+ )
+
+ }
}