Exemple #1
0
        public static bool IsPointInPolygon(Vector2 point, Polygon poly, bool trueOnEdge = true, bool useWindingMethod = true)
        {
            if (trueOnEdge)
            {
                bool onEdge = false;
                for (var n = 0; n < poly.NumVertices; n++)
                {
                    var next = n == poly.NumVertices - 1 ? 0 : n + 1;
                    if (LineIntersection.IsPointOnLineSegment(point, poly.Vertices[n], poly.Vertices[next]))
                    {
                        onEdge = true;
                    }
                    if (onEdge)
                    {
                        return(true);
                    }
                }
            }

            if (useWindingMethod)
            {
                return(CalculatePolygonWinding(poly, point) != 0);
            }
            else
            {
                return(CalculatePolyCrossing(poly, point));
            }
        }
Exemple #2
0
        public static bool CheckLineIntersect(AABB aabb, Vector2 p0, Vector2 p1)
        {
            if (aabb.Contains(p0))
            {
                return(true);
            }

            if (aabb.Contains(p1))
            {
                return(true);
            }

            var points = new Vector2[]
            {
                new Vector2(aabb.Left, aabb.Top),
                new Vector2(aabb.Right, aabb.Top),
                new Vector2(aabb.Right, aabb.Bottom),
                new Vector2(aabb.Left, aabb.Bottom)
            };

            for (var n = 0; n < 4; n++)
            {
                var m = n + 1;
                if (m == 4)
                {
                    m = 0;
                }

                if (LineIntersection.RobustLineIntersect(points[n], points[m], p0, p1))
                {
                    return(true);
                }
            }

            return(false);
        }