angle_sc/vector2d.rs
1// Copyright (c) 2026 Ken Barker
2
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21//! The `vector2d` module contains 2D vector functions.
22
23/// 2D vector dot product function: a . b.
24///
25/// * `a_0`, `a_1` the first vector values.
26/// * `b_0`, `b_1` the second vector values.
27///
28/// * returns the dot product of the 2D vectors.
29#[must_use]
30pub fn dot_product(a_0: f64, a_1: f64, b_0: f64, b_1: f64) -> f64 {
31 a_0 * b_0 + a_1 * b_1
32}
33
34/// 2D vector perp product function: a x b.
35///
36/// * `a_0`, `a_1` the first vector values.
37/// * `b_0`, `b_1` the second vector values.
38///
39/// * returns the perp product of the 2D vectors.
40#[must_use]
41pub fn perp_product(a_0: f64, a_1: f64, b_0: f64, b_1: f64) -> f64 {
42 dot_product(a_0, a_1, b_1, -b_0)
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_dot_product() {
51 assert_eq!(1.0, dot_product(1.0, 0.0, 1.0, 0.0));
52 assert_eq!(0.0, dot_product(1.0, 0.0, 0.0, 1.0));
53 assert_eq!(0.0, dot_product(0.0, 1.0, 1.0, 0.0));
54 }
55
56 #[test]
57 fn test_perp_product() {
58 assert_eq!(0.0, perp_product(1.0, 0.0, 1.0, 0.0));
59 assert_eq!(1.0, perp_product(1.0, 0.0, 0.0, 1.0));
60 assert_eq!(-1.0, perp_product(0.0, 1.0, 1.0, 0.0));
61 }
62}