當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

js根據(jù)坐標(biāo)判斷構(gòu)成單個(gè)多邊形是否合法
2021-10-18 17:45:03

前言

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

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

安裝

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"
/**
 * 坐標(biāo)轉(zhuǎn)線段
 * @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;
}
/**
 * 判斷坐標(biāo)組成的單個(gè)多邊形是否合法
 * @param {*} path 
 * @description 請傳入[[1,2],[2,2],[3,3]] 類似的二維數(shù)組 
 * @returns {boolean}
 */
export function isTruePolygon(path) {
    //  判斷數(shù)組且數(shù)組的長度小于3不構(gòu)成滿足一個(gè)面的必要條件終止
    if (!Array.isArray(path) || path.length < 3) return false;
    //  具體坐標(biāo)也需是一個(gè)一維數(shù)組,并且數(shù)組的長度等于2
    if (!path.every(item => Array.isArray(item) && item.length == 2)) return false;

    // 將坐標(biāo)轉(zhuǎn)成線段
    const lines = pathToLines(path);
    // 是否合法標(biāo)志
    let isTrue = true;
    // 驗(yàn)證函數(shù)
    function check() {
        // 倒序循環(huán)
        for (let i = lines.length - 1; i >= 0; i--) {
            // 基準(zhǔn)線段
            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];
            // 相鄰二根線段必須要有交點(diǎn)
            if (
                !isIntersect(line, lineNext)
                || !isIntersect(line, lineLast)
            ) {
                console.log('相鄰二根線段必須要有交點(diǎn)', line, lineNext, lineLast, isIntersect(line, lineNext), isIntersect(line, lineLast))
                isTrue = false;
                return;
            }
            // 非相鄰的線段必須無交點(diǎn)
            const noNearLines = lines.filter((item, i) => i !== lineNextIndex && i !== lineLastIndex);
            noNearLines.forEach(le => {
                if (isIntersect(line, le)) {
                    console.log('非相鄰的線段必須無交點(diǎn)')
                    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/

開通會(huì)員,享受整站包年服務(wù)立即開通 >