コード例 #1
0
ファイル: Polygon.cs プロジェクト: benov84/MathLibrary
        public ConvexPolygon2D GetIntersectionOfPolygons(ConvexPolygon2D poly1, ConvexPolygon2D poly2)
        {
            List <Point2D> clippedCorners = new List <Point2D>();

            //Add  the corners of poly1 which are inside poly2
            for (int i = 0; i < poly1.Corners.Length; i++)
            {
                if (IsPointInsidePoly(poly1.Corners[i], poly2))
                {
                    AddPoints(clippedCorners, new Point2D[] { poly1.Corners[i] });
                }
            }

            //Add the corners of poly2 which are inside poly1
            for (int i = 0; i < poly2.Corners.Length; i++)
            {
                if (IsPointInsidePoly(poly2.Corners[i], poly1))
                {
                    AddPoints(clippedCorners, new Point2D[] { poly2.Corners[i] });
                }
            }

            //Add  the intersection points
            for (int i = 0, next = 1; i < poly1.Corners.Length; i++, next = (i + 1 == poly1.Corners.Length) ? 0 : i + 1)
            {
                AddPoints(clippedCorners, GetIntersectionPoints(poly1.Corners[i], poly1.Corners[next], poly2));
            }

            return(new ConvexPolygon2D(OrderClockwise(clippedCorners.ToArray())));
        }
コード例 #2
0
ファイル: Polygon.cs プロジェクト: benov84/MathLibrary
        // taken from https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
        public bool IsPointInsidePoly(Point2D test, ConvexPolygon2D poly)
        {
            int  i;
            int  j;
            bool result = false;

            for (i = 0, j = poly.Corners.Length - 1; i < poly.Corners.Length; j = i++)
            {
                if ((poly.Corners[i].Y > test.Y) != (poly.Corners[j].Y > test.Y) &&
                    (test.X < (poly.Corners[j].X - poly.Corners[i].X) * (test.Y - poly.Corners[i].Y) / (poly.Corners[j].Y - poly.Corners[i].Y) + poly.Corners[i].X))
                {
                    result = !result;
                }
            }
            return(result);
        }
コード例 #3
0
ファイル: Polygon.cs プロジェクト: benov84/MathLibrary
        public virtual Point2D[] GetIntersectionPoints(Point2D l1p1, Point2D l1p2, ConvexPolygon2D poly)
        {
            List <Point2D> intersectionPoints = new List <Point2D>();

            for (int i = 0; i < poly.Corners.Length; i++)
            {
                int next = (i + 1 == poly.Corners.Length) ? 0 : i + 1;

                Point2D ip = GetIntersectionPoint(l1p1, l1p2, poly.Corners[i], poly.Corners[next]);

                if (ip != null)
                {
                    intersectionPoints.Add(ip);
                }
            }

            return(intersectionPoints.ToArray());
        }