Sharpex2D.Math.Rectangle.Intersects C# (CSharp) Method

Intersects() public method

Intersectses the specified rectangle.
public Intersects ( Rectangle rectangle ) : bool
rectangle Rectangle The rectangle.
return bool
        public bool Intersects(Rectangle rectangle)
        {
            return
                !(Left > rectangle.Right ||
                  Right < rectangle.Left ||
                  Top > rectangle.Bottom ||
                  Bottom < rectangle.Top);
        }

Usage Example

示例#1
0
        /// <summary>
        ///     A value indicating whether the player intersects.
        /// </summary>
        /// <param name="player">The Player.</param>
        /// <returns>True if intersecting.</returns>
        public bool Intersects(Player player)
        {
            lock (_lockObj)
            {
                foreach (Pipe pipe in _pipes)
                {
                    /*/var polygon1 = new Polygon();
                    polygon1.Add(new Vector2(pipe.Position.X, pipe.Position.Y),
                        new Vector2(pipe.Position.X + 44, pipe.Position.Y),
                        new Vector2(pipe.Position.X + 44, pipe.Position.Y + pipe.TopPipeHeight + 22),
                        new Vector2(pipe.Position.X, pipe.Position.Y + pipe.TopPipeHeight + 22));

                    var polygon2 = new Polygon();
                    polygon2.Add(new Vector2(pipe.Position.X, 480), new Vector2(pipe.Position.X + 44, 480),
                      new Vector2(pipe.Position.X + 44, pipe.BottomPipeY - 22),
                                   new Vector2(pipe.Position.X, pipe.BottomPipeY - 22));

                    if (player.Bounds.Intersects(polygon1))
                    {
                        return true;
                    }

                    if (player.Bounds.Intersects(polygon2))
                    {
                        return true;
                    }/*/

                    var rect1 = new Rectangle(pipe.Position.X, pipe.Position.Y, 46, pipe.TopPipeHeight + 22);
                    var rect2 = new Rectangle(pipe.Position.X, pipe.BottomPipeY, 46, pipe.BottomPipeHeight);
                    var playerrect = new Rectangle(player.Position, new Vector2(32, 24));

                    if (playerrect.Intersects(rect1) || playerrect.Intersects(rect2))
                    {
                        return true;
                    }
                }
            }
            return false;
        }