File tree 2 files changed +23
-0
lines changed
2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -417,6 +417,9 @@ namespace s3d
417
417
[[nodiscard]]
418
418
double perimeter () const noexcept ;
419
419
420
+ [[nodiscard]]
421
+ Vec2 centroid () const ;
422
+
420
423
[[nodiscard]]
421
424
RectF computeBoundingRect () const noexcept ;
422
425
Original file line number Diff line number Diff line change @@ -330,6 +330,26 @@ namespace s3d
330
330
return total;
331
331
}
332
332
333
+ Vec2 MultiPolygon::centroid () const
334
+ {
335
+ if (m_data.empty ())
336
+ {
337
+ return Vec2{ 0 , 0 };
338
+ }
339
+
340
+ Vec2 weightedCoordsTotal{ 0 , 0 };
341
+ double areaTotal = 0.0 ;
342
+
343
+ for (const auto & polygon : m_data)
344
+ {
345
+ const double polygonArea = polygon.area ();
346
+ weightedCoordsTotal += polygonArea * polygon.centroid ();
347
+ areaTotal += polygonArea;
348
+ }
349
+
350
+ return weightedCoordsTotal / areaTotal;
351
+ }
352
+
333
353
RectF MultiPolygon::computeBoundingRect () const noexcept
334
354
{
335
355
if (isEmpty ())
You can’t perform that action at this time.
0 commit comments