위와 같은 좌표 구성의 다각형이 있을때 면적과 무게 중심 값은 아래와 같다.
위 식을 이용하는 코드를 C++로 작성해 보았다.
for (i = 0; i < points; i++)
{
j = (i + 1) % points;
pt1 = pPoly->GetPointAt(i);
pt2 = pPoly->GetPointAt(j);
x1 = pt1.x;
x2 = pt2.x;
y1 = pt1.y;
y2 = pt2.y;
area += x1 * y2;
area -= y1 * x2;
centerx += ((x1 + x2) * ((x1 * y2) - (x2 * y1)));
centery += ((y1 + y2) * ((x1 * y2) - (x2 * y1)));
}
area /= 2.0;
area = fabs(area);
centerx = centerx / (6.0 * area);
centery = centery / (6.0 * area);
출처 : Calculating the area and centroid of a polygon
참고 : Polygon Area -- from Wolfram MathWorld
Original Post : http://neodreamer-dev.tistory.com/317
No comments :
Post a Comment