Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiPolygon::centroid()の実装(#1186) #1190

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
Reputeless marked this conversation as resolved.
Show resolved Hide resolved
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
Loading