判断相交

**
* 判断线段ab与线段cd是否相交,两线段重叠或交点为顶点视为不相交
*/
var isIntersection = function(a, b, c, d) {
var flag = false;
if (a == c && b == d
|| b == c
|| b == d
|| a == d
|| (b.y - a.y) * (c.x - d.x) - (c.y - d.y) * (b.x - a.x) == 0) {
return flag;
}
var intersection = new OpenLayers.Geometry.Point();
intersection.x = ((b.x - a.x) * (c.x - d.x) * (c.y - a.y) - c.x
* (b.x - a.x) * (c.y - d.y) + a.x * (b.y - a.y) * (c.x - d.x))
/ ((b.y - a.y) * (c.x - d.x) - (b.x - a.x) * (c.y - d.y));

intersection.y = ((b.y - a.y) * (c.y - d.y) * (c.x - a.x) - c.y
* (b.y - a.y) * (c.x - d.x) + a.y * (b.x - a.x) * (c.y - d.y))
/ ((b.x - a.x) * (c.y - d.y) - (b.y - a.y) * (c.x - d.x));

intersection.x = intersection.x.toFixed(11);//四舍五入为指定位数
intersection.y = intersection.y.toFixed(12);

if ((intersection.x - a.x) * (intersection.x - b.x) <= 0
&& (intersection.x - c.x) * (intersection.x - d.x) <= 0
&& (intersection.y - a.y) * (intersection.y - b.y) <= 0
&& (intersection.y - c.y) * (intersection.y - d.y) <= 0) {
flag = true; // '相交 否则'相交但不在线段上
} else if (c.x == d.x && c.y != d.y && a.y == b.y && a.x != b.x) { // 直角由内向外
flag = true;
} else if (a.x == d.x && a.y != d.y && b.y == c.y && b.x != c.x) { // 直角由外向内
flag = true;
} else if (a.x == b.x && a.y != b.y || a.y == b.y && a.x != b.x) { // 锐角外边平信个,且有一边两端点某坐标在同一坐标
flag = true;
}
return flag;
};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值