the function below tries to calculate the difference between 2 angles. I use the mathematical reasoning is as follows :
1) Obtain the 2 angles :
angle1 = ST_Azimuth(ST_StartPoint(l1), ST_EndPoint(l1))
angle2 = ST_Azimuth(ST_StartPoint(l2), ST_EndPoint(l2))
2) Obtain vectors (of lenght == 1) from 2 angles :
V1 = (cos(angle1), sin(angle1))
V2 = (cos(angle2), sin(angle2))
3) Compute the dot product :
V1 * V2 = V1[x] * V2[x] + V1[y] * V2[y]
=
cos(angle1) * cos(angle2) + sin(angle1) * sin(angle1)
Since the lenght of the vectors are both 1, it follows that :
the angle between the two vectors = ArcCos(dotProduct)/(length(v1)*legnth(v2)
= ArcCos(dotProduct)
The problem in the function below is that the dot product is yielding values greater than 1, which should not be possible if the math reasoning is correct, and that is my question :
is the reasoning correct ?
CREATE OR REPLACE FUNCTION angleDiff(l1 geometry, l2 geometry)
RETURNS FLOAT AS $$
DECLARE angle1 FLOAT;
DECLARE angle2 FLOAT;
BEGIN
SELECT ST_Azimuth(ST_StartPoint(l1), ST_EndPoint(l1)) into angle1;
SELECT ST_Azimuth(ST_StartPoint(l2), ST_EndPoint(l2)) into angle2;
RETURN acos(cos(angle1) * cos(angle2) + sin(angle1) * sin(angle1));
END;
$$ LANGUAGE plpgsql
sin(angle1)
should besin(angle2)
. Is that the source of the problem or does fixing that still produce an error? Incidentally, most people would compute the difference between two angles asangle1-angle2
. What is the reason you are using this roundabout calculation?