I hope I understand your question correctly as wanting the acute angle rather than the obtuse angle of the intersection of two lines. Am I correct?
Acute and obtuse angles of an intersection are 180 deg complements 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.
Therefore, the max difference between two results of atan is pi or 180 deg, whether you use the +/-
notation or positive 0 to pi
notation of a gradient.
Consider the following pseudocode:
acuteAngle(m1, m2){
a = atan(m1) - atan(m2);
// if obtuse get the complementary acute angle:
if (a>PI/2)
a = PI - a;
return a;
}
The function acuteAngle
illustrates what you need to do, mathematically.
However, it cannot be used for values of angles in the neighbourhood of PI/2 because binary comparisons of angles with results in that neighbourhood is questionable whether an obtuse or acute angle is represented.
Therefore, we have to compare the coordinates of the points of the two lines.
We find out whether the 3rd line formed from [(x2,y2)(x3,y3)]
is shorter, equal or longer than the hypothetical hypotenuse.
By virtue of Pythagoras' theorem,
A hypotenuse is formed if the angle is exactly PI/2 or 90 deg. Let's call his hypothetical hypotenuse line L3Hypo.
By geometrical visualisation in your mind,
- If the 3rd line is longer than
L3Hypo, the angle is obtuse.
- If shorter, the angle is acute.
- Otherwise, perfect 90.
Therefore,
L1.lengthSquared = sq(x2-x1) + sq(y2-y1)
L2.lengthSquared = sq(x3-x1) + sq(y3-y1)
L3Hypo.lengthSquared = L1.lengthSquared + L2.lengthSquared
L3.lengthSquared = sq(x3-x2) + sq(y3-y2)
Therefore, the following pseudo-code,
struct Point{
double x, y;
}
// no need to struct, for clarity only
struct Line{
double lengthSquared;
}
#define sq(n) (n*n)
int isObtuse(Point P1, P2, P3){
Line L1, L2, L3, L3Hypo;
L1.lengthSquared = sq(P2.x-P1.x) + sq(P2.y-P1.y);
L2.lengthSquared = sq(P3.x-P1.x) + sq(P3.y-P1.y);
L3Hypo.lengthSquared = L1.lengthSquared + L2.lengthSquared;
L3.lengthSquared = sq(P3.x-P2.x) + sq(P3.y-P2.y);
if (L3>L3Hypo) return 1; //obtuse
else if (L3<L3Hypo) return -1; //acute
else return 0;
}
Presuming you already have the function
getGradient(Point P, Q):
double m1m2 = getGradient(P1,P2);
double m1m3 = getGradient(P1,P3);
double a = Abs(atan(m1m2) - atan(m1m3));
if (isObtuse(P1, P2, P3)>0)
a = PI - a;
I may have committed some typo mistakes in the pseudo-code (hopefully not) but I demonstrated the gist of the concept. If so, someone could be so kind to edit away the typos.
Further
However, after mulling over it, I find that the struggle for precision pivots on its weakest link due to the directive
#define PI 3.14159blah..blah..blah.
So, we might as well save all the trouble and simply do this:
double m1m2 = getGradient(P1,P2);
double m1m3 = getGradient(P1,P3);
double a = Abs(atan(m1m2) - atan(m1m3));
double b = PI - a;
return min(a, b);//the smaller of the two is the acute