I'd like to calculate the angle between two points on a circle, in which the radius of the circle is known, as are the coordinates of the two points and the center point. The problem is similar to this one, except I don't want to be limited to one point being at the 12-o'clock position.

Known:

p1 - starting point on circle

p2 - ending point on circle

c - center point on circle

r - radius of circle

Calculate:

central angle

The incomplete solution I came up with is to calculate the distance between the two points first (lets call them p1 and p2):

d = sqrt((x2 - x1)^2 + (y2 - y1)^2)

With that, the angle can be calculated as (r is radius of the circle):

angle = 2asin(d/2r)

The problem with this solution is that it doesn't produce angles over 180 degrees. When it goes over 180 degrees it starts giving the minor angle again, which leaves me unable to determine if the degrees it is giving are clockwise or counter clockwise from the starting point. (Here's a demo showing how to calculate the central angle, and it also runs into the same problem I encountered. It doesn't seem to know how to calculate an angle greater than 180 degrees.)

In other words, I always want this to calculate the angle clockwise from the starting point (p1) to the end point (p2), so that it produces angles up to 360 degrees.