■ Point 클래스를 사용해 다각형 내부 위치 여부를 구하는 방법을 보여준다.
▶ Point 클래스 : 다각형 내부 위치 여부 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
List<Point> polygonPointList = new List<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(polygonPointList, 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 51 52 53 54 55 56 57 58 59 60 61 |
#region 다각형 내부 위치 여부 구하기 - IsPointInPolygon(polygonPointList, point) /// <summary> /// 다각형 내부 위치 여부 구하기 /// </summary> /// <param name="polygonPointList">다각형 포인트 리스트</param> /// <param name="point">포인트</param> /// <returns>다각형 내부 위치 여부</returns> public bool IsPointInPolygon(IList<Point> polygonPointList, Point point) { List<int> intersectList = new List<int>(); Point currentPoint = polygonPointList.Last(); foreach(Point polygonPoint in polygonPointList) { if(polygonPoint.X == point.X && polygonPoint.Y == point.Y) { return true; } if ( polygonPoint.X == currentPoint.X && point.X == currentPoint.X && point.X >= Math.Min(currentPoint.Y, polygonPoint.Y) && point.Y <= Math.Max(currentPoint.Y, polygonPoint.Y) ) { return true; } if ( polygonPoint.Y == currentPoint.Y && point.Y == currentPoint.Y && point.X >= Math.Min(currentPoint.X, polygonPoint.X) && point.X <= Math.Max(currentPoint.X, polygonPoint.X) ) { return true; } if((polygonPoint.Y < point.Y && currentPoint.Y >= point.Y) || (currentPoint.Y < point.Y && polygonPoint.Y >= point.Y)) { int pointX = (int)(polygonPoint.X + 1.0 * (point.Y - polygonPoint.Y) / (currentPoint.Y - polygonPoint.Y) * (currentPoint.X - polygonPoint.X)); intersectList.Add(pointX); } currentPoint = polygonPoint; } intersectList.Sort(); return intersectList.IndexOf(point.X) % 2 == 0 || intersectList.Count(x => x < point.X) % 2 == 1; } #endregion |