ccw计算机中心,ccw算法的说明(Explanation of ccw algorithm)

这篇博客探讨了用于判断二维平面上两条线段是否相交的ccw算法。ccw函数通过比较向量叉积来确定点的相对位置,而intersect函数利用ccw的返回值来判断线段交叉情况。虽然博主能理解intersect函数的工作原理,但对于ccw函数内部的细节感到困惑,尤其是为什么没有直接使用叉积进行比较。博客详细解析了ccw函数的逻辑,帮助读者理解其背后的数学原理。
摘要由CSDN通过智能技术生成

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?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值