js判断点在连续折线哪一侧

该博客介绍了一种用于判断台风点是否位于24h或48h警戒线内的算法。通过`judgePointSideIn`函数确定点相对于直线的位置,然后使用`judgePointInWarning`函数检查点是否在警戒线路径内。算法适用于地图台风预警系统的实时监测。

其实这文跟上文 百度地图画24h和48h台风警戒线 有关系,主要用来判断台风点是否在警戒线以内

1. 先判断点在两点坐标推导的直线哪一侧

/**
** @params { array } list	用来推导直线方法
** @params { array } point	被用于判断在直线哪一侧的点
** @description  判断点在线条左侧(小于0)还是右侧(大于0)
*/
function judgePointSideIn(list, point) {
    let [[x1, y1], [x2, y2]] = list;
    let [x, y] = point;
    if(x1 === x2){
        return x - x1;
    }else if(y1 === y2){
        return y - y1;
    }
    return (y-y1)*(x2-x1) - (x-x1)*(y2-y1);
}

2. 具体判断台风点是24h还是48h以内

/**
** @params { array } point	被用于判断在连续折线的哪一侧的点
** @params { array } list	警戒线坐标数据
** @description  判断点是否在警戒线以内,目前在线上的不算在24h和48h以内
*/
function judgePointInWarning(point, list){
    let [x, y] = point;  
    let vals = [];
    
    list.some(item => {
        let inArea = false;
        const { list, key } = item;
        let firstItem = list[0];
        let lastItem = list[list.length - 1];
        if(!(y <= firstItem[1] || x >= lastItem[0])){
            //  排除警戒线头和尾最外面的数据
            if(x <= firstItem[0] || y >= lastItem[1]){
                //  在警戒线范围内,但不在线段范围内的特殊处理
                inArea = true;
            }else{
                list.slice(1, list.length).some((each, i) => {
                    let [x1, y1] = each;
                    let [x2, y2] = list[i];
                    if((y > y1 && y <= y2) || (y > y2 && y <= y1)){
                        if(judgePointSideIn([each, list[i]], point) < 0){
                            inArea = true;
                            return true;
                        }
                    }
                });
            }
        }
        if(inArea){
            vals.push([key, inArea]);
            if(key === 24){	//	在24h以内的一定在48h以内
                vals.push([48, inArea]);
            }
            return inArea;
        }
    })
    return vals;
}

3. 结果测试

judgePointInWarning([119, 19],earlyWarnLine);
//	[[24, true],[48, true]]
judgePointInWarning([119, 10],earlyWarnLine);
//	[[48, true]]
judgePointInWarning([133, 33],earlyWarnLine);
//	[]

earlyWarnLine 参数在上文 【百度地图画24h和48h台风警戒线】 已定义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值