Skip to content

Commit 8ed25d3

Browse files
authored
MultiPolygon::centroid()の実装(#1186) (#1190)
1 parent 3bd3308 commit 8ed25d3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Siv3D/include/Siv3D/MultiPolygon.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ namespace s3d
417417
[[nodiscard]]
418418
double perimeter() const noexcept;
419419

420+
[[nodiscard]]
421+
Vec2 centroid() const;
422+
420423
[[nodiscard]]
421424
RectF computeBoundingRect() const noexcept;
422425

Siv3D/src/Siv3D/MultiPolygon/SivMultiPolygon.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ namespace s3d
330330
return total;
331331
}
332332

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+
333353
RectF MultiPolygon::computeBoundingRect() const noexcept
334354
{
335355
if (isEmpty())

0 commit comments

Comments
 (0)