Advertisement
karbaev

polygon-center

Mar 8th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Point2D
  4. {
  5.     double x;
  6.     double y;
  7. };
  8.  
  9. Point2D compute2DPolygonCentroid(const Point2D* vertices, int vertexCount)
  10. {
  11.     Point2D centroid = {0, 0};
  12.     double signedArea = 0.0;
  13.     double x0 = 0.0; // Current vertex X
  14.     double y0 = 0.0; // Current vertex Y
  15.     double x1 = 0.0; // Next vertex X
  16.     double y1 = 0.0; // Next vertex Y
  17.     double a = 0.0;  // Partial signed area
  18.  
  19.     // For all vertices except last
  20.     int i=0;
  21.     for (i=0; i<vertexCount-1; ++i)
  22.     {
  23.         x0 = vertices[i].x;
  24.         y0 = vertices[i].y;
  25.         x1 = vertices[i+1].x;
  26.         y1 = vertices[i+1].y;
  27.         a = x0*y1 - x1*y0;
  28.         signedArea += a;
  29.         centroid.x += (x0 + x1)*a;
  30.         centroid.y += (y0 + y1)*a;
  31.     }
  32.  
  33.     // Do last vertex separately to avoid performing an expensive
  34.     // modulus operation in each iteration.
  35.     x0 = vertices[i].x;
  36.     y0 = vertices[i].y;
  37.     x1 = vertices[0].x;
  38.     y1 = vertices[0].y;
  39.     a = x0*y1 - x1*y0;
  40.     signedArea += a;
  41.     centroid.x += (x0 + x1)*a;
  42.     centroid.y += (y0 + y1)*a;
  43.  
  44.     signedArea *= 0.5;
  45.     centroid.x /= (6.0*signedArea);
  46.     centroid.y /= (6.0*signedArea);
  47.  
  48.     return centroid;
  49. }
  50.  
  51. //If you don't mind performing two (potentially expensive) extra modulus operations in each iteration,
  52. //then the compute2DPolygonCentroid function above can be simplified to:
  53. Point2D simple_compute2DPolygonCentroid(const Point2D* vertices, int vertexCount)
  54. {
  55.     Point2D centroid = {0, 0};
  56.     double signedArea = 0.0;
  57.     double x0 = 0.0; // Current vertex X
  58.     double y0 = 0.0; // Current vertex Y
  59.     double x1 = 0.0; // Next vertex X
  60.     double y1 = 0.0; // Next vertex Y
  61.     double a = 0.0;  // Partial signed area
  62.  
  63.     // For all vertices
  64.     int i=0;
  65.     for (i=0; i<vertexCount-1; ++i)
  66.     {
  67.         x0 = vertices[i].x;
  68.         y0 = vertices[i].y;
  69.         x1 = vertices[(i+1) % vertexCount].x;
  70.         y1 = vertices[(i+1) % vertexCount].y;
  71.         a = x0*y1 - x1*y0;
  72.         signedArea += a;
  73.         centroid.x += (x0 + x1)*a;
  74.         centroid.y += (y0 + y1)*a;
  75.     }
  76.  
  77.     signedArea *= 0.5;
  78.     centroid.x /= (6.0*signedArea);
  79.     centroid.y /= (6.0*signedArea);
  80.  
  81.     return centroid;
  82. }
  83.  
  84. int main()
  85. {
  86.     Point2D polygon[] = {{0.0,0.0}, {0.0,10.0}, {10.0,10.0}, {10.0,0.0}};
  87.     size_t vertexCount = sizeof(polygon) / sizeof(polygon[0]);
  88.     Point2D centroid = compute2DPolygonCentroid(polygon, vertexCount);
  89.     std::cout << "Centroid is (" << centroid.x << ", " << centroid.y << ")\n";
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement