From 0593a784dba4705312cd9d45ec1ee22f5b9b8567 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Mon, 17 Mar 2025 21:02:31 -0400 Subject: [PATCH] add mean_angle --- math.scad | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/math.scad b/math.scad index ffc11794..8d5d1e29 100644 --- a/math.scad +++ b/math.scad @@ -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)