Lets say you have this:
P1 = (x=2, y=50)
P2 = (x=9, y=40)
P3 = (x=5, y=20)
Assume that P1
is the center point of a circle. It is always the same.
I want the angle that is made up by P2
and P3
, or in other words the angle that is next to P1
. The inner angle to be precise. It will always be an acute angle, so less than -90 degrees.
I thought: Man, that's simple geometry math. But I have looked for a formula for around 6 hours now, and only find people talking about complicated NASA stuff like arccos and vector scalar product stuff. My head feels like it's in a fridge.
Some math gurus here that think this is a simple problem? I don't think the programming language matters here, but for those who think it does: java and objective-c. I need it for both, but haven't tagged it for these.
Here's a C# method to return the angle (0-360) anticlockwise from the horizontal for a point on a circle.
Cheers, Paul
Very Simple Geometric Solution with Explanation
Few days ago, a fell into the same problem & had to sit with the math book. I solved the problem by combining and simplifying some basic formulas.
Lets consider this figure-
We want to know ϴ, so we need to find out α and β first. Now, for any straight line-
Let- A = (ax, ay), B = (bx, by), and O = (ox, oy). So for the line OA-
In the same way, for line OB-
Now, we need
ϴ = β - α
. In trigonometry we have a formula-After replacing the value of
tan α
(from eqn-2) andtan b
(from eqn-3) in eqn-4, and applying simplification we get-So,
That is it!
Now, take following figure-
This C# or, Java method calculates the angle (ϴ)-
In Objective-C you could do this by
Or read more here
If you are thinking of P1 as the center of a circle, you are thinking too complicated. You have a simple triangle, so your problem is solveable with the law of cosines. No need for any polar coordinate tranformation or somesuch. Say the distances are P1-P2 = A, P2-P3 = B and P3-P1 = C:
All you need to do is calculate the length of the distances A, B and C. Those are easily available from the x- and y-coordinates of your points and Pythagoras' theorem
Let me give an example in JavaScript, I've fought a lot with that:
Bonus: Example with HTML5-canvas
Recently, I too have the same problem... In Delphi It's very similar to Objective-C.