■ Point 클래스를 사용해 다각형 내부 위치 여부를 구하는 방법을 보여준다.
▶ Point 클래스 : 다각형 내부 위치 여부 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#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 |