gfxmath_vec4/impls/mul/
mul.rs

1use crate::Vec4;
2use core::ops::Mul;
3
4/// ```
5/// use gfxmath_vec4::Vec4;
6/// 
7/// let mut a = Vec4::<f32>::new(1.5, 2.5, -2.0, 5.0);
8/// let b = Vec4::<f32>::new(4.0, 2.0,  4.0, 5.0);
9/// let c = Vec4::from(&a * &b);
10/// 
11/// a.x = 2.0;
12/// 
13/// assert_eq!( 6.0, c.x);
14/// assert_eq!( 5.0, c.y);
15/// assert_eq!(-8.0, c.z);
16/// assert_eq!(25.0, c.w);
17/// 
18/// let c = Vec4::from(a * b);
19///
20/// assert_eq!( 8.0, c.x);
21/// assert_eq!( 5.0, c.y);
22/// assert_eq!(-8.0, c.z);
23/// assert_eq!(25.0, c.w);
24/// ```
25#[opimps::impl_ops(Mul)]
26#[inline]
27fn mul<T>(self: Vec4<T>, rhs: Vec4<T>) -> Vec4<T> where T: Mul<Output = T> + Copy {
28    let l = self.as_slice();
29    let r = rhs.as_slice();
30    Vec4::new(
31        l[0] * r[0],
32        l[1] * r[1],
33        l[2] * r[2],
34        l[3] * r[3],
35    )
36}
37
38/// Scalar multiplication with vector
39/// 
40/// ```
41/// use gfxmath_vec4::Vec4;
42/// 
43/// let a = Vec4::<f32>::new(0.5, 2.5, -2.5, 4.0);
44/// let b = Vec4::from(a * 2.0);
45/// 
46/// assert_eq!( 1.0, b.x);
47/// assert_eq!( 5.0, b.y);
48/// assert_eq!(-5.0, b.z);
49/// assert_eq!( 8.0, b.w);
50/// ```
51#[opimps::impl_ops_rprim(Mul)]
52fn mul<T>(self: Vec4<T>, rhs: T) -> Vec4<T> where T: Mul<Output = T> + Copy {
53    return Vec4::new(self.x * rhs, self.y * rhs, self.z * rhs, self.w * rhs);
54}
55
56/// Scalar multiplication with vector
57/// 
58/// ```
59/// use gfxmath_vec4::Vec4;
60/// 
61/// let a = Vec4::<f32>::new(0.5, 2.5, -2.5, 4.0);
62/// let b = Vec4::from(2.0 * a);
63/// 
64/// assert_eq!( 1.0, b.x);
65/// assert_eq!( 5.0, b.y);
66/// assert_eq!(-5.0, b.z);
67/// assert_eq!( 8.0, b.w);
68/// ```
69#[opimps::impl_ops_lprim(Mul)]
70fn mul(self: f32, rhs: Vec4<f32>) -> Vec4<f32> {
71    return Vec4::new(self * rhs.x, self * rhs.y, self * rhs.z, self * rhs.w);
72}
73
74/// Scalar multiplication with vector
75/// 
76/// ```
77/// use gfxmath_vec4::Vec4;
78/// 
79/// let a = Vec4::<f64>::new(0.5, 2.5, -2.5, 4.0);
80/// let b = Vec4::from(2.0 * a);
81/// 
82/// assert_eq!( 1.0, b.x);
83/// assert_eq!( 5.0, b.y);
84/// assert_eq!(-5.0, b.z);
85/// assert_eq!( 8.0, b.w);
86/// ```
87#[opimps::impl_ops_lprim(Mul)]
88fn mul(self: f64, rhs: Vec4<f64>) -> Vec4<f64> {
89    return Vec4::new(self * rhs.x, self * rhs.y, self * rhs.z, self * rhs.w);
90}
91
92/// Scalar multiplication with vector
93/// 
94/// ```
95/// use gfxmath_vec4::Vec4;
96/// 
97/// let a = Vec4::<i32>::new(5, 2, -2, 4);
98/// let b = Vec4::from(2 * a);
99/// 
100/// assert_eq!( 10, b.x);
101/// assert_eq!( 4, b.y);
102/// assert_eq!(-4, b.z);
103/// assert_eq!( 8, b.w);
104/// ```
105#[opimps::impl_ops_lprim(Mul)]
106fn mul(self: i32, rhs: Vec4<i32>) -> Vec4<i32> {
107    return Vec4::new(self * rhs.x, self * rhs.y, self * rhs.z, self * rhs.w);
108}
109
110/// Scalar multiplication with vector
111/// 
112/// ```
113/// use gfxmath_vec4::Vec4;
114/// 
115/// let a = Vec4::<i64>::new(5, 2, -2, 4);
116/// let b = Vec4::from(2 * a);
117/// 
118/// assert_eq!( 10, b.x);
119/// assert_eq!(  4, b.y);
120/// assert_eq!( -4, b.z);
121/// assert_eq!(  8, b.w);
122/// ```
123#[opimps::impl_ops_lprim(Mul)]
124fn mul(self: i64, rhs: Vec4<i64>) -> Vec4<i64> {
125    return Vec4::new(self * rhs.x, self * rhs.y, self * rhs.z, self * rhs.w);
126}
127// End of scalar multiplication