File tree Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 11pub mod vec2;
22pub mod vec3;
3+ pub mod vec4;
34
45use std:: ops:: { Add , Mul , Sub } ;
56
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments