views:

83

answers:

2

Lets say I am any two points on 2d plane (p1(x1,y1), p2(x2,y1)) and both points forms a line with the center(c(c1,c2)). Therefore I am two lines end at same point. I want to know how can I calculate angle between those two lines. I want to be able to show angle range from 0-2pi. Also 0-(-2pi) which let the line form by p1 and c to be line 1 and other line 2. I do have some idea by using atan2() but did not work out like I want it to. Thanks

+1  A: 

Convert the points to vectors (subtract the center point from each end point) and use the dot product to compute the angle.

mikerobi
A: 

Find the delta vectors between the center and your two points

d1 = p1-c;
d2 = p2-c;

You can use atan2 to get the angle of each of these:

angle1 = atan2(d1.Y, d1.X)
angle2 = atan2(d2.Y, d2.X)

and your desired angle is simply the difference:

a = angle2-angle1;

Depending on whether you want the angle to be represented as between 0 and 2pi, or -2pi and 0, you can just use a while loop to keep subtracting 2pi / adding 2pi to get the representation you want, although you should only need to do this when presenting the angle to a human

Rob Fonseca-Ensor