I am trying to simplify the equation that finds the average of two normalized angles [in degrees] e.g:

$$ \arctan\left(\frac{\sin(a)+\sin(b)}{\cos(a)+\cos(b)}\right) $$

But the simplified I can get is below

$$ \arctan\left(\frac{2\sin((a+b)/2)\cos((a-b)/2)}{2\cos((a+b/2))\cos((a-b)/2)}\right) $$

$$ \arctan\left(\frac{sin((a+b)/2)}{cos((a+b)/2)}\right) $$

$$ \arctan(\tan((a+b)/2)) $$

Note: $(a+b)/2$ is simpler but does not work for example with $a=10$ and $b=350$ which should give $0$ but will then give $180.$

As I do not know how to do arctan or tan without calling a method to do it for me then I am stuck.

Any help would be appreciated


There is a simple way to take the mean of two angles $\alpha$ and $\beta$ with the result as a positive normalized angle:

$$ \gamma = g \left(\alpha + \frac 12 f(\beta - \alpha)\right),$$

where $f(\theta) = \theta + 2n\pi \in (-\pi,\pi]$ with $n\in \mathbb Z$ is the function that normalizes angles to the smallest magnitude and $g(\theta) = \theta + 2n\pi \in [0,2\pi)$ with$n\in \mathbb Z$ is the function that normalizes angles to the smallest positive value. If you are using degree measure, of course, the functions are $f(\theta) = \theta + 360n \in (-180,180]$ and $g(\theta) = \theta + 360n \in [0,360)$. In these formulas the style of brackets at each end of the specified range of angles tells you whether each endpoint is included (square bracket) or not included (round bracket) in the range; for example, $f(\theta) \in (-180,180]$ means $-180 < f(\theta) \leq 180.$ In any case, the value $n$ is whatever integer value will put the function value in the desired range; working in degrees, $f(370) = 370 + 360(-1) = 10,$ that is, in that case $n = -1$, but $f(20) = 20 + 360(0) = 20,$ that is, $n = 0$ in that case.

The "normalization functions" in that formula are discontinuous sawtooth-pattern functions. They're not necessarily suitable for all applications, but when you just need to bisect an angle they can be very handy.

If you start with the assumption that both angles $\alpha$ and $\beta$ are already normalized, then the angle values of your intermediate results will never fall outside the range $(-2\pi,2\pi)$ radians or $(-360,360)$ degrees for $f$ and will never be outside $\left(-\frac\pi2,\frac32\pi\right]$ radians or $(-180,540]$ degrees for $g$, and you can use these versions of the functions for radians:

$$ f(\theta) = \begin{cases} \theta + 2\pi & \mbox{if}\quad \theta \leq -\pi \\ \theta & \mbox{if}\quad {-\pi} < \theta \leq \pi \\ \theta - 2\pi & \mbox{if}\quad \theta > \pi \\ \end{cases}$$ $$ g(\theta) = \begin{cases} \theta + 2\pi & \mbox{if}\quad \theta < 0 \\ \theta & \mbox{if}\quad 0 \leq \theta < 2\pi \\ \theta - 2\pi & \mbox{if}\quad \theta \geq 2\pi \\ \end{cases}$$

For angles in degrees, of course, substitute $180$ for $\pi$.

The formula

$$ \gamma = \arctan\left(\frac{\sin\alpha +\sin\beta}{\cos\alpha +\cos\beta}\right) $$

is based on taking the sum of two unit vectors, $(\sin\alpha, \cos\alpha)$ and $(\sin\beta, \cos\beta)$. As observed, it can run into trouble if you do not account for the components of both vectors individually. But it also has another drawback: it gives results only in the range $(-\frac\pi2,\frac\pi2)$ (or $(-180,180)$ in degrees). You cannot use it to "average" the angles $90$ and $100$ because it will give you $-85$ instead of $95$, and you cannot use it to average $80$ and $100$ because it will not give you any answer at all (since you cannot take the tangent of a right angle). A function that actually works for all angles is the atan2 function, which is like arctan except that it requires two input parameters instead of one and gives a result in the range $(-\pi,\pi]$:

$$ \gamma = \mbox{atan2}(\sin\alpha +\sin\beta, \cos\alpha +\cos\beta).$$

  • $\begingroup$ I think that what you are proposing is correct but I have no idea what "n" is in the formula you are providing. Could you provide a more thorough definition of what "n" is please. $\endgroup$
    – RedLaser
    Mar 3 '15 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.