您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么利用js根據(jù)坐標(biāo)判斷構(gòu)成單個(gè)多邊形是否合法,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
需求:在高德地圖中判斷用戶繪制的圍欄是否合法。
核心解決點(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 請(qǐng)傳入[[1,2],[2,2],[3,3]] 類似的二維數(shù)組 * @returns {boolean} */ export function isTruePolygon(path) { // 判斷數(shù)組且數(shù)組的長(zhǎng)度小于3不構(gòu)成滿足一個(gè)面的必要條件終止 if (!Array.isArray(path) || path.length < 3) return false; // 具體坐標(biāo)也需是一個(gè)一維數(shù)組,并且數(shù)組的長(zhǎng)度等于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
關(guān)于怎么利用js根據(jù)坐標(biāo)判斷構(gòu)成單個(gè)多邊形是否合法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。