You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
346 B

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. export default function(polygon) {
  2. var i = -1,
  3. n = polygon.length,
  4. x = 0,
  5. y = 0,
  6. a,
  7. b = polygon[n - 1],
  8. c,
  9. k = 0;
  10. while (++i < n) {
  11. a = b;
  12. b = polygon[i];
  13. k += c = a[0] * b[1] - b[0] * a[1];
  14. x += (a[0] + b[0]) * c;
  15. y += (a[1] + b[1]) * c;
  16. }
  17. return k *= 3, [x / k, y / k];
  18. }