Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angle between points?

I have a triangle (A, B, C) and am trying to find the angle between each pair of the three points.

The problem is that the algorithms I can find online are for determining the angle between vectors. Using the vectors I would compute the angle between the vector that goes from (0, 0) to the point I have, and that doesn't give me the angles inside the triangle.

OK, here's some code in Python after the method on the Wikipedia page and after subtracting the values:

import numpy as np points = np.array([[343.8998, 168.1526], [351.2377, 173.7503], [353.531, 182.72]])  A = points[2] - points[0] B = points[1] - points[0] C = points[2] - points[1]  for e1, e2 in ((A, B), (A, C), (B, C)):     num = np.dot(e1, e2)     denom = np.linalg.norm(e1) * np.linalg.norm(e2)     print np.arccos(num/denom) * 180 

That gives me 60.2912487814, 60.0951900475 and 120.386438829, so what am I doing wrong?

like image 265
luct Avatar asked Feb 25 '11 20:02

luct


2 Answers

There are two errors here.

  • You missed a factor of π when translating from radians to degrees (it's × 180 / π)

  • You have to be careful about the signs of vectors, since they are directed line segments.

If I make these modifications I get a result that makes sense:

import numpy as np points = np.array([[343.8998, 168.1526], [351.2377, 173.7503], [353.531, 182.72]])  A = points[2] - points[0] B = points[1] - points[0] C = points[2] - points[1]  angles = [] for e1, e2 in ((A, B), (A, C), (B, -C)):     num = np.dot(e1, e2)     denom = np.linalg.norm(e1) * np.linalg.norm(e2)     angles.append(np.arccos(num/denom) * 180 / np.pi) print angles print sum(angles) 

which prints out

[19.191300537488704, 19.12889310421054, 141.67980635830079] 180.0 

I'd probably make things more symmetrical and use A, B, C vectors that are cyclic and sum to zero:

import numpy as np points = np.array([[343.8998, 168.1526], [351.2377, 173.7503], [353.531, 182.72]])  A = points[1] - points[0] B = points[2] - points[1] C = points[0] - points[2]  angles = [] for e1, e2 in ((A, -B), (B, -C), (C, -A)):     num = np.dot(e1, e2)     denom = np.linalg.norm(e1) * np.linalg.norm(e2)     angles.append(np.arccos(num/denom) * 180 / np.pi) print angles print sum(angles) 

which prints out

[141.67980635830079, 19.12889310421054, 19.191300537488704] 180.0 

The minus signs in the dot product come because we're trying to get the inside angles.

I'm sorry we drove you away in your time of need, by closing the question.

like image 73
Jason S Avatar answered Sep 21 '22 12:09

Jason S


I would use the law of cosines, since you can easily calculate the length of each side of the triangle and then solve for each angles individually.

like image 35
JoshAdel Avatar answered Sep 20 '22 12:09

JoshAdel