I'm having some trouble understanding the ccw (counterclockwise) algorithm:
int ccw (Point P0, Point P1, Point P2) {
dx1 = P1.x - P0.x;
dx2 = P2.x - P0.x;
dy1 = P1.y - P0.y;
dy2 = P1.y - P0.y;
if (dy1 * dx2 > dy2 * dx1) return -1;
if (dx1 * dy2 > dy1 * dx2) return 1;
if ((dx1 * dx2 < 0) || (dy1 * dy2 < 0)) return 1;
if ((dx1 * dx1 + dy1 * dy1) < (dx2 * dx2 + dy2 * dy2)) return -1;
return 0;
}
This code it used to see if two lines intersect:
bool intersect (Vector2D l1, Vector2D l2) {
return (((ccw(l1.start, l1.end, l2.start) * ccw(l1.start, l1.end, l2.end)) <= 0)
&& ((ccw(l2.start, l2.end, l1.start) * ccw(l2.start, l2.end, l1.start)) <= 0))
}
I can understand the code inside the intersect function, but I don't really understand the code inside the ccw function.
Why it isn't used the cross product?
这篇博客探讨了用于判断二维平面上两条线段是否相交的ccw算法。ccw函数通过比较向量叉积来确定点的相对位置,而intersect函数利用ccw的返回值来判断线段交叉情况。虽然博主能理解intersect函数的工作原理,但对于ccw函数内部的细节感到困惑,尤其是为什么没有直接使用叉积进行比较。博客详细解析了ccw函数的逻辑,帮助读者理解其背后的数学原理。
838

被折叠的 条评论
为什么被折叠?



