2009-01-28 16:18:34 +00:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace SFML
|
|
|
|
{
|
|
|
|
namespace Graphics
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// IntRect is an utility class for manipulating 2D rectangles
|
|
|
|
/// with integer coordinates
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct IntRect
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Construct the rectangle from its coordinates
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">Left coordinate of the rectangle</param>
|
|
|
|
/// <param name="top">Top coordinate of the rectangle</param>
|
2010-04-09 17:05:21 +00:00
|
|
|
/// <param name="width">Width of the rectangle</param>
|
|
|
|
/// <param name="height">Height of the rectangle</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
2010-04-09 17:05:21 +00:00
|
|
|
public IntRect(int left, int top, int width, int height)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
|
|
|
Left = left;
|
|
|
|
Top = top;
|
2010-04-09 17:05:21 +00:00
|
|
|
Width = width;
|
|
|
|
Height = height;
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Check if a point is inside the rectangle's area
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">X coordinate of the point to test</param>
|
|
|
|
/// <param name="y">Y coordinate of the point to test</param>
|
|
|
|
/// <returns>True if the point is inside</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public bool Contains(int x, int y)
|
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
return (x >= Left) && (x < Left + Width) && (y >= Top) && (y < Top + Height);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Check intersection between two rectangles
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rect"> Rectangle to test</param>
|
|
|
|
/// <returns>True if rectangles overlap</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public bool Intersects(IntRect rect)
|
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
// 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);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Check intersection between two rectangles
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rect"> Rectangle to test</param>
|
|
|
|
/// <param name="overlap">Rectangle to be filled with overlapping rect</param>
|
|
|
|
/// <returns>True if rectangles overlap</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public bool Intersects(IntRect rect, out IntRect overlap)
|
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
// 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);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
2010-04-09 17:05:21 +00:00
|
|
|
// If the intersection is valid (positive non zero area), then there is an intersection
|
|
|
|
if ((left < right) && (top < bottom))
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
overlap.Left = left;
|
|
|
|
overlap.Top = top;
|
|
|
|
overlap.Width = right - left;
|
|
|
|
overlap.Height = bottom - top;
|
2009-01-28 16:18:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
overlap.Left = 0;
|
|
|
|
overlap.Top = 0;
|
2010-04-09 17:05:21 +00:00
|
|
|
overlap.Width = 0;
|
|
|
|
overlap.Height = 0;
|
2009-01-28 16:18:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 12:37:29 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Provide a string describing the object
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>String description of the object</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return "[IntRect]" +
|
2010-01-17 10:03:46 +00:00
|
|
|
" Left(" + Left + ")" +
|
|
|
|
" Top(" + Top + ")" +
|
2010-04-09 17:05:21 +00:00
|
|
|
" Width(" + Width + ")" +
|
|
|
|
" Height(" + Height + ")";
|
2010-01-06 12:37:29 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <summary>Left coordinate of the rectangle</summary>
|
|
|
|
public int Left;
|
|
|
|
|
|
|
|
/// <summary>Top coordinate of the rectangle</summary>
|
|
|
|
public int Top;
|
|
|
|
|
2010-04-09 17:05:21 +00:00
|
|
|
/// <summary>Width of the rectangle</summary>
|
|
|
|
public int Width;
|
2009-01-28 16:18:34 +00:00
|
|
|
|
2010-04-09 17:05:21 +00:00
|
|
|
/// <summary>Height of the rectangle</summary>
|
|
|
|
public int Height;
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2010-04-09 17:05:21 +00:00
|
|
|
/// IntRect is an utility class for manipulating 2D rectangles
|
2009-01-28 16:18:34 +00:00
|
|
|
/// with float coordinates
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct FloatRect
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Construct the rectangle from its coordinates
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">Left coordinate of the rectangle</param>
|
|
|
|
/// <param name="top">Top coordinate of the rectangle</param>
|
2010-04-09 17:05:21 +00:00
|
|
|
/// <param name="width">Width of the rectangle</param>
|
|
|
|
/// <param name="height">Height of the rectangle</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
2010-04-09 17:05:21 +00:00
|
|
|
public FloatRect(float left, float top, float width, float height)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
|
|
|
Left = left;
|
|
|
|
Top = top;
|
2010-04-09 17:05:21 +00:00
|
|
|
Width = width;
|
|
|
|
Height = height;
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Check if a point is inside the rectangle's area
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">X coordinate of the point to test</param>
|
|
|
|
/// <param name="y">Y coordinate of the point to test</param>
|
|
|
|
/// <returns>True if the point is inside</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public bool Contains(float x, float y)
|
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
return (x >= Left) && (x < Left + Width) && (y >= Top) && (y < Top + Height);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Check intersection between two rectangles
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rect"> Rectangle to test</param>
|
|
|
|
/// <returns>True if rectangles overlap</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public bool Intersects(FloatRect rect)
|
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
// 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);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Check intersection between two rectangles
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rect"> Rectangle to test</param>
|
|
|
|
/// <param name="overlap">Rectangle to be filled with overlapping rect</param>
|
|
|
|
/// <returns>True if rectangles overlap</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public bool Intersects(FloatRect rect, out FloatRect overlap)
|
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
// 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);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
2010-04-09 17:05:21 +00:00
|
|
|
// If the intersection is valid (positive non zero area), then there is an intersection
|
|
|
|
if ((left < right) && (top < bottom))
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2010-04-09 17:05:21 +00:00
|
|
|
overlap.Left = left;
|
|
|
|
overlap.Top = top;
|
|
|
|
overlap.Width = right - left;
|
|
|
|
overlap.Height = bottom - top;
|
2009-01-28 16:18:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
overlap.Left = 0;
|
|
|
|
overlap.Top = 0;
|
2010-04-09 17:05:21 +00:00
|
|
|
overlap.Width = 0;
|
|
|
|
overlap.Height = 0;
|
2009-01-28 16:18:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 12:37:29 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Provide a string describing the object
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>String description of the object</returns>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return "[FloatRect]" +
|
2010-01-17 10:03:46 +00:00
|
|
|
" Left(" + Left + ")" +
|
|
|
|
" Top(" + Top + ")" +
|
2010-04-09 17:05:21 +00:00
|
|
|
" Width(" + Width + ")" +
|
|
|
|
" Height(" + Height + ")";
|
2010-01-06 12:37:29 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <summary>Left coordinate of the rectangle</summary>
|
|
|
|
public float Left;
|
|
|
|
|
|
|
|
/// <summary>Top coordinate of the rectangle</summary>
|
|
|
|
public float Top;
|
|
|
|
|
2010-04-09 17:05:21 +00:00
|
|
|
/// <summary>Width of the rectangle</summary>
|
|
|
|
public float Width;
|
2009-01-28 16:18:34 +00:00
|
|
|
|
2010-04-09 17:05:21 +00:00
|
|
|
/// <summary>Height of the rectangle</summary>
|
|
|
|
public float Height;
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|