• 當前位置:首頁 > IT技術 > Web編程 > 正文

    js根據坐標判斷構成單個多邊形是否合法
    2021-10-18 17:45:03

    前言

    需求:在高德地圖中判斷用戶繪制的圍欄是否合法。

    核心解決點:倒序依次判斷如果是相鄰的二根線段,判斷是否有交點,非相鄰的線段不相交。

    安裝

    npm install  @turf/helpers  @turf/line-intersect
    

    代碼

    /**
     * geometric 幾何工具庫
     * @author maybe
     * @license https://gitee.com/null_639_5368
     */
    import * as  turf from "@turf/helpers"
    import lineIntersect from "@turf/line-intersect"
    /**
     * 坐標轉線段
     * @param {*} path 
     * @returns {arr}
     */
    export function pathToLines(path) {
        const lines = [];
    
        path.forEach((p, pi) => {
            let line;
            if (pi == path.length - 1) {
                line = turf.lineString([path[pi], path[0]]);
                lines.push(line)
                return;
            }
            line = turf.lineString([path[pi], path[pi + 1]]);
            lines.push(line)
        })
        // console.log(JSON.stringify(lines))
        return lines;
    }
    /**
     * 判斷坐標組成的單個多邊形是否合法
     * @param {*} path 
     * @description 請傳入[[1,2],[2,2],[3,3]] 類似的二維數組 
     * @returns {boolean}
     */
    export function isTruePolygon(path) {
        //  判斷數組且數組的長度小于3不構成滿足一個面的必要條件終止
        if (!Array.isArray(path) || path.length < 3) return false;
        //  具體坐標也需是一個一維數組,并且數組的長度等于2
        if (!path.every(item => Array.isArray(item) && item.length == 2)) return false;
    
        // 將坐標轉成線段
        const lines = pathToLines(path);
        // 是否合法標志
        let isTrue = true;
        // 驗證函數
        function check() {
            // 倒序循環
            for (let i = lines.length - 1; i >= 0; i--) {
                // 基準線段
                const line = lines[i];
                const lineNextIndex = i == 0 ? lines.length - 1 : i - 1;
                const lineLastIndex = i == lines.length - 1 ? 0 : i + 1;
                const lineNext = lines[lineNextIndex];
                const lineLast = lines[lineLastIndex];
                // 相鄰二根線段必須要有交點
                if (
                    !isIntersect(line, lineNext)
                    || !isIntersect(line, lineLast)
                ) {
                    console.log('相鄰二根線段必須要有交點', line, lineNext, lineLast, isIntersect(line, lineNext), isIntersect(line, lineLast))
                    isTrue = false;
                    return;
                }
                // 非相鄰的線段必須無交點
                const noNearLines = lines.filter((item, i) => i !== lineNextIndex && i !== lineLastIndex);
                noNearLines.forEach(le => {
                    if (isIntersect(line, le)) {
                        console.log('非相鄰的線段必須無交點')
                        isTrue = false;
                        return;
                    }
                })
            }
        }
        check();
        isTrue ? console.info('多邊形合法') : console.log("多邊形不合法")
        return isTrue;
    
    }
    
    function isIntersect(line1, line2) {
        return lineIntersect(line1, line2).features.length > 0;
    }
    export default {
        pathToLines,
        isTruePolygon,
    }
    

    測試

    import { isTruePolygon } from './geometric'
    const path_false = [
        [116.403322, 39.920255],
        [116.385726, 39.909893],
        [116.410703, 39.897555],
        [116.402292, 39.892353],
        [116.389846, 39.891365]
    ]
    const path_true = [
        [116.403322, 39.920255],
        [116.410703, 39.897555],
        [116.402292, 39.892353],
        [116.389846, 39.891365]
    ]
    console.log(isTruePolygon(path_true)); // true
    console.log(isTruePolygon(path_false)); // false
    

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码