2009-07-31 07:55:04 8 Comments

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.

### Related Questions

#### Sponsored Content

#### 30 Answered Questions

### [SOLVED] Is floating point math broken?

**2009-02-25 21:39:02****Cato Johnston****335095**View**3119**Score**30**Answer- Tags: math language-agnostic floating-point floating-accuracy

#### 54 Answered Questions

### [SOLVED] How do I check if an array includes a value in JavaScript?

**2008-10-25 22:14:40****brad****2683149**View**4133**Score**54**Answer- Tags: javascript arrays algorithm time-complexity javascript-objects

#### 42 Answered Questions

### [SOLVED] Calculate distance between two latitude-longitude points? (Haversine formula)

**2008-08-26 12:50:45****Robin Minto****821106**View**933**Score**42**Answer- Tags: algorithm math maps latitude-longitude haversine

#### 5 Answered Questions

### [SOLVED] How do I determine whether my calculation of pi is accurate?

**2013-01-11 17:15:32****Ishan Sharma****119980**View**773**Score**5**Answer- Tags: algorithm math language-agnostic pi

#### 20 Answered Questions

### [SOLVED] How do CSS triangles work?

**2011-08-16 03:54:51****Stanislav Shabalin****171648**View**1879**Score**20**Answer- Tags: css geometry polygon css-shapes

#### 36 Answered Questions

### [SOLVED] How can I pair socks from a pile efficiently?

**2013-01-19 15:34:35****amit****414925**View**3946**Score**36**Answer- Tags: algorithm sorting language-agnostic matching

#### 23 Answered Questions

### [SOLVED] Big O, how do you calculate/approximate it?

**2008-08-06 10:18:16****sven****435380**View**895**Score**23**Answer- Tags: algorithm optimization complexity-theory big-o performance

#### 10 Answered Questions

#### 1 Answered Questions

### [SOLVED] Calculate angle between 3D points of sphere with specific center

**2019-04-17 18:41:19****KNN****1294**View**1**Score**1**Answer- Tags: r math vector geometry coordinates

## 16 comments

## @James Penner 2018-05-30 07:52:30

there IS a simple answer for this using high school math..

Let say that you have 3 points

To get angle from point A to B

`angle = atan2(A.x - B.x, B.y - A.y)`

To get angle from point B to C

`angle2 = atan2(B.x - C.x, C.y - B.y)`

I just used this code in the recent project that I made, change the B to P1.. you might as well remove the "180 +" if you want

## @VeganEye 2017-06-15 10:44:49

well, the other answers seem to cover everything required, so I would like to just add this if you are using JMonkeyEngine:

`Vector3f.angleBetween(otherVector)`

as that is what I came here looking for :)

## @Dominic 2017-02-22 11:41:48

## @Minhas Kamal 2016-09-24 07:20:55

## 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), andO = (ox, oy). So for the lineOA-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) and`tan 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 (

ϴ)-## @Vikrant 2017-08-11 08:56:59

How can this method be used for an equilateral triangle ?

## @Vikrant 2017-08-17 11:37:12

Well , your answer is working fine now. It was some logic issue in my code week earlier.

## @Daniel Quinn 2016-10-14 20:44:53

}

## @Lance Roberts 2009-07-31 08:05:15

If you mean the angle that P1 is the vertex of then using the Law of Cosines should work:

where P

_{12}is the length of the segment from P1 to P2, calculated by## @Matt W 2012-02-15 09:26:46

mathwords.com/c/cosine_inverse.htm

## @geon 2014-10-21 13:27:39

@Rafa Firenze cos^-1 is a common notation for acos, but acos is less ambiguous. en.wikipedia.org/wiki/Inverse_trigonometric_functions

## @Lance Roberts 2014-10-21 14:00:24

I'll leave the edit since it doesn't hurt anything, but having Math/CS/EE degrees, cos^-1 is certainly the most common notation.

## @Michael Scheper 2015-03-03 20:55:29

Only a handful of languages use a caret for 'power of', so if you don't want to call it arcos, please just type cos⁻¹. (If you're using a commercial operating system that makes it difficult to type exponents, I expect there would be keycaps applications you could buy, or maybe a browser plug-in you could install. Or you can websearch and copy and paste.)

## @Lance Roberts 2015-03-03 22:43:01

@MichaelScheper, I was only using the caret in the comments where html is limited. I would certainly just use the sub/superscript notation in any actual answer.

## @phuclv 2016-03-24 10:16:48

it's

`arccos`

, not`arcos`

## @Lance Roberts 2016-03-25 03:05:47

@LưuVĩnhPhúc, yes, you're right, it was that way before the last edit. fixed.

## @Wilmer E. Henao 2016-06-13 15:08:02

Worth mentioning that this formula is derived from the "Law of Cosines". So people don't think that we're doing magic here :)

## @takluiper 2017-12-08 13:07:27

it works, but how you can find the sign of the angle?

## @Luc Boissaye 2018-03-07 15:16:06

Check atan2 solution below.

## @Paul 2016-06-25 04:32:32

Here's a C# method to return the angle (0-360) anticlockwise from the horizontal for a point on a circle.

Cheers, Paul

## @Adeem Maqsood Basraa 2010-07-26 13:58:22

In Objective-C you could do this by

Or read more here

## @Jim Balter 2015-04-09 05:08:25

Uh, no. There are three points, the center is not at (0,0), and this gives an angle of a right triangle, not the angle of the apex. And what sort of name is "xpoint" for an angle?

## @Marc 2010-03-22 05:45:20

I ran into a similar problem recently, only I needed to differentiate between a positive and negative angles. In case this is of use to anyone, I recommend the code snippet I grabbed from this mailing list about detecting rotation over a touch event for Android:

## @shaman.sir 2011-09-21 20:04:21

Let me give an example in JavaScript, I've fought a lot with that:

Bonus: Example with HTML5-canvas

## @Phrogz 2014-02-11 04:46:45

You can make this more efficient by doing less

`sqrt`

and squaring. See my answer here (written in Ruby), or in this updated demo (JavaScript).## @Luc Boissaye 2018-03-07 15:16:38

You could use atan2 for a simpler solution.

## @antonio 2011-11-30 07:35:50

Recently, I too have the same problem... In Delphi It's very similar to Objective-C.

## @6502 2015-07-10 07:24:41

The best way to deal with angle computation is to use

`atan2(y, x)`

that given a point`x, y`

returns the angle from that point and the`X+`

axis in respect to the origin.Given that the computation is

i.e. you basically translate the two points by

`-P1`

(in other words you translate everything so that`P1`

ends up in the origin) and then you consider the difference of the absolute angles of`P3`

and of`P2`

.The advantages of

`atan2`

is that the full circle is represented (you can get any number between -π and π) where instead with`acos`

you need to handle several cases depending on the signs to compute the correct result.The only singular point for

`atan2`

is`(0, 0)`

... meaning that both`P2`

and`P3`

must be different from`P1`

as in that case doesn't make sense to talk about an angle.## @Mario 2017-08-08 22:46:17

Thanks for your answer. That was exactly what I was looking for. Simple solution and you can get easily get the counter clockwise angle if I just add 2pi when the value is negative.

## @6502 2017-08-09 06:02:47

@marcpt:

`atan2`

is exactly what is needed for this problem, but looks like most people getting to this question just can't read or can't understand why`acos`

-based solution are bad. Luckily enough for me I left the "someone is wrong on the internet" (xkcd.com/386) phase many years ago and I'm not going to start a fight for defending the obvious :-)## @nicoco 2017-09-28 12:59:31

Thanks for pointing this out, but can you handle 3D this way?

## @6502 2017-09-28 15:00:37

@nicoco: in three dimensions how do you define the angle? More specifically can the angle be negative or more than pi (180 degrees)? Two non parallel vectors in 3d define a plane, but the plane can be "seen" from two sides: looked from one side A will appear "to the left" of B and from the other it will appear "to the right"...

## @nicoco 2017-09-28 15:11:27

@6505 Thanks for your answer, I posted before thinking my problem through. I got it figured out now, though.

## @Andre Miller 2009-07-31 08:00:26

Basically what you have is two vectors, one vector from P1 to P2 and another from P1 to P3. So all you need is an formula to calculate the angle between two vectors.

Have a look here for a good explanation and the formula.

## @Andrea Ambu 2009-08-30 15:56:05

It gets very simple if you think it as two vectors, one from point P1 to P2 and one from P1 to P3

so:

a = (p1.x - p2.x, p1.y - p2.y)

b = (p1.x - p3.x, p1.y - p3.y)

You can then invert the dot product formula:

to get the angle:

Remember that just means: a1*b1 + a2*b2 (just 2 dimensions here...)

## @Daniel Little 2015-03-16 01:04:17

Ah magnitude of the vector

## @Luc Boissaye 2018-03-07 15:15:51

Check the atan2 solution.

## @Treb 2009-07-31 08:08:00

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

## @Dominic 2017-02-22 10:58:58

I'm a bit confused how to actually implement this as you're treating P1 etc as individual values rather than (x,y)

## @Treb 2017-04-20 13:25:16

@Dominic Tobias: The Notation

`P1-P2 = A`

should not be read as "To calculate A, subtract P2 from P1", but as "I am defining A as the distance from P1 to P2", which can then be calculated using the second equation. I just wanted to define a shorthand for the distances, to make the equations more readable.## @peter.murray.rust 2009-08-30 15:29:22

You mentioned a signed angle (-90). In many applications angles may have signs (positive and negative, see http://en.wikipedia.org/wiki/Angle). If the points are (say) P2(1,0), P1(0,0), P3(0,1) then the angle P3-P1-P2 is conventionally positive (PI/2) whereas the angle P2-P1-P3 is negative. Using the lengths of the sides will not distinguish between + and - so if this matters you will need to use vectors or a function such as Math.atan2(a, b).

Angles can also extend beyond 2*PI and while this is not relevant to the current question it was sufficiently important that I wrote my own Angle class (also to make sure that degrees and radians did not get mixed up). The questions as to whether angle1 is less than angle2 depends critically on how angles are defined. It may also be important to decide whether a line (-1,0)(0,0)(1,0) is represented as Math.PI or -Math.PI