I found a somewhat bloated solution by doing some additional calculations. The name of the game is to check if P2 lies to the left or right (relative) to the line formed by P1 and the opposite side of the circle. To test this condition, you need to create a third point (we'll call it Pa) that lies anywhere along the starting heading:
1. Create a point somewhere on the heading line, an arbitrary distance d. Take heading, h, and subtract 180 degrees:
h1 = h - 180
Calculate Pa (x, y) using:
xa = x1 - d cos h1
ya = y1 - d sin h1
2. Calculate the lengths of all three lines in the triangle formed by P1-P2-Pa:
a = sqrt( (x2 - x1)^2 + (y2 - y1)^2) )
b = sqrt( (xa - x1)^2 + (ya - y1)^2) )
c = sqrt( (x2 - xa)^2 + (y2 - ya)^2) )
3. Now take the
law of cosines and rearange the variables to isolate the angle C:
C = acos( (c^2 - a^2 - b^2) / (-2ab) )
4. This generates an angle C. If the angle C is acute, then just use the regular value generated by the equations in my first post above (angle, a value 0-180). If the angle C is obtuse, then use the calculation (360 - angle) to generate values > 180.
I find this pretty tedious, so if anyone has a more elegant solution to derive angles 0 to 360 without all this mumbo-jumbo that would be great.