using System; using System.Runtime.InteropServices; namespace SFML { namespace Graphics { //////////////////////////////////////////////////////////// /// /// IntRect is an utility class for manipulating 2D rectangles /// with integer coordinates /// //////////////////////////////////////////////////////////// [StructLayout(LayoutKind.Sequential)] public struct IntRect { //////////////////////////////////////////////////////////// /// /// Construct the rectangle from its coordinates /// /// Left coordinate of the rectangle /// Top coordinate of the rectangle /// Right coordinate of the rectangle /// Bottom coordinate of the rectangle //////////////////////////////////////////////////////////// public IntRect(int left, int top, int right, int bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } //////////////////////////////////////////////////////////// /// /// Width of the rectangle /// //////////////////////////////////////////////////////////// public int Width { get {return Right - Left;} } //////////////////////////////////////////////////////////// /// /// Height of the rectangle /// //////////////////////////////////////////////////////////// public int Height { get {return Bottom - Top;} } //////////////////////////////////////////////////////////// /// /// Move the whole rectangle by the given offset /// /// Horizontal offset /// Vertical offset //////////////////////////////////////////////////////////// public void Offset(int offsetX, int offsetY) { Left += offsetX; Top += offsetY; Right += offsetX; Bottom += offsetY; } //////////////////////////////////////////////////////////// /// /// Check if a point is inside the rectangle's area /// /// X coordinate of the point to test /// Y coordinate of the point to test /// True if the point is inside //////////////////////////////////////////////////////////// public bool Contains(int x, int y) { return (x >= Left) && (x <= Right) && (y >= Top) && (y <= Bottom); } //////////////////////////////////////////////////////////// /// /// Check intersection between two rectangles /// /// Rectangle to test /// True if rectangles overlap //////////////////////////////////////////////////////////// public bool Intersects(IntRect rect) { return ((Math.Max(Left, rect.Left) < Math.Min(Right, rect.Right)) && (Math.Max(Top, rect.Top) < Math.Min(Bottom, rect.Bottom))); } //////////////////////////////////////////////////////////// /// /// Check intersection between two rectangles /// /// Rectangle to test /// Rectangle to be filled with overlapping rect /// True if rectangles overlap //////////////////////////////////////////////////////////// public bool Intersects(IntRect rect, out IntRect overlap) { IntRect Overlapping = new IntRect(Math.Max(Left, rect.Left), Math.Max(Top, rect.Top), Math.Min(Right, rect.Right), Math.Min(Bottom, rect.Bottom)); if ((Overlapping.Left < Overlapping.Right) && (Overlapping.Top < Overlapping.Bottom)) { overlap.Left = Overlapping.Left; overlap.Top = Overlapping.Top; overlap.Right = Overlapping.Right; overlap.Bottom = Overlapping.Bottom; return true; } else { overlap.Left = 0; overlap.Top = 0; overlap.Right = 0; overlap.Bottom = 0; return false; } } //////////////////////////////////////////////////////////// /// /// Provide a string describing the object /// /// String description of the object //////////////////////////////////////////////////////////// public override string ToString() { return "[IntRect]" + " Left(" + Left + ")" + " Top(" + Top + ")" + " Right(" + Right + ")" + " Bottom(" + Bottom + ")"; } /// Left coordinate of the rectangle public int Left; /// Top coordinate of the rectangle public int Top; /// Right coordinate of the rectangle public int Right; /// Bottom coordinate of the rectangle public int Bottom; } //////////////////////////////////////////////////////////// /// /// FloatRect is an utility class for manipulating 2D rectangles /// with float coordinates /// //////////////////////////////////////////////////////////// [StructLayout(LayoutKind.Sequential)] public struct FloatRect { //////////////////////////////////////////////////////////// /// /// Construct the rectangle from its coordinates /// /// Left coordinate of the rectangle /// Top coordinate of the rectangle /// Right coordinate of the rectangle /// Bottom coordinate of the rectangle //////////////////////////////////////////////////////////// public FloatRect(float left, float top, float right, float bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } //////////////////////////////////////////////////////////// /// /// Width of the rectangle /// //////////////////////////////////////////////////////////// public float Width { get {return Right - Left;} } //////////////////////////////////////////////////////////// /// /// Height of the rectangle /// //////////////////////////////////////////////////////////// public float Height { get {return Bottom - Top;} } //////////////////////////////////////////////////////////// /// /// Move the whole rectangle by the given offset /// /// Horizontal offset /// Vertical offset //////////////////////////////////////////////////////////// public void Offset(float offsetX, float offsetY) { Left += offsetX; Top += offsetY; Right += offsetX; Bottom += offsetY; } //////////////////////////////////////////////////////////// /// /// Check if a point is inside the rectangle's area /// /// X coordinate of the point to test /// Y coordinate of the point to test /// True if the point is inside //////////////////////////////////////////////////////////// public bool Contains(float x, float y) { return (x >= Left) && (x <= Right) && (y >= Top) && (y <= Bottom); } //////////////////////////////////////////////////////////// /// /// Check intersection between two rectangles /// /// Rectangle to test /// True if rectangles overlap //////////////////////////////////////////////////////////// public bool Intersects(FloatRect rect) { return ((Math.Max(Left, rect.Left) < Math.Min(Right, rect.Right)) && (Math.Max(Top, rect.Top) < Math.Min(Bottom, rect.Bottom))); } //////////////////////////////////////////////////////////// /// /// Check intersection between two rectangles /// /// Rectangle to test /// Rectangle to be filled with overlapping rect /// True if rectangles overlap //////////////////////////////////////////////////////////// public bool Intersects(FloatRect rect, out FloatRect overlap) { FloatRect Overlapping = new FloatRect(Math.Max(Left, rect.Left), Math.Max(Top, rect.Top), Math.Min(Right, rect.Right), Math.Min(Bottom, rect.Bottom)); if ((Overlapping.Left < Overlapping.Right) && (Overlapping.Top < Overlapping.Bottom)) { overlap.Left = Overlapping.Left; overlap.Top = Overlapping.Top; overlap.Right = Overlapping.Right; overlap.Bottom = Overlapping.Bottom; return true; } else { overlap.Left = 0; overlap.Top = 0; overlap.Right = 0; overlap.Bottom = 0; return false; } } //////////////////////////////////////////////////////////// /// /// Provide a string describing the object /// /// String description of the object //////////////////////////////////////////////////////////// public override string ToString() { return "[FloatRect]" + " Left(" + Left + ")" + " Top(" + Top + ")" + " Right(" + Right + ")" + " Bottom(" + Bottom + ")"; } /// Left coordinate of the rectangle public float Left; /// Top coordinate of the rectangle public float Top; /// Right coordinate of the rectangle public float Right; /// Bottom coordinate of the rectangle public float Bottom; } } }