[Math] Calculating distance between two points on a circle in either radians or degrees

arc lengthcircles

I've asked this question a couple of times but I haven't been specific enough, I think, to get to the true answer. After spending a couple of weeks trying to get to the answer to this issue I finally have a real example of numbers I'm dealing with and the unexpected results I get using the various proposed mathematical solutions.

I'm building a game. In my game I have a character around which I am rotating a camera. The camera will rotate around the character at a fixed distance from the character. After the object has rotated around the character a variable number of degrees I want the camera to stop rotating. Here' is a visual representation of what I'm doing.

enter image description here

This image shows two circles. The character is at position $(x_3, y_3)$ and the camera rotates around the character along an imaginary circle. $(x_1, y_1)$ is the starting point of the camera and $(x_2, y_2)$ is the current position of the camera. I want to use this information to figure out the length of the red portion of the circle in either radians or degrees.

When I asked this question before (Calculating the distance in degrees between two points on a circle). It was suggested that I use the following process to do this:

Let the radius (distance from centre two two corrdinates) be $r$.

Let the distance from =$(x_1, y_1)$ to $(x_2, y_2)$ be represented by $c$.

$c=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$

If $x_1 < x_2$ (from Law of Cosines):

$\theta=\arccos\frac{2r^2-c^2}{2r^2}$

If $x1 > x2$:

$\theta=360-\arccos\frac{2r^2-c^2}{2r^2}$

In fact I implemented this method and it seemed to work great for a few of my tests so I marked the question as answered. However, after doing so I decided to play around with the number of degrees I wanted the camera to travel before stopping and soon realized it wasn't working fully. In fact, as the camera orbits around its center, this equation never returns a value larger than $136°$ before it starts to decrease. It certainly never reaches $180°$ – as it should.

Here is a set of real-world numbers I'm working with laid out graphically:

enter image description here

In this real example, the starting point for the camera is at $(-13.145792, 7.98047543)$. There are two points along the circle where I recorded the position as it rotated around the object: $point 1: (-14.6517525, 17.653318)$ and $point 2: (-8.312066, 14.7897005)$. The center point is the object around which I'm rotating. When I apply the aforementioned equation I don't get the results I am expecting. For $point 1$ I expect to get a value somewhere in between $135°$ and $180°$ but instead I get a ridiculous value of $358.600336634706$, which turns out to be $20546.2931677548°$. That's mainly because $x_1$ is $>$ $x_2$ so I'm subtracting the result I get from the $arccos$ from $360$ which is an obvious error in the equation since $arccos$ will return a result in radians and the $360$ is meant to compensate for a result in degrees. If I don't do that I at least get a value $< 360$ but still nowhere near the correct number since I end up with $80.1948322451861°$ which is way too low.

The last thing that I want to mention is that the distance between the position of the camera and the character should always stay the same if I'm rotating along a perfect circle, however, I'm not in control of this because the game engine is rotating my object for me; And one thing I have discovered is that the distance value between points on the circle and the center point is not always the same. In 3 examples I witnessed here are three different measurements I found for $r: 4.9999, 4.95677$ and $5.02961$. Again, I know that's not how circles work 🙂 but I'm not in control of that part of the process. However, given that the difference is very small I am hoping it won't prevent me from solving my problem without having to account for or offset the changing radius.

In case it's helpful, here's some code I'm using to concept this problem out and it's results. In this code I've converted the value I get from $arccos$ to degrees before I subtract the result from 360.

var x2 = -14.6517525;
var x1 = -13.145792;
var y2 = 17.653318;
var y1 = 7.98047543;
var r = 5.37358332;

var c = Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
Console.WriteLine("c = {0}", c);

double theta = Math.Acos((Math.Pow(2*r, 2) - Math.Pow(c, 2))/Math.Pow(2*r, 2));
var degrees = theta * 57.2958;

if (x1 > x2)
{
    degrees = 360 - degrees;
}

Console.WriteLine("θ = {0}", theta);
Console.WriteLine("° = {0}", degrees);

And here's the output of the code:

c = 9.7893718088325 
θ = 1.39966336529355 
° = 279.805167754814

Thank you math wizards for your help on this problem.

Best Answer

For point 1 from the bottom point, I find $r=5, c=8.350575, \frac {2r^2-c^2}{2r^2}=-0.394642, \theta = 113.2^\circ$ As your figure is drawn, I believe this is correct. The original line is almost vertical and the line to point 1 is definitely below $45^\circ$, so you should not expect an angle greater than $135^\circ$.

If I take a new point that is $3$ to the right of center and $4$ above it, so at $(-10.0042,16.978345)$ I get $c^2=90.83116$ and $\theta=144.7^\circ$, comfortably over $135^\circ$. The formula you have is fine.

An alternate approach, which may be easier, is to use the Atan2 function to compute the angles of the radii and subtract them. It deals with quadrants just fine.