add mean_angle

This commit is contained in:
Adrian Mariano 2025-03-17 21:02:31 -04:00
parent 8ae3a97277
commit 0593a784db

View file

@ -576,7 +576,7 @@ function constrain(v, minval, maxval) =
// Usage:
// mod = posmod(x, m)
// Description:
// Returns the positive modulo `m` of `x`. Value returned will be in the range 0 ... `m`-1.
// Returns the positive modulo `m` of `x`. Value returned will be satisfy `0 <= mod < m`.
// Arguments:
// x = The value to constrain.
// m = Modulo value.
@ -613,6 +613,31 @@ function modang(x) =
let(xx = posmod(x,360)) xx<180? xx : xx-360;
// Function: mean_angle()
// Synopsis: Returns the mean angle of two angles
// Topics: Math
// See Also: modang()
// Usage:
// half_ang = mean_angle(angle1,angle2);
// Description:
// Takes two angles (degrees) in any range and finds the angle halfway between
// the given angles, where halfway is interpreted using the shorter direction.
// In the case where the angles are exactly 180 degrees apart,
// it will return `angle1+90`. The returned angle is always in the interval [0,360).
// Arguments:
// angle1 = first angle
// angle2 = second angle
function mean_angle(angle1,angle2) =
assert(is_vector([angle1,angle2]), "Inputs must be finite numbers.")
let(
ang1 = posmod(angle1,360),
ang2 = posmod(angle2,360)
)
approx(abs(ang1-ang2),180) ? posmod(angle1+90,360)
: abs(ang1-ang2)<=180 ? (ang1+ang2)/2
: posmod((ang1+ang2-360)/2,360);
// Section: Operations on Lists (Sums, Mean, Products)