• <small id='hN7Yh'></small><noframes id='hN7Yh'>

      • <bdo id='hN7Yh'></bdo><ul id='hN7Yh'></ul>
    1. <tfoot id='hN7Yh'></tfoot>
      <i id='hN7Yh'><tr id='hN7Yh'><dt id='hN7Yh'><q id='hN7Yh'><span id='hN7Yh'><b id='hN7Yh'><form id='hN7Yh'><ins id='hN7Yh'></ins><ul id='hN7Yh'></ul><sub id='hN7Yh'></sub></form><legend id='hN7Yh'></legend><bdo id='hN7Yh'><pre id='hN7Yh'><center id='hN7Yh'></center></pre></bdo></b><th id='hN7Yh'></th></span></q></dt></tr></i><div id='hN7Yh'><tfoot id='hN7Yh'></tfoot><dl id='hN7Yh'><fieldset id='hN7Yh'></fieldset></dl></div>
      <legend id='hN7Yh'><style id='hN7Yh'><dir id='hN7Yh'><q id='hN7Yh'></q></dir></style></legend>

      1. 在 JavaScript 中查找多边形的中心点

        时间:2023-09-08

            <bdo id='apqub'></bdo><ul id='apqub'></ul>

              • <i id='apqub'><tr id='apqub'><dt id='apqub'><q id='apqub'><span id='apqub'><b id='apqub'><form id='apqub'><ins id='apqub'></ins><ul id='apqub'></ul><sub id='apqub'></sub></form><legend id='apqub'></legend><bdo id='apqub'><pre id='apqub'><center id='apqub'></center></pre></bdo></b><th id='apqub'></th></span></q></dt></tr></i><div id='apqub'><tfoot id='apqub'></tfoot><dl id='apqub'><fieldset id='apqub'></fieldset></dl></div>
              • <small id='apqub'></small><noframes id='apqub'>

                  <tbody id='apqub'></tbody>
                <legend id='apqub'><style id='apqub'><dir id='apqub'><q id='apqub'></q></dir></style></legend>
                • <tfoot id='apqub'></tfoot>

                  本文介绍了在 JavaScript 中查找多边形的中心点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个来自谷歌地图的地点"对象,它有一组坐标,代表给定位置的边界框,比如伦敦.每组坐标都有一个纬度和经度.

                  I have a "place" object from Google Maps which has a set of coordinates that represent a bounding box for a given location, say London. Each set of coordinates has a latitude and longitude.

                  我已经编写了以下代码来查找中心点,但我不确定它是否确实产生了中心点.如果多边形有 5 个点而不是 4 个呢?另外,这是否可以以更有效的方式完成,操作更少?

                  I have written the below code to find the centerpoint, but I am not sure if it does actually produce the centerpoint. What if the polygon has 5 points instead of 4? Also, can this be done in a more efficient way, with less operations?

                  function average(array) {
                    // Add together and then divide by the length
                    return _.reduce(array, function (sum, num) {
                      return sum + num;
                    }, 0) / array.length;
                  }
                  
                  // I have a two-dimensional array that I want to get the average of
                  
                  var coords = [
                    [ -1.2, 5.1 ],
                    [ -1.3, 5.2 ],
                    [ -1.8, 5.9 ],
                    [ -1.9, 5.8 ]
                  ]
                  
                  // So I get the first column
                  
                  var lats = coords.map(function (coord) {
                    return coord[0];
                  })
                  
                  // Then the second
                  
                  var longs = coords.map(function (coord) {
                    return coord[1];
                  })
                  
                  // And average each column out
                  
                  console.log([average(lats), average(longs)])
                  

                  示例.

                  推荐答案

                  这应该得到 centroid 任何 区域wiki/多边形" rel="noreferrer">多边形

                  This should get the centroid of the area of any polygon

                  /*jslint sub: true, maxerr: 50, indent: 4, browser: true */
                  /*global console */
                  
                  (function () {
                      "use strict";
                  
                      function Point(x, y) {
                          this.x = x;
                          this.y = y;
                      }
                  
                      function Region(points) {
                          this.points = points || [];
                          this.length = points.length;
                      }
                  
                      Region.prototype.area = function () {
                          var area = 0,
                              i,
                              j,
                              point1,
                              point2;
                  
                          for (i = 0, j = this.length - 1; i < this.length; j=i,i++) {
                              point1 = this.points[i];
                              point2 = this.points[j];
                              area += point1.x * point2.y;
                              area -= point1.y * point2.x;
                          }
                          area /= 2;
                  
                          return area;
                      };
                  
                      Region.prototype.centroid = function () {
                          var x = 0,
                              y = 0,
                              i,
                              j,
                              f,
                              point1,
                              point2;
                  
                          for (i = 0, j = this.length - 1; i < this.length; j=i,i++) {
                              point1 = this.points[i];
                              point2 = this.points[j];
                              f = point1.x * point2.y - point2.x * point1.y;
                              x += (point1.x + point2.x) * f;
                              y += (point1.y + point2.y) * f;
                          }
                  
                          f = this.area() * 6;
                  
                          return new Point(x / f, y / f);
                      };
                  
                      var polygon = [
                              {"x": -1.2, "y": 5.1},
                              {"x": -1.3, "y": 5.2},
                              {"x": -1.8, "y": 5.9},
                              {"x": -1.9, "y": 5.8}
                          ],
                          region = new Region(polygon);
                  
                      console.log(region.centroid());
                  }());
                  

                  关于 jsfiddle

                  这篇关于在 JavaScript 中查找多边形的中心点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 CSS 样式化 SVG 圆圈 下一篇:用 Three.js 画一个圆(没有阴影)

                  相关文章

                  最新文章

                  <small id='20RZF'></small><noframes id='20RZF'>

                    <bdo id='20RZF'></bdo><ul id='20RZF'></ul>
                  1. <i id='20RZF'><tr id='20RZF'><dt id='20RZF'><q id='20RZF'><span id='20RZF'><b id='20RZF'><form id='20RZF'><ins id='20RZF'></ins><ul id='20RZF'></ul><sub id='20RZF'></sub></form><legend id='20RZF'></legend><bdo id='20RZF'><pre id='20RZF'><center id='20RZF'></center></pre></bdo></b><th id='20RZF'></th></span></q></dt></tr></i><div id='20RZF'><tfoot id='20RZF'></tfoot><dl id='20RZF'><fieldset id='20RZF'></fieldset></dl></div>
                    <tfoot id='20RZF'></tfoot>

                  2. <legend id='20RZF'><style id='20RZF'><dir id='20RZF'><q id='20RZF'></q></dir></style></legend>