Skip to content

Improvements #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add some useful functions to the Vec type
  • Loading branch information
swiftcoder committed Jan 18, 2021
commit 6ca10556a0e62e67928a9ce6da697b03e12ccd8b
47 changes: 47 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ impl Vec3 {
z: 1.0,
}
}

/// Create a vector by taking the absolute value of each component in this vector
pub fn abs(&self) -> Self {
Self {
x: self.x.abs(),
y: self.y.abs(),
z: self.z.abs(),
}
}

/// Sum all of the components in this vector
pub fn component_sum(&self) -> f32 {
self.x + self.y + self.z
}

/// Find the maximum value out of all components in this vector
pub fn component_max(&self) -> f32 {
self.x.max(self.y.max(self.z))
}

/// Find the minimum value out of all components in this vector
pub fn component_min(&self) -> f32 {
self.x.min(self.y.min(self.z))
}
}

impl std::ops::Add for Vec3 {
Expand Down Expand Up @@ -81,6 +105,13 @@ impl std::ops::Mul<f32> for Vec3 {
Vec3::new(self.x * other, self.y * other, self.z * other)
}
}
impl std::ops::Mul<Vec3> for f32 {
type Output = Vec3;

fn mul(self, other: Vec3) -> Vec3 {
Vec3::new(self * other.x, self * other.y, self * other.z)
}
}

impl std::ops::Div for Vec3 {
type Output = Vec3;
Expand All @@ -89,3 +120,19 @@ impl std::ops::Div for Vec3 {
Vec3::new(self.x / other.x, self.y / other.y, self.z / other.z)
}
}

impl std::ops::Div<f32> for Vec3 {
type Output = Vec3;

fn div(self, other: f32) -> Vec3 {
Vec3::new(self.x / other, self.y / other, self.z / other)
}
}

impl std::ops::Div<Vec3> for f32 {
type Output = Vec3;

fn div(self, other: Vec3) -> Vec3 {
Vec3::new(self / other.x, self / other.y, self / other.z)
}
}
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