vec2/
norm.rs

1use core::ops::{Div, Mul};
2use num_traits::real::Real;
3
4use super::{len, sdiv};
5
6/// # Example
7/// ```
8/// let mut v = vec2::new_zero::<f32>();
9/// assert_eq!(vec2::norm(&mut v, &vec2::new_one()), 2_f32.sqrt());
10/// assert_eq!(v, [1_f32 / 2_f32.sqrt(); 2]);
11/// ```
12#[inline]
13pub fn norm<'out, T>(out: &'out mut [T; 2], b: &[T; 2]) -> T
14where
15    T: Real,
16    for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Div<&'b T, Output = T>,
17{
18    let s = len(b);
19    sdiv(out, b, &s);
20    s
21}
22
23/// # Example
24/// ```
25/// let mut v = vec2::new_one::<f32>();
26/// assert_eq!(vec2::norm_mut(&mut v), 2_f32.sqrt());
27/// assert_eq!(v, [1_f32 / 2_f32.sqrt(); 2]);
28/// ```
29#[inline]
30pub fn norm_mut<'out, T>(out: &'out mut [T; 2]) -> T
31where
32    T: Real,
33    for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Div<&'b T, Output = T>,
34{
35    let tmp = out.clone();
36    norm(out, &tmp)
37}