Skip to content

Commit 6ca1055

Browse files
committed
Add some useful functions to the Vec type
1 parent 0585a7e commit 6ca1055

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/math.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ impl Vec3 {
4848
z: 1.0,
4949
}
5050
}
51+
52+
/// Create a vector by taking the absolute value of each component in this vector
53+
pub fn abs(&self) -> Self {
54+
Self {
55+
x: self.x.abs(),
56+
y: self.y.abs(),
57+
z: self.z.abs(),
58+
}
59+
}
60+
61+
/// Sum all of the components in this vector
62+
pub fn component_sum(&self) -> f32 {
63+
self.x + self.y + self.z
64+
}
65+
66+
/// Find the maximum value out of all components in this vector
67+
pub fn component_max(&self) -> f32 {
68+
self.x.max(self.y.max(self.z))
69+
}
70+
71+
/// Find the minimum value out of all components in this vector
72+
pub fn component_min(&self) -> f32 {
73+
self.x.min(self.y.min(self.z))
74+
}
5175
}
5276

5377
impl std::ops::Add for Vec3 {
@@ -81,6 +105,13 @@ impl std::ops::Mul<f32> for Vec3 {
81105
Vec3::new(self.x * other, self.y * other, self.z * other)
82106
}
83107
}
108+
impl std::ops::Mul<Vec3> for f32 {
109+
type Output = Vec3;
110+
111+
fn mul(self, other: Vec3) -> Vec3 {
112+
Vec3::new(self * other.x, self * other.y, self * other.z)
113+
}
114+
}
84115

85116
impl std::ops::Div for Vec3 {
86117
type Output = Vec3;
@@ -89,3 +120,19 @@ impl std::ops::Div for Vec3 {
89120
Vec3::new(self.x / other.x, self.y / other.y, self.z / other.z)
90121
}
91122
}
123+
124+
impl std::ops::Div<f32> for Vec3 {
125+
type Output = Vec3;
126+
127+
fn div(self, other: f32) -> Vec3 {
128+
Vec3::new(self.x / other, self.y / other, self.z / other)
129+
}
130+
}
131+
132+
impl std::ops::Div<Vec3> for f32 {
133+
type Output = Vec3;
134+
135+
fn div(self, other: Vec3) -> Vec3 {
136+
Vec3::new(self / other.x, self / other.y, self / other.z)
137+
}
138+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy