Skip to main content

Vector4

Struct Vector4 

Source
pub struct Vector4<T> {
    pub x: T,
    pub y: T,
    pub z: T,
    pub w: T,
}

Fields§

§x: T§y: T§z: T§w: T

Implementations§

Source§

impl<T> Vector4<T>

Source

pub fn new(x: T, y: T, z: T, w: T) -> Self

Source

pub fn length(&self) -> f64
where T: Into<f64> + Copy,

Examples found in repository?
examples/vector_operations.rs (line 59)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}
Source

pub fn dot(&self, other: &Self) -> f64
where T: Into<f64> + Copy,

Examples found in repository?
examples/vector_operations.rs (line 66)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}
Source

pub fn cross(&self, other: &Self) -> Vector4<f64>
where T: Into<f64> + Copy,

Examples found in repository?
examples/vector_operations.rs (line 73)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}
Source

pub fn normalize(&self) -> Self
where T: Into<f64> + Copy + From<f64>,

Source

pub fn project_onto(&self, other: &Self) -> Self
where T: Into<f64> + Copy + From<f64> + Mul<Output = T> + Add<Output = T> + Div<Output = T>,

Examples found in repository?
examples/vector_operations.rs (line 80)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}
Source

pub fn reject_from(&self, other: &Self) -> Self
where T: Into<f64> + Copy + From<f64> + Mul<Output = T> + Add<Output = T> + Div<Output = T> + Sub<Output = T>,

Source

pub fn lerp(&self, other: &Self, t: f64) -> Self
where T: Into<f64> + Copy + From<f64> + Add<Output = T> + Sub<Output = T> + Mul<f64, Output = T>,

Examples found in repository?
examples/vector_operations.rs (line 87)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}
Source

pub fn angle_between(&self, other: &Self) -> f64
where T: Into<f64> + Copy,

Examples found in repository?
examples/vector_operations.rs (line 112)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}
Source

pub fn swizzle(&self, x: usize, y: usize, z: usize, w: usize) -> Self
where T: Copy,

Examples found in repository?
examples/vector_operations.rs (line 97)
3fn main() {
4    // Example usage
5    let v2_a = Vector2 { x: 1.0, y: 2.0 };
6    let v2_b = Vector2 { x: 3.0, y: 4.0 };
7    let v3_a = Vector3 {
8        x: 1.0,
9        y: 2.0,
10        z: 3.0,
11    };
12    let v3_b = Vector3 {
13        x: 4.0,
14        y: 5.0,
15        z: 6.0,
16    };
17    let v4_a = Vector4 {
18        x: 1.0,
19        y: 2.0,
20        z: 3.0,
21        w: 4.0,
22    };
23    let v4_b = Vector4 {
24        x: 5.0,
25        y: 6.0,
26        z: 7.0,
27        w: 8.0,
28    };
29
30    println!("========================================");
31
32    println!("v2_a + v2_b = {:?}", v2_a + v2_b);
33    println!("v3_a + v3_b = {:?}", v3_a + v3_b);
34    println!("v4_a + v4_b = {:?}", v4_a + v4_b);
35
36    println!("========================================");
37
38    println!("v2_a - v2_b = {:?}", v2_a - v2_b);
39    println!("v3_a - v3_b = {:?}", v3_a - v3_b);
40    println!("v4_a - v4_b = {:?}", v4_a - v4_b);
41
42    println!("========================================");
43
44    println!("v2_a * v2_b = {:?}", v2_a - v2_b);
45    println!("v3_a * v3_b = {:?}", v3_a - v3_b);
46    println!("v4_a * v4_b = {:?}", v4_a - v4_b);
47
48    println!("========================================");
49
50    println!("v2_a / v2_b = {:?}", v2_a / v2_b);
51    println!("v3_a / v3_b = {:?}", v3_a / v3_b);
52    println!("v4_a / v4_b = {:?}", v4_a / v4_b);
53
54    println!("========================================");
55
56    // Example of length calculation
57    println!("Length of Vector2: {}", v2_a.length());
58    println!("Length of Vector3: {}", v3_a.length());
59    println!("Length of Vector4: {}", v4_a.length());
60
61    println!("========================================");
62
63    // Example of dot product calculation
64    println!("Dot Product of Vector2: {}", v2_a.dot(&v2_b));
65    println!("Dot Product of Vector3: {}", v3_a.dot(&v3_b));
66    println!("Dot Product of Vector4: {}", v4_a.dot(&v4_b));
67
68    println!("========================================");
69
70    // Example of cross product calculation
71    println!("Cross Product of Vector2: {:?}", v2_a.cross(&v2_a));
72    println!("Cross Product of Vector3: {:?}", v3_a.cross(&v3_b));
73    println!("Cross Product of Vector4: {:?}", v4_a.cross(&v4_b));
74
75    println!("========================================");
76
77    // Example of projection
78    println!("Projection onto of Vector2: {:?}", v2_a.project_onto(&v2_b));
79    println!("Projection onto of Vector3: {:?}", v3_a.project_onto(&v3_b));
80    println!("Projection onto of Vector4: {:?}", v4_a.project_onto(&v4_b));
81
82    println!("========================================");
83
84    // Example of Linear Interpolation
85    println!("Lerp for Vector2 (t = 0.5): {:?}", v2_a.lerp(&v2_b, 0.5));
86    println!("Lerp for Vector3 (t = 0.5): {:?}", v3_a.lerp(&v3_b, 0.5));
87    println!("Lerp for Vector4 (t = 0.5): {:?}", v4_a.lerp(&v4_b, 0.5));
88
89    println!("Lerp for Vector2 (t = 0.0): {:?}", v2_a.lerp(&v2_b, 0.0)); // Should return v2_a
90    println!("Lerp for Vector3 (t = 1.0): {:?}", v3_a.lerp(&v3_b, 1.0)); // Should return v3_b
91
92    println!("========================================");
93
94    // Example of Swizzling
95    println!("Swizzled Vector2 (yx): {:?}", v2_a.swizzle(1, 0)); // Swap x and y
96    println!("Swizzled Vector3 (yxz): {:?}", v3_a.swizzle(1, 0, 2)); // Swap x and y
97    println!("Swizzled Vector4 (wzyx): {:?}", v4_a.swizzle(3, 2, 1, 0)); // Reverse order
98
99    println!("========================================");
100
101    // Example of Angle Between Vectors
102    println!(
103        "Angle between Vector2 (radians): {}",
104        v2_a.angle_between(&v2_b)
105    );
106    println!(
107        "Angle between Vector3 (radians): {}",
108        v3_a.angle_between(&v3_b)
109    );
110    println!(
111        "Angle between Vector4 (radians): {}",
112        v4_a.angle_between(&v4_b)
113    );
114
115    println!("========================================");
116}

Trait Implementations§

Source§

impl<T> Add for Vector4<T>
where T: Add<Output = T>,

Source§

type Output = Vector4<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Clone> Clone for Vector4<T>

Source§

fn clone(&self) -> Vector4<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Vector4<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div for Vector4<T>
where T: Div<Output = T>,

Source§

type Output = Vector4<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> Mul for Vector4<T>
where T: Mul<Output = T>,

Source§

type Output = Vector4<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: PartialEq> PartialEq for Vector4<T>

Source§

fn eq(&self, other: &Vector4<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Sub for Vector4<T>
where T: Sub<Output = T>,

Source§

type Output = Vector4<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Copy> Copy for Vector4<T>

Source§

impl<T> StructuralPartialEq for Vector4<T>

Auto Trait Implementations§

§

impl<T> Freeze for Vector4<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Vector4<T>
where T: RefUnwindSafe,

§

impl<T> Send for Vector4<T>
where T: Send,

§

impl<T> Sync for Vector4<T>
where T: Sync,

§

impl<T> Unpin for Vector4<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Vector4<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Vector4<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.