첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ Point 클래스를 사용해 다각형 내부 위치 여부를 구하는 방법을 보여준다.

 

▶ Point 클래스 : 다각형 내부 위치 여부 구하기 예제 (C#)

Point[] polygonPointArray = new Point[]
{
    new Point(100, 100),
    new Point(150, 130),
    new Point(120, 170),
    new Point( 90, 150),
    new Point(100, 100)
};

Point point = new Point(100, 150);

bool result = IsPointInPolygon(polygonPointArray, point);

 

▶ Point 클래스 : 다각형 내부 위치 여부 구하기 (C#)

#region 다각형 내부 위치 여부 구하기 - IsPointInPolygon(polygonPointEnumerable, point)

/// <summary>
/// 다각형 내부 위치 여부 구하기
/// </summary>
/// <param name="polygonPointEnumerable">다각형 포인트 열거 가능형</param>
/// <param name="point">포인트</param>
/// <returns>다각형 내부 위치 여부</returns>
public bool IsPointInPolygon(IEnumerable<Point> polygonPointEnumerable, Point point)
{
    bool inside = false;

    Point currentPoint = polygonPointEnumerable.Last();

    foreach(Point polygonPoint in polygonPointEnumerable)
    {
        if((polygonPoint.X == point.X) && (polygonPoint.Y == point.Y))
        {
            return true;
        }

        if((polygonPoint.Y == currentPoint.Y) && (point.Y == currentPoint.Y))
        {
            if((currentPoint.X <= point.X) && (point.X <= polygonPoint.X))
            {
                return true;
            }

            if((polygonPoint.X <= point.X) && (point.X <= currentPoint.X))
            {
                return true;
            }
        }

        if((polygonPoint.Y < point.Y) && (currentPoint.Y >= point.Y) || (currentPoint.Y < point.Y) && (polygonPoint.Y >= point.Y))
        {
            if(polygonPoint.X + (point.Y - polygonPoint.Y) / (currentPoint.Y - polygonPoint.Y) * (currentPoint.X - polygonPoint.X) <= point.X)
            {
                inside = !inside;
            }
        }

        currentPoint = polygonPoint;
    }
    return inside;
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요