city-generation/Game/Assets/Scripts/Model/Rectangle.cs

78 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TransportGame.Model
{
public struct Rectangle
{
public float Left { get; set; }
public float Top { get; set; }
public float Right { get; set; }
public float Bottom { get; set; }
public Rectangle(float left, float top, float right, float bottom)
: this()
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
if (left > right)
throw new ArgumentException("Left must be smaller than right.");
if (top > bottom)
throw new ArgumentException("Top must be smaller than bottom.");
}
public float Width
{
get
{
return Right - Left;
}
set
{
Right = Left + value;
}
}
public float Height
{
get
{
return Bottom - Top;
}
set
{
Bottom = Top + value;
}
}
public bool Contains(float x, float y)
{
return x >= Left && x <= Right && y >= Top && y <= Bottom;
}
public bool Contains(Vector2 p)
{
return Contains(p.X, p.Y);
}
public static bool Intersects (Rectangle a, Rectangle b)
{
return !(b.Left > a.Right ||
b.Right < a.Left ||
b.Top > a.Bottom ||
b.Bottom < a.Top);
}
public override string ToString()
{
return string.Format("({0}, {1}, {2}, {3})", Left, Top, Right, Bottom);
}
}
}