Polygon.IsPointInside C# (CSharp) Method

IsPointInside() public method

public IsPointInside ( Vector2 point ) : bool
point Vector2
return bool
    public bool IsPointInside(Vector2 point)
    {
        Vector2D[] farFarAwayPoints = new Vector2D[]{
            new Vector2D(point) + new Vector2D(99999,99998),
            new Vector2D(point) + new Vector2D(-99999,89990),
        };
        LineSegment2D[] lineSegments = new LineSegment2D[] {
            new LineSegment2D(new Vector2D(point), farFarAwayPoints[0]),
            new LineSegment2D(new Vector2D(point), farFarAwayPoints[1]),
        };
        foreach (var ls in lineSegments) {
            int hits = 0;
            for (int i = 0;i < vertices.Count; i++) {
                LineSegment2D l1 = new LineSegment2D(Polygon.V2(vertices[i]), Polygon.V2(vertices[(i+1)%vertices.Count]));
                Vector2D res = Vector2D.zero;
                if (ls.LineIntersect(l1,out res)){
                    hits++;
                }
            }
            if (hits % 2 == 1){
                return true;
            }
        }
        return false;
    }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Returns whether this instance collides with the argument Polygon.
        /// </summary>
        /// <param name="polygon">The Polygon to test collision against.</param>
        /// <returns>Whether a collision has occurred.</returns>
        #endregion
        public bool CollideAgainst(Polygon polygon)
        {
            UpdateDependencies(TimeManager.CurrentTime);
            polygon.UpdateDependencies(TimeManager.CurrentTime);

            Point transformedPoint1 = new Point(RelativePoint1.X, RelativePoint1.Y);
            Point transformedPoint2 = new Point(RelativePoint2.X, RelativePoint2.Y);


            FlatRedBall.Math.MathFunctions.TransformPoint(ref transformedPoint1, ref mRotationMatrix);
            FlatRedBall.Math.MathFunctions.TransformPoint(ref transformedPoint2, ref mRotationMatrix);

            Point tp1 = new Point(transformedPoint1.X, transformedPoint1.Y);
            Point tp2 = new Point(transformedPoint2.X, transformedPoint2.Y);

            // Get world-positioned segment
            Segment a = new Segment(
                new Point(Position.X + transformedPoint1.X,
                          Position.Y + transformedPoint1.Y),
                new Point(Position.X + transformedPoint2.X,
                          Position.Y + transformedPoint2.Y));

            // Check if one of the segment's endpoints is inside the Polygon
            if (polygon.IsPointInside(ref tp1, ref Position, ref mIdentityMatrix))
            {
                polygon.mLastCollisionPoint = tp1;
                mLastCollisionPoint         = tp1;
                return(true);
            }
            if (polygon.IsPointInside(ref tp2, ref Position, ref mIdentityMatrix))
            {
                polygon.mLastCollisionPoint = tp2;
                mLastCollisionPoint         = tp2;
                return(true);
            }

            Point intersectionPoint;

            // Check if one of the polygon's edges intersects the line segment
            for (int i = 0; i < polygon.Points.Count - 1; i++)
            {
                int indexAfter = i + 1;

                var point1 = new Point(polygon.Vertices[i].Position.X,
                                       polygon.Vertices[i].Position.Y);

                var point2 = new Point(polygon.Vertices[indexAfter].Position.X,
                                       polygon.Vertices[indexAfter].Position.Y);

                if (a.Intersects(new Segment(point1, point2), out intersectionPoint))
                {
                    mLastCollisionPoint         = intersectionPoint;
                    polygon.mLastCollisionPoint = intersectionPoint;
                    return(true);
                }
            }

            // No collision
            return(false);
        }
All Usage Examples Of Polygon::IsPointInside