views:

594

answers:

6

Hi folks,

I have two lines: Line1 and Line2. Each line is defined by two points (P1L1(x1, y1), P2L1(x2, y2) and P1L1(x1, y1), P2L3(x2, y3)). I want to know the inner angle defined by these two lines.

For do it I calculate the angle of each line with the abscissa:

double theta1 = atan(m1) * (180.0 / PI);
double theta2 = atan(m2) * (180.0 / PI);

After to know the angle I calculate the following:

double angle = abs(theta2 - theta1);

The problem or doubt that I have is: sometimes I get the correct angle but sometimes I get the complementary angle (for me outer). How can I know when subtract 180º to know the inner angle? There is any algorithm better to do that? Because I tried some methods: dot product, following formula:

result = (m1 - m2) / (1.0 + (m1 * m2));

But always I have the same problem; I never known when I have the outer angle or the inner angle!

Thanks in advance for reading my trouble and for your time!

Oscar.

+3  A: 
if (result > 180)
{
     result = 360 - result;
}

That way it will always be the inner angle. Just add it after you get result.

thyrgle
Thanks thyrgle, but I don't want an angle bigger that 180º. I want the same if I like to know one angle of a triangle knowing only the three points.
ocell
I don't think I understand you... The outer angle will always be greater than 180 or equal to it (and in that case the outer angle = the inner angle) so you can't get an angle bigger than 180 using this method considering 180 is half 360...
thyrgle
In other words, you can't get an angle greater than 180 with this.
thyrgle
Understandably, acute and obtuse angles of an intersection are 180 deg complents of each other. i.e. acute + obtuse = PI.http://www.mathworks.com/access/helpdesk/help/techdoc/ref/atan.html exhibits that an atan is asymptotic at +/- PI/2 or 90 deg, as we all already know.Because the max absolute result of atan is 90 deg or PI/2, the max diff between two atan results is 180 deg or PI.So there is nothing that avails us to deal with 360 deg threshold.So the two guys who voted you up is also in error.
Blessed Geek
Also, the arithmetic precision of the test [if(result>PI/4)] in radian is a problem that no known supercomputer could solve because PI/4 is not an exact/proper fraction. At the neighbourhood of 90 deg, the precision of the binary representation is questionable whether it is acute or obtuse.
Blessed Geek
I meant to say PI/2 not PI/4.
Blessed Geek
A: 

Getting the outer angle vs the inner angle is determined entirely by the order of your subtractions (think about it). You need to subtract the smaller theta from the larger in order to reliably always get the inner angle. You also probably want to use the atan2 function because of the type of data you're expecting.

Donnie
Let's say smaller theta is 10 deg and larger theta is -10 deg (170 deg).170 - 10 = 160. 160 is hardly an acute angle. Whereas, its complement, 20 deg would be the answer.
Blessed Geek
+6  A: 
andand
Hi andand, thanks for the response but I have the same problem, when I'm calculating an angle from a line that goes left to right (horizontal) with another line that goes right to left (from quad1 to quad3) and in a paper this lines have an acute angle (less that 90) the algorithm shows me an angle of 150 or more degrees and really I have an angle of 30 degrees. For other cases works fine.Thanks a lot for your time!Oscar.
ocell
@ocell: all you need to add is `float final_theta = theta > 90degrees ? 180degrees - theta : theta` to get the result you're looking for.
Skizz
@ocell: What are the actual values of (x1, y1), (x2, y2) and (x3, y3) that are demonstrating the behavior you're describe?
andand
@Skizz: What youpropose will produce erroneous results when the inner angle actually is obtuse.
andand
@andand E.g.:Line from ( 174 , 228 ) to ( 215 , 294 ) Line from ( 215 , 294 ) to ( 303 , 294 ) Angle: 120degreesLine from ( 19 , 457 ) to ( 153 , 457 ) Line from ( 153 , 457 ) to ( 15 , 470 ) Angle: 5degrees
ocell
@ocell: Okay, I updated the code to reflect the points you gave, and the results are approximately what you indicate they should be. Are you certain you're using the point p1 = (x1, y1) as your vertex in your implementation?
andand
@andand: Sorry the points are get from the log of View class (a class that has all the visual components: Line, Mark, etc.). Actually when I calculate the angle I'm using P2 (in the log) as P1 for the angle.
ocell
@All people involved in the answer: Thanks for your time and for help me to understand better the measurement of angles in a plane; I was so obfuscated in one direction and finally I see different alternatives that can help in development of my dissertation. Once again thanks to everyb@dy and specially to andand that finally put a great code.See you soon!Oscar.
ocell
A: 

Inner angle between 2 vectors (v1, v2) = arc cos ( inner product(v1,v2) / (module(v1) * module(v2)) ).

Where inner product(v1,v2) = xv1*xv2 + yv1*yv2

module(v) = sqrt(pow(xv,2) + pow(yv,2))

So, the answer of your question is implemented on the following example:

#define PI   3.14159258

int main()
{
    double x1,y1,x2,y2,y3;
    double m1, m2;
    double mod1, mod2, innerp, angle;

    cout << "x1 :";
    cin >> x1;
    cout << "y1 :";
    cin >> y1;
    cout << "x2 :";
    cin >> x2;
    cout << "y2 :";
    cin >> y2;
    cout << "y3 :";
    cin >> y3;

    m1 = atan((y2-y1)/(x2-x1)) * 180 / PI;
    m2 = atan((y3-y1)/(x2-x1)) * 180 / PI;

    mod1   = sqrt(pow(y2-y1,2)+pow(x2-x1,2));
    mod2   = sqrt(pow(y3-y1,2)+pow(x2-x1,2));
    innerp = (x2-x1)*(x2-x1) + (y2-y1)*(y3-y1);
    angle  = acos(innerp / (mod1 * mod2)) * 180 / PI;

    cout << "m1 : " << m1 << endl;
    cout << "m2 : " << m2 << endl;
    cout << "angle : " << angle << endl;
}
Jorg B Jorge
What's the purpose of computing m1 and m2? The answer the OP is looking for is in angle and you use neither m1 nor m2 in computing it.
andand
Using pow for power of two? ouch. It is supposed to be used for fractional powers.
SigTerm
m1 and m2 are just to illustrate the program, showing the individual angle of each line. I used pow just because it would be easier to the reader to understand the formula. I am not trying to improve performance right here. I'm just trying to help the guy about the comprehension of the math formula.
Jorg B Jorge
A: 
Blessed Geek
@h2g2java: The OP asked for the inner angle of two lines defined by 3 points (one point (x1, y1) in common to both lines). In some cases this inner angle will be acute, in others it will be obtuse.
andand
+1  A: 

The whole point is much easier than the given answers:

When you use atan(slope) you lose (literally) one bit of information, that is there are exactly two angles (theta) and (theta+PI) in the range (0..2*PI), which give the same value for the function tan().

Just use atan2(deltax, deltay) and you get the right angle. For instance

atan2(1,1) == PI/4
atan2(-1,-1) == 5*PI/4

Then subtract, take absolute value, and if greater than PI subtract from 2*PI.

saverio