顺时针路径排序

This commit is contained in:
2023-03-09 11:48:10 +08:00
parent 7f21dd5016
commit 822fc341a4
3 changed files with 79 additions and 56 deletions

15
src/utils/geoUtils.js Normal file
View File

@ -0,0 +1,15 @@
import {points} from "@turf/helpers";
import center from "@turf/center";
// [[-97.522259, 35.4691],
// [-97.502754, 35.463455],
// [-97.508269, 35.463245]]
export const pointSortClockwise = (coordinates) => {
const centerCoor = center(points(coordinates)).geometry.coordinates;
return coordinates.sort((a, b) => {
// 转换为以 centerCoor 为原点的坐标
const point_a = [a[0] - centerCoor[0], a[1] - centerCoor[1]]
const point_b = [b[0] - centerCoor[0], b[1] - centerCoor[1]]
return Math.atan2(...point_b.reverse()) - Math.atan2(...point_a.reverse())
})
}