我有两条路线作为折线作为坐标列表,例如: [13.072624,52.333508,13.763972,52.679616] [52.679616,13.763972,52.333508,13.072624]
我需要比较两条折线的方向。为此,我想要获得它们之间的角度,如果它小于45°,则返回true(朝同一方向)。
我们怎么能这样做?
答案 0 :(得分:0)
您需要相互比较的是线段的单位矢量。我不知道你在使用什么语言或列表是如何组织的,但是下面的伪代码将为你提供方向。
// create end points of the line segments from lists
point0(x0, y0);
point1(x1, y1);
point2(x2, y2);
point3(x3, y3);
// create direction vectors from points
directionA = point1 - point0;
directionB = point3 - point2;
// normalize the direction vectors to have a length of 1
// assuming they are not degenerate
directionA.unitize();
directionB.unitize();
// get the dot product of the two direction vectors
dot = directionA.dot(directionB);
// Because the direction vectors are both unit length
// the dot product will return a value between -1.0 and 1.0
// The arc cosine will give you your angle between 0 and 180
angle = arc_cosine_degree(dot)
然后将角度与您选择用于确定“相同”方向的容差进行比较。维基百科对我在伪代码中使用的每个概念(即单位向量,点积,反余弦)都有很好的解释。
祝你好运! GCB