计算两条折线之间的方位
calculate bearing between two polylines
我得到了两条路线作为坐标列表的折线,例如:
[13.072624,52.333508,13.763972,52.679616]
[52.679616,13.763972,52.333508,13.072624]
我需要比较两条折线的方向。为此,我想得到它们之间的角度,如果它小于 45°,return 为真(朝同一方向)。
example of route comparison
我们该怎么做?
你需要相互比较的是线段的单位向量。我不知道您使用的是哪种语言,也不知道列表是如何组织的,但以下伪代码会让您了解方位。
// 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)
然后将角度与您选择的用于确定 "same" 方向的公差进行比较。维基百科对我在伪代码中使用的每个概念(即单位向量、点积、反余弦)都有很好的解释。
祝你好运! GCB
我得到了两条路线作为坐标列表的折线,例如: [13.072624,52.333508,13.763972,52.679616] [52.679616,13.763972,52.333508,13.072624]
我需要比较两条折线的方向。为此,我想得到它们之间的角度,如果它小于 45°,return 为真(朝同一方向)。
example of route comparison
我们该怎么做?
你需要相互比较的是线段的单位向量。我不知道您使用的是哪种语言,也不知道列表是如何组织的,但以下伪代码会让您了解方位。
// 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)
然后将角度与您选择的用于确定 "same" 方向的公差进行比较。维基百科对我在伪代码中使用的每个概念(即单位向量、点积、反余弦)都有很好的解释。
祝你好运! GCB