js 获取不规则图形的重心

let list = [
  [464.1, 884.4],
  [1.1, 884.4],
  [1.1, 23.4],
  [204.1, 23.4],
  [464.1, 23.4],
];

function a(list) {
  let area = 0.0;
  let Gx = 0.0;
  let Gy = 0.0;
  for (let i = 1; i <= list.length; i++) {
    let iLat = list[i % list.length][0];
    let iLng = list[i % list.length][1];
    let nextLat = list[i - 1][0];
    let nextLng = list[i - 1][1];
    let temp = (iLat * nextLng - iLng * nextLat) / 2.0;
    area += temp;
    Gx += (temp * (iLat + nextLat)) / 3.0;
    Gy += (temp * (iLng + nextLng)) / 3.0;
  }
  Gx = Gx / area;
  Gy = Gy / area;
  return [Gx, Gy];
}
console.log(a(list));
posted @ 2022-01-18 10:15  7c89  阅读(265)  评论(0编辑  收藏  举报