【发布时间】:2015-12-22 12:11:34
【问题描述】:
以下是C++代码取自CP3,计算通过a和b的线与p和q定义的线segment的交点,假设交叉点存在。有人可以解释它在做什么以及它为什么起作用(几何上)吗?
// line segment p-q intersect with line A-B.
point lineIntersectSeg(point p, point q, point A, point B) {
double a = B.y - A.y;
double b = A.x - B.x;
double c = B.x * A.y - A.x * B.y;
double u = fabs(a * p.x + b * p.y + c);
double v = fabs(a * q.x + b * q.y + c);
return point((p.x * v + q.x * u) / (u+v), (p.y * v + q.y * u) / (u+v));
}
请注意,此解决方案似乎与 here 或 Wikipedia page 中解释的不同,因为此解决方案使用绝对值函数。
我已经扩展了交点 (x, y) 的表达式:
【问题讨论】:
-
看起来和the formula in Wikipedia的方法基本一样,只是写法略有不同。
标签: c++ geometry line computational-geometry intersection