MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / getCentroid

Method getCentroid

src/Core/Transform/Polygon.cpp:224–257  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

222 }
223
224 UnitVector Polygon::getCentroid() const
225 {
226 Transform::UnitVector centroid = { 0, 0 };
227 double signedArea = 0.0;
228 double x0, y0, x1, y1, a;
229
230 std::size_t i;
231 for (i = 0; i < m_points.size() - 1; ++i)
232 {
233 x0 = m_points[i]->x;
234 y0 = m_points[i]->y;
235 x1 = m_points[i + 1]->x;
236 y1 = m_points[i + 1]->y;
237 a = x0 * y1 - x1 * y0;
238 signedArea += a;
239 centroid.x += (x0 + x1) * a;
240 centroid.y += (y0 + y1) * a;
241 }
242
243 x0 = m_points[i]->x;
244 y0 = m_points[i]->y;
245 x1 = m_points[0]->x;
246 y1 = m_points[0]->y;
247 a = x0 * y1 - x1 * y0;
248 signedArea += a;
249 centroid.x += (x0 + x1) * a;
250 centroid.y += (y0 + y1) * a;
251
252 signedArea *= 0.5;
253 centroid.x /= (6.0 * signedArea);
254 centroid.y /= (6.0 * signedArea);
255
256 return centroid;
257 }
258
259 std::optional<PolygonPoint*> Polygon::getPointAroundPosition(
260 const Transform::UnitVector& position, const Transform::UnitVector& tolerance)

Callers 5

getRelativePositionMethod · 0.80
setRelativePositionMethod · 0.80

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected