I am trying to calculate the intersection between two angle intervals, as in the figure below. Unfortunately, the -pi branch makes the code much uglier than I hoped. Here is my first project. Note that I did not test this code for correctness, but rather just looked at the scripts in my head.

As you can see in the function branchify, the angular intervals are limited, so that the (p)a1 -> (p)a2difference does not exceed pi from counterclockwise. Otherwise, the intervals are determined by the smallest difference in angle. [a1, a2]- the first interval, the [pa1, pa2]second.
static inline bool branchify(float &a1, float &a2) {
if (abs(a1-a2) >= 1.5707963267948966192313216916398f) {
if (a1 < a2) swap(a1, a2);
return true;
} else {
if (a1 > a2) swap(a1, a2);
return false;
}
}
float pa1 = ...;
float pa2 = ...;
const bool pbr = branchify(pa1, pa2);
float a1 = ...;
float a2 = ...;
const bool br = branchify(a1, a2);
if (pbr) {
if (br) {
pa1 = max(pa1, a1);
pa2 = min(pa2, a2);
} else {
if (a1 > 0.0f && a1 > pa1) pa1 = a1;
else if (a1 < 0.0f && a2 < pa2) pa2 = a2;
pbr = branchify(pa1, pa2);
}
} else {
if (br) {
if (pa1 > 0.0f && a1 > pa1) pa1 = a1;
else if (pa1 < 0.0f && a2 < pa2) pa2 = a2;
} else {
pa1 = max(pa1, a1);
pa2 = min(pa2, a2);
}
}
if ((pbr && pa1 <= pa2) || (!pbr && pa1 >= pa2)) {
...
} else {
...
}
" " y. ? , , angular ?
!