前言
需求:在高德地图中判断用户绘制的围栏是否合法。
核心解决点:倒序依次判断如果是相邻的二根线段,判断是否有交点,非相邻的线段不相交。
安装
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]]