<small id='Lla1A'></small><noframes id='Lla1A'>

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

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

        找到多边形的质心?

        时间:2023-09-16

          <small id='HYIwD'></small><noframes id='HYIwD'>

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

                  <legend id='HYIwD'><style id='HYIwD'><dir id='HYIwD'><q id='HYIwD'></q></dir></style></legend>
                1. <tfoot id='HYIwD'></tfoot>
                    <tbody id='HYIwD'></tbody>
                2. <i id='HYIwD'><tr id='HYIwD'><dt id='HYIwD'><q id='HYIwD'><span id='HYIwD'><b id='HYIwD'><form id='HYIwD'><ins id='HYIwD'></ins><ul id='HYIwD'></ul><sub id='HYIwD'></sub></form><legend id='HYIwD'></legend><bdo id='HYIwD'><pre id='HYIwD'><center id='HYIwD'></center></pre></bdo></b><th id='HYIwD'></th></span></q></dt></tr></i><div id='HYIwD'><tfoot id='HYIwD'></tfoot><dl id='HYIwD'><fieldset id='HYIwD'></fieldset></dl></div>
                3. 本文介绍了找到多边形的质心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  为了获得中心,我尝试将每个顶点添加到总数中,然后除以顶点数.

                  To get the center, I have tried, for each vertex, to add to the total, divide by the number of vertices.

                  我也试过找到最上面、最下面 -> 找到中点...找到最左边、最右边,找到中点.

                  I've also tried to find the topmost, bottommost -> get midpoint... find leftmost, rightmost, find the midpoint.

                  这两个都没有返回完美的中心,因为我依靠中心来缩放多边形.

                  Both of these did not return the perfect center because I'm relying on the center to scale a polygon.

                  我想缩放我的多边形,所以我可以在它们周围放置一个边框.

                  I want to scale my polygons, so I may put a border around them.

                  考虑到多边形可能是凹的、凸的并且有许多不同长度的边,找到多边形质心的最佳方法是什么?

                  What is the best way to find the centroid of a polygon given that the polygon may be concave, convex and have many many sides of various lengths?

                  推荐答案

                  给出公式这里 表示顶点按照它们沿多边形周长的出现次数排序.

                  对于那些难以理解这些公式中的 sigma 符号的人,这里有一些 C++ 代码展示了如何进行计算:

                  For those having difficulty understanding the sigma notation in those formulas, here is some C++ code showing how to do the computation:

                  #include <iostream>
                  
                  struct Point2D
                  {
                      double x;
                      double y;
                  };
                  
                  Point2D compute2DPolygonCentroid(const Point2D* vertices, int vertexCount)
                  {
                      Point2D centroid = {0, 0};
                      double signedArea = 0.0;
                      double x0 = 0.0; // Current vertex X
                      double y0 = 0.0; // Current vertex Y
                      double x1 = 0.0; // Next vertex X
                      double y1 = 0.0; // Next vertex Y
                      double a = 0.0;  // Partial signed area
                  
                      // For all vertices except last
                      int i=0;
                      for (i=0; i<vertexCount-1; ++i)
                      {
                          x0 = vertices[i].x;
                          y0 = vertices[i].y;
                          x1 = vertices[i+1].x;
                          y1 = vertices[i+1].y;
                          a = x0*y1 - x1*y0;
                          signedArea += a;
                          centroid.x += (x0 + x1)*a;
                          centroid.y += (y0 + y1)*a;
                      }
                  
                      // Do last vertex separately to avoid performing an expensive
                      // modulus operation in each iteration.
                      x0 = vertices[i].x;
                      y0 = vertices[i].y;
                      x1 = vertices[0].x;
                      y1 = vertices[0].y;
                      a = x0*y1 - x1*y0;
                      signedArea += a;
                      centroid.x += (x0 + x1)*a;
                      centroid.y += (y0 + y1)*a;
                  
                      signedArea *= 0.5;
                      centroid.x /= (6.0*signedArea);
                      centroid.y /= (6.0*signedArea);
                  
                      return centroid;
                  }
                  
                  int main()
                  {
                      Point2D polygon[] = {{0.0,0.0}, {0.0,10.0}, {10.0,10.0}, {10.0,0.0}};
                      size_t vertexCount = sizeof(polygon) / sizeof(polygon[0]);
                      Point2D centroid = compute2DPolygonCentroid(polygon, vertexCount);
                      std::cout << "Centroid is (" << centroid.x << ", " << centroid.y << ")
                  ";
                  }
                  

                  我只针对右上 x/y 象限中的方形多边形进行了测试.

                  I've only tested this for a square polygon in the upper-right x/y quadrant.

                  如果您不介意在每次迭代中执行两个(可能很昂贵)额外的模数运算,那么您可以将之前的 compute2DPolygonCentroid 函数简化为以下内容:

                  If you don't mind performing two (potentially expensive) extra modulus operations in each iteration, then you can simplify the previous compute2DPolygonCentroid function to the following:

                  Point2D compute2DPolygonCentroid(const Point2D* vertices, int vertexCount)
                  {
                      Point2D centroid = {0, 0};
                      double signedArea = 0.0;
                      double x0 = 0.0; // Current vertex X
                      double y0 = 0.0; // Current vertex Y
                      double x1 = 0.0; // Next vertex X
                      double y1 = 0.0; // Next vertex Y
                      double a = 0.0;  // Partial signed area
                  
                      // For all vertices
                      int i=0;
                      for (i=0; i<vertexCount; ++i)
                      {
                          x0 = vertices[i].x;
                          y0 = vertices[i].y;
                          x1 = vertices[(i+1) % vertexCount].x;
                          y1 = vertices[(i+1) % vertexCount].y;
                          a = x0*y1 - x1*y0;
                          signedArea += a;
                          centroid.x += (x0 + x1)*a;
                          centroid.y += (y0 + y1)*a;
                      }
                  
                      signedArea *= 0.5;
                      centroid.x /= (6.0*signedArea);
                      centroid.y /= (6.0*signedArea);
                  
                      return centroid;
                  }
                  

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

                  上一篇:对象指针向量上的 vector::erase() 是否会破坏对象本 下一篇:C++ 删除向量、对象、空闲内存

                  相关文章

                  最新文章

                4. <tfoot id='C6J9J'></tfoot>
                  • <bdo id='C6J9J'></bdo><ul id='C6J9J'></ul>
                  <legend id='C6J9J'><style id='C6J9J'><dir id='C6J9J'><q id='C6J9J'></q></dir></style></legend>

                5. <small id='C6J9J'></small><noframes id='C6J9J'>

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