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 @   7c89  阅读(273)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示