Skip to content

Commit 0961a03

Browse files
committed
add 4D Vector Type
1 parent ece4ec6 commit 0961a03

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/vector/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod vec2;
22
pub mod vec3;
3+
pub mod vec4;
34

45
use std::ops::{Add, Mul, Sub};
56

src/vector/vec4.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use std::ops::{Add, Mul, Sub};
2+
3+
use super::VecMethods;
4+
5+
#[derive(Copy, Clone, Debug)]
6+
pub struct Vec4<T> {
7+
pub x: T,
8+
pub y: T,
9+
pub z: T,
10+
pub w: T,
11+
}
12+
13+
impl<T> Vec4<T> {
14+
pub const fn new(x: T, y: T, z: T, w: T) -> Self {
15+
Self { x, y, z, w }
16+
}
17+
}
18+
19+
impl<T> VecMethods<T> for Vec4<T>
20+
where
21+
T: Add<Output = T>,
22+
T: Mul<Output = T>,
23+
T: Sub<Output = T>,
24+
T: Copy,
25+
{
26+
fn sum(&self) -> T {
27+
self.x + self.y + self.z + self.w
28+
}
29+
30+
fn get_attenuation_factor(&self) -> T {
31+
(self.x * self.x) + (self.y * self.y) + (self.z * self.z) + (self.w * self.w)
32+
}
33+
}
34+
35+
impl<T: Copy> Vec4<T> {
36+
pub fn map<Y>(&self, f: impl Fn(T) -> Y) -> Vec4<Y> {
37+
Vec4 {
38+
x: f(self.x),
39+
y: f(self.y),
40+
z: f(self.z),
41+
w: f(self.w),
42+
}
43+
}
44+
}
45+
46+
impl<T> Sub<Vec4<T>> for Vec4<T>
47+
where
48+
T: Sub<Output = T>,
49+
{
50+
type Output = Vec4<T>;
51+
52+
fn sub(self, rhs: Self) -> Self::Output {
53+
Vec4 {
54+
x: self.x - rhs.x,
55+
y: self.y - rhs.y,
56+
z: self.z - rhs.z,
57+
w: self.w - rhs.w,
58+
}
59+
}
60+
}
61+
62+
impl<T: Add<Output = T>> Add<Vec4<T>> for Vec4<T> {
63+
type Output = Vec4<T>;
64+
65+
fn add(self, rhs: Self) -> Self::Output {
66+
Vec4 {
67+
x: self.x + rhs.x,
68+
y: self.y + rhs.y,
69+
z: self.z + rhs.z,
70+
w: self.w + rhs.w,
71+
}
72+
}
73+
}
74+
75+
impl<T> Mul<T> for Vec4<T>
76+
where
77+
T: Mul<Output = T>,
78+
T: Copy,
79+
{
80+
type Output = Vec4<T>;
81+
82+
fn mul(self, rhs: T) -> Self::Output {
83+
Vec4 {
84+
x: self.x * rhs,
85+
y: self.y * rhs,
86+
z: self.z * rhs,
87+
w: self.w * rhs,
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)