Showing posts with label polygon. Show all posts
Showing posts with label polygon. Show all posts

2010/03/15

주어진 점이 다각형에 포함되는지 확인하기


function PtInPoly
(const Points: Array of TPoint; X,Y: Integer):
Boolean;
var Count, K, J : Integer;
begin
Result := False;
Count := Length(Points) ;
J := Count-1;
for K := 0 to Count-1 do begin
if ((Points[K].Y <=Y) and (Y < Points[J].Y)) or
((Points[J].Y <=Y) and (Y < Points[K].Y)) then
begin
if (x < (Points[j].X - Points[K].X) *
(y - Points[K].Y) /
(Points[j].Y - Points[K].Y) + Points[K].X) then
Result := not Result;
end;
J := K;
end;
end;





2009/09/02

다각형의 면적과 무게 중심 구하기

업무 때문에 찾아 본 자료이다. 다각형의 면적과 무게 중심을 구하는 공식인데 생각보다는 심플 한데 이해가 쉽지는 않다. 공부를 해야겠지....





위와 같은 좌표 구성의 다각형이 있을때 면적과 무게 중심 값은 아래와 같다.











위 식을 이용하는 코드를 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