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 /// Width of the rectangle /// Height of the rectangle //////////////////////////////////////////////////////////// public IntRect(int left, int top, int width, int height) { Left = left; Top = top; Width = width; Height = height; } //////////////////////////////////////////////////////////// /// /// 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 < Left + Width) && (y >= Top) && (y < Top + Height); } //////////////////////////////////////////////////////////// /// /// Check intersection between two rectangles /// /// Rectangle to test /// True if rectangles overlap //////////////////////////////////////////////////////////// public bool Intersects(IntRect rect) { // Compute the intersection boundaries int left = Math.Max(Left, rect.Left); int top = Math.Max(Top, rect.Top); int right = Math.Min(Left + Width, rect.Left + rect.Width); int bottom = Math.Min(Top + Height, rect.Top + rect.Height); return (left < right) && (top < 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) { // Compute the intersection boundaries int left = Math.Max(Left, rect.Left); int top = Math.Max(Top, rect.Top); int right = Math.Min(Left + Width, rect.Left + rect.Width); int bottom = Math.Min(Top + Height, rect.Top + rect.Height); // If the intersection is valid (positive non zero area), then there is an intersection if ((left < right) && (top < bottom)) { overlap.Left = left; overlap.Top = top; overlap.Width = right - left; overlap.Height = bottom - top; return true; } else { overlap.Left = 0; overlap.Top = 0; overlap.Width = 0; overlap.Height = 0; return false; } } //////////////////////////////////////////////////////////// /// /// Provide a string describing the object /// /// String description of the object //////////////////////////////////////////////////////////// public override string ToString() { return "[IntRect]" + " Left(" + Left + ")" + " Top(" + Top + ")" + " Width(" + Width + ")" + " Height(" + Height + ")"; } /// Left coordinate of the rectangle public int Left; /// Top coordinate of the rectangle public int Top; /// Width of the rectangle public int Width; /// Height of the rectangle public int Height; } //////////////////////////////////////////////////////////// /// /// IntRect 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 /// Width of the rectangle /// Height of the rectangle //////////////////////////////////////////////////////////// public FloatRect(float left, float top, float width, float height) { Left = left; Top = top; Width = width; Height = height; } //////////////////////////////////////////////////////////// /// /// 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 < Left + Width) && (y >= Top) && (y < Top + Height); } //////////////////////////////////////////////////////////// /// /// Check intersection between two rectangles /// /// Rectangle to test /// True if rectangles overlap //////////////////////////////////////////////////////////// public bool Intersects(FloatRect rect) { // Compute the intersection boundaries float left = Math.Max(Left, rect.Left); float top = Math.Max(Top, rect.Top); float right = Math.Min(Left + Width, rect.Left + rect.Width); float bottom = Math.Min(Top + Height, rect.Top + rect.Height); return (left < right) && (top < 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) { // Compute the intersection boundaries float left = Math.Max(Left, rect.Left); float top = Math.Max(Top, rect.Top); float right = Math.Min(Left + Width, rect.Left + rect.Width); float bottom = Math.Min(Top + Height, rect.Top + rect.Height); // If the intersection is valid (positive non zero area), then there is an intersection if ((left < right) && (top < bottom)) { overlap.Left = left; overlap.Top = top; overlap.Width = right - left; overlap.Height = bottom - top; return true; } else { overlap.Left = 0; overlap.Top = 0; overlap.Width = 0; overlap.Height = 0; return false; } } //////////////////////////////////////////////////////////// /// /// Provide a string describing the object /// /// String description of the object //////////////////////////////////////////////////////////// public override string ToString() { return "[FloatRect]" + " Left(" + Left + ")" + " Top(" + Top + ")" + " Width(" + Width + ")" + " Height(" + Height + ")"; } /// Left coordinate of the rectangle public float Left; /// Top coordinate of the rectangle public float Top; /// Width of the rectangle public float Width; /// Height of the rectangle public float Height; } } }