Skip to content

Commit

Permalink
MultiPolygon::centroid()の実装(#1186) (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogame3334 authored Jan 29, 2024
1 parent 3bd3308 commit 8ed25d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Siv3D/include/Siv3D/MultiPolygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ namespace s3d
[[nodiscard]]
double perimeter() const noexcept;

[[nodiscard]]
Vec2 centroid() const;

[[nodiscard]]
RectF computeBoundingRect() const noexcept;

Expand Down
20 changes: 20 additions & 0 deletions Siv3D/src/Siv3D/MultiPolygon/SivMultiPolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,26 @@ namespace s3d
return total;
}

Vec2 MultiPolygon::centroid() const
{
if (m_data.empty())
{
return Vec2{ 0, 0 };
}

Vec2 weightedCoordsTotal{ 0, 0 };
double areaTotal = 0.0;

for (const auto& polygon : m_data)
{
const double polygonArea = polygon.area();
weightedCoordsTotal += polygonArea * polygon.centroid();
areaTotal += polygonArea;
}

return weightedCoordsTotal / areaTotal;
}

RectF MultiPolygon::computeBoundingRect() const noexcept
{
if (isEmpty())
Expand Down

0 comments on commit 8ed25d3

Please sign in to comment.