/// <summary> /// Rectangle style constructor /// </summary> /// <param name="x">Left most X value</param> /// <param name="y">Top most Y value</param> /// <param name="width">Width of the box</param> /// <param name="height">Height of the box</param> public BoundingRectangle(float x, float y, float width, float height) { center = new Vector2(x + width / 2, y + height / 2); boundingRectangle = new RectangleFloat(x, y, width, height); dimensionsFromCenter = new Vector2(width / 2, height / 2); image = SpriteDatabase.Blank; }
/// <summary> /// Bounding sphere style constructor /// </summary> /// <param name="center">Center of the box</param> /// <param name="radius">Radius from center</param> public BoundingRectangle(Vector2 center, float radius) { dimensionsFromCenter = new Vector2(radius, radius); this.center = center; boundingRectangle = new RectangleFloat(center.X - radius, center.Y - radius, 2 * radius, 2 * radius); image = SpriteDatabase.Blank; }
/// <summary> /// Bounding sphere style constructor 2 /// </summary> /// <param name="center">Center of the box</param> /// <param name="x">X radius from center</param> /// <param name="y">Y radius from center</param> public BoundingRectangle(Vector2 center, float x, float y) { dimensionsFromCenter = new Vector2(x, y); this.center = center; boundingRectangle = new RectangleFloat(center.X - x, center.Y - y, 2 * x, 2 * y); image = SpriteDatabase.Blank; }
/// <summary> /// Copy constructor /// </summary> /// <param name="rect">Other rectangle</param> public RectangleFloat(RectangleFloat rect) { X = rect.X; Y = rect.Y; Width = rect.Width; Height = rect.Height; }
/// <summary> /// Checks for collision with a rectangle /// </summary> /// <param name="rectangle">Rectangle to check collision with</param> /// <returns>True if there is a collision</returns> public bool Collides(RectangleFloat rectangle) { return boundingRectangle.Intersects(rectangle); }
/// <summary> /// Checks for intersection between two rectangles /// </summary> /// <param name="rect">Other rectangle</param> /// <returns>Whether or not the two rectangles intersect</returns> public bool Intersects(RectangleFloat rect) { if (rect.Right < Left || rect.Left > Right || rect.Bottom < Top || rect.Top > Bottom) { return false; } return true; }
/// <summary> /// If rectangle contains another rectangle /// </summary> /// <param name="rectangle">Other rectangle</param> /// <param name="result">If rectangle contains another rectangle</result> public void Contains(ref RectangleFloat rectangle, out bool result) { result = rectangle.Left >= Left && rectangle.Right <= Right && rectangle.Top >= Top && rectangle.Bottom <= Bottom; }
/// <summary> /// If rectangle contains another rectangle /// </summary> /// <param name="rectangle">Other rectangle</param> /// <returns>If rectangle contains another rectangle</returns> public bool Contains(RectangleFloat rectangle) { return (rectangle.Left >= Left && rectangle.Right <= Right && rectangle.Top >= Top && rectangle.Bottom <= Bottom); }
/// <summary> /// Checks rectangles for intersection /// </summary> /// <param name="rect1">First rectangle</param> /// <param name="rect2">Second rectangle</param> /// <param name="result">Area of intersection</param> public static void Intersect(ref RectangleFloat rect1, ref RectangleFloat rect2, out RectangleFloat result) { if (rect1.Contains(rect2)) { result = new RectangleFloat(rect2); } else if (rect2.Contains(rect1)) { result = new RectangleFloat(rect1); } else if (!rect1.Intersects(rect2)) { result = new RectangleFloat(); } else { result = new RectangleFloat( Math.Max(rect1.X, rect2.X), Math.Max(rect1.Y, rect2.Y), Math.Min(rect1.Right, rect2.Right) - Math.Max(rect1.Left, rect2.Left), Math.Min(rect1.Bottom, rect2.Bottom) - Math.Max(rect1.Top, rect2.Top)); } }
/// <summary> /// Checks rectangles for intersection /// </summary> /// <param name="rect1">First rectangle</param> /// <param name="rect2">Second rectangle</param> /// <returns>Area of intersection</returns> public static RectangleFloat Intersect(RectangleFloat rect1, RectangleFloat rect2) { RectangleFloat result; if (rect1.Contains(rect2)) { result = new RectangleFloat(rect2); } else if (rect2.Contains(rect1)) { result = new RectangleFloat(rect1); } else if (!rect1.Intersects(rect2)) { result = new RectangleFloat(); } else { result = new RectangleFloat( Math.Max(rect1.X, rect2.X), Math.Max(rect1.Y, rect2.Y), Math.Min(rect1.Right, rect2.Right) - Math.Max(rect1.Left, rect2.Left), Math.Min(rect1.Bottom, rect2.Bottom) - Math.Max(rect1.Top, rect2.Top)); } return result; }