基本上Lou Franco 的answer 向您解释了如何获得每条路径的两条中心线的交点,然后该交点是开始转弯的好点。
我会添加一个关于如何计算路径中心线的建议。
根据我的经验,在处理从图像中提取的线的浮点表示时,这些线实际上永远不会平行,它们通常只是在图像外的一点相交(可能很远)。
以下 C++ 函数 bisector_of_lines 的灵感来自 CGAL source code 中的方法 bisector_of_linesC2。
一行表示为a*x+b*y+c=0,如下函数
构造p 和q 两条线的平分线。
line p is pa*x+pb*y+pc=0
line q is qa*x+qb*y+qc=0
平分线的a、b、c是函数的最后三个参数:a、b和c。
在一般情况下,平分线的向量方向是两条线的归一化方向之和,并且通过p和q的交点。如果p 和q 平行,则平分线定义为与p 方向相同且与p 和q 距离相同的直线(参见CGAL 官方文档为CGAL::Line_2<Kernel> CGAL::bisector)。
void
bisector_of_lines(const double &pa, const double &pb, const double &pc,
const double &qa, const double &qb, const double &qc,
double &a, double &b, double &c)
{
// We normalize the equations of the 2 lines, and we then add them.
double n1 = sqrt(pa*pa + pb*pb);
double n2 = sqrt(qa*qa + qb*qb);
a = n2 * pa + n1 * qa;
b = n2 * pb + n1 * qb;
c = n2 * pc + n1 * qc;
// Care must be taken for the case when this produces a degenerate line.
if (a == 0 && b == 0) {// maybe it is best to replace == with https://stackoverflow.com/questions/19837576/comparing-floating-point-number-to-zero
a = n2 * pa - n1 * qa;
b = n2 * pb - n1 * qb;
c = n2 * pc - n1 * qc;
}
}