motion/formulas/dot.rs
1use crate::vec::vec_2d::Vec2d;
2
3use super::sqrt::sqrt;
4
5/// Calculates the dot product of two 2D vectors.
6///
7/// # Examples
8///
9/// ```
10/// let u = Vec2d::new(3.0, 4.0);
11/// let v = Vec2d::new(1.0, 2.0);
12/// let result = dot(u, v);
13/// assert_eq!(result, 11.0);
14/// ```
15///
16/// # Parameters
17///
18/// - `u`: The first vector.
19/// - `v`: The second vector.
20///
21/// # Returns
22///
23/// The dot product of the two vectors.
24#[must_use]
25pub fn dot(u: Vec2d, v: Vec2d) -> f32 {
26 u.x * v.x + u.y * v.y
27}
28
29/// Calculates the squared length of a 2D vector.
30///
31/// # Examples
32///
33/// ```
34/// let u = Vec2d::new(3.0, 4.0);
35/// let result = length2(u);
36/// assert_eq!(result, 25.0);
37/// ```
38///
39/// # Parameters
40///
41/// - `u`: The vector to calculate the squared length for.
42///
43/// # Returns
44///
45/// The squared length of the vector.
46#[must_use]
47pub fn length2(u: Vec2d) -> f32 {
48 dot(u, u)
49}
50
51/// Calculates the length (magnitude) of a 2D vector.
52///
53/// # Examples
54///
55/// ```
56/// let u = Vec2d::new(3.0, 4.0);
57/// let result = length(u);
58/// assert_eq!(result, 5.0);
59/// ```
60///
61/// # Parameters
62///
63/// - `u`: The vector to calculate the length for.
64///
65/// # Returns
66///
67/// The length of the vector.
68#[must_use]
69pub fn length(u: Vec2d) -> f32 {
70 sqrt(length2(u))
71}