rs_math/vector/
vector_3d.rs

1#[derive(Debug)]
2pub struct Vector3D {
3    pub x: f64,
4    pub y: f64,
5    pub z: f64,
6}
7
8impl Vector3D {
9    /// 向量加法操作。
10    ///
11    /// # 参数
12    ///
13    /// - `other`: 另一个向量。
14    ///
15    /// # 返回值
16    ///
17    /// 返回两个向量相加的结果,结果是一个新的 `Vector3D` 实例。
18    ///
19    /// # 示例
20    ///
21    /// ```
22    /// use rs_math::vector::vector_3d::Vector3D;
23    ///
24    /// let vector1 = Vector3D { x: 1.0, y: 2.0, z: 3.0 };
25    /// let vector2 = Vector3D { x: 4.0, y: 5.0, z: 6.0 };
26    /// let result = vector1.add(&vector2);
27    /// ```
28    pub fn add(&self, other: &Vector3D) -> Vector3D {
29        Vector3D {
30            x: self.x + other.x,
31            y: self.y + other.y,
32            z: self.z + other.z,
33        }
34    }
35
36    /// 向量减法操作。
37    ///
38    /// # 参数
39    ///
40    /// - `other`: 被减的向量。
41    ///
42    /// # 返回值
43    ///
44    /// 返回两个向量相减的结果,结果是一个新的 `Vector3D` 实例。
45    ///
46    /// # 示例
47    ///
48    /// ```
49    /// use rs_math::vector::vector_3d::Vector3D;
50    ///
51    /// let vector1 = Vector3D { x: 1.0, y: 2.0, z: 3.0 };
52    /// let vector2 = Vector3D { x: 4.0, y: 5.0, z: 6.0 };
53    /// let result = vector1.subtract(&vector2);
54    /// ```
55    pub fn subtract(&self, other: &Vector3D) -> Vector3D {
56        Vector3D {
57            x: self.x - other.x,
58            y: self.y - other.y,
59            z: self.z - other.z,
60        }
61    }
62
63    /// 数量乘法操作。
64    ///
65    /// # 参数
66    ///
67    /// - `scalar`: 乘以向量的标量。
68    ///
69    /// # 返回值
70    ///
71    /// 返回数量乘法的结果,结果是一个新的 `Vector3D` 实例。
72    ///
73    /// # 示例
74    ///
75    /// ```
76    /// use rs_math::vector::vector_3d::Vector3D;
77    ///
78    /// let vector = Vector3D { x: 1.0, y: 2.0, z: 3.0 };
79    /// let scalar = 2.0;
80    /// let result = vector.scalar_multiply(scalar);
81    /// ```
82    pub fn scalar_multiply(&self, scalar: f64) -> Vector3D {
83        Vector3D {
84            x: self.x * scalar,
85            y: self.y * scalar,
86            z: self.z * scalar,
87        }
88    }
89
90    /// 计算两个向量的叉积。
91    ///
92    /// # 参数
93    ///
94    /// - `other`: 另一个向量。
95    ///
96    /// # 返回值
97    ///
98    /// 返回两个向量的叉积,结果是一个新的 `Vector3D` 实例。
99    ///
100    /// # 示例
101    ///
102    /// ```
103    /// use rs_math::vector::vector_3d::Vector3D;
104    ///
105    /// let vector1 = Vector3D { x: 1.0, y: 2.0, z: 3.0 };
106    /// let vector2 = Vector3D { x: 4.0, y: 5.0, z: 6.0 };
107    /// let cross_product = vector1.cross_product(&vector2);
108    /// ```
109    pub fn cross_product(&self, other: &Vector3D) -> Vector3D {
110        Vector3D {
111            x: self.y * other.z - self.z * other.y,
112            y: self.z * other.x - self.x * other.z,
113            z: self.x * other.y - self.y * other.x,
114        }
115    }
116
117    /// 计算两个向量的点积。
118    ///
119    /// # 参数
120    ///
121    /// - `other`: 另一个向量。
122    ///
123    /// # 返回值
124    ///
125    /// 返回两个向量的点积。
126    ///
127    /// # 示例
128    ///
129    /// ```
130    /// use rs_math::vector::vector_3d::Vector3D;
131    ///
132    /// let vector1 = Vector3D { x: 1.0, y: 2.0, z: 3.0 };
133    /// let vector2 = Vector3D { x: 4.0, y: 5.0, z: 6.0 };
134    /// let dot_product = vector1.dot_product(&vector2);
135    /// ```
136    pub fn dot_product(&self, other: &Vector3D) -> f64 {
137        self.x * other.x + self.y * other.y + self.z * other.z
138    }
139    /// 计算向量的模长。
140    ///
141    /// # 返回值
142    ///
143    /// 返回向量的模长。
144    ///
145    /// # 示例
146    ///
147    /// ```
148    /// use rs_math::vector::vector_3d::Vector3D;
149    ///
150    /// let vector = Vector3D { x: 3.0, y: 4.0, z: 5.0 };
151    /// let magnitude = vector.magnitude();
152    /// ```
153    pub fn magnitude(&self) -> f64 {
154        (self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
155    }
156    /// 计算两个向量之间的夹角(弧度)。
157    ///
158    /// # 参数
159    ///
160    /// - `other`:要计算夹角的另一个向量。
161    ///
162    /// # 返回值
163    ///
164    /// 返回两个向量之间的夹角(弧度)。
165    ///
166    /// # 示例
167    ///
168    /// ```
169    /// use rs_math::vector::vector_3d::Vector3D;
170    ///
171    /// let vector1 = Vector3D { x: 3.0, y: 2.0, z: 1.0 };
172    /// let vector2 = Vector3D { x: 1.0, y: 4.0, z: 2.0 };
173    /// let angle = vector1.angle_between(&vector2);
174    /// ```
175    pub fn angle_between(&self, other: &Vector3D) -> f64 {
176        let dot_product = self.dot_product(other);
177        let magnitude_product = self.magnitude() * other.magnitude();
178
179        dot_product.acos() / magnitude_product
180    }
181}