// -----------------------------------------------------------------------
//
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
//
// -----------------------------------------------------------------------
namespace UnityEngine.U2D.Animation.TriangleNet
.Geometry
{
using System;
using System.Collections.Generic;
///
/// A simple rectangle class.
///
internal class Rectangle
{
double xmin, ymin, xmax, ymax;
///
/// Initializes a new instance of the class.
///
public Rectangle()
{
this.xmin = this.ymin = double.MaxValue;
this.xmax = this.ymax = -double.MaxValue;
}
public Rectangle(Rectangle other)
: this(other.Left, other.Bottom, other.Right, other.Top)
{
}
///
/// Initializes a new instance of the class
/// with predefined bounds.
///
/// Minimum x value (left).
/// Minimum y value (bottom).
/// Width of the rectangle.
/// Height of the rectangle.
public Rectangle(double x, double y, double width, double height)
{
this.xmin = x;
this.ymin = y;
this.xmax = x + width;
this.ymax = y + height;
}
///
/// Gets the minimum x value (left boundary).
///
public double Left
{
get { return xmin; }
}
///
/// Gets the maximum x value (right boundary).
///
public double Right
{
get { return xmax; }
}
///
/// Gets the minimum y value (bottom boundary).
///
public double Bottom
{
get { return ymin; }
}
///
/// Gets the maximum y value (top boundary).
///
public double Top
{
get { return ymax; }
}
///
/// Gets the width of the rectangle.
///
public double Width
{
get { return xmax - xmin; }
}
///
/// Gets the height of the rectangle.
///
public double Height
{
get { return ymax - ymin; }
}
///
/// Update bounds.
///
/// Add dx to left and right bounds.
/// Add dy to top and bottom bounds.
public void Resize(double dx, double dy)
{
xmin -= dx;
xmax += dx;
ymin -= dy;
ymax += dy;
}
///
/// Expand rectangle to include given point.
///
/// Point.
public void Expand(Point p)
{
xmin = Math.Min(xmin, p.x);
ymin = Math.Min(ymin, p.y);
xmax = Math.Max(xmax, p.x);
ymax = Math.Max(ymax, p.y);
}
///
/// Expand rectangle to include a list of points.
///
public void Expand(IEnumerable points)
{
foreach (var p in points)
{
Expand(p);
}
}
///
/// Expand rectangle to include given rectangle.
///
/// X coordinate.
/// Y coordinate.
public void Expand(Rectangle other)
{
xmin = Math.Min(xmin, other.xmin);
ymin = Math.Min(ymin, other.ymin);
xmax = Math.Max(xmax, other.xmax);
ymax = Math.Max(ymax, other.ymax);
}
///
/// Check if given point is inside rectangle.
///
/// Point to check.
/// Point to check.
/// Return true, if rectangle contains given point.
public bool Contains(double x, double y)
{
return ((x >= xmin) && (x <= xmax) && (y >= ymin) && (y <= ymax));
}
///
/// Check if given point is inside rectangle.
///
/// Point to check.
/// Return true, if rectangle contains given point.
public bool Contains(Point pt)
{
return Contains(pt.x, pt.y);
}
///
/// Check if this rectangle contains other rectangle.
///
/// Rectangle to check.
/// Return true, if this rectangle contains given rectangle.
public bool Contains(Rectangle other)
{
return (xmin <= other.Left && other.Right <= xmax
&& ymin <= other.Bottom && other.Top <= ymax);
}
///
/// Check if this rectangle intersects other rectangle.
///
/// Rectangle to check.
/// Return true, if given rectangle intersects this rectangle.
public bool Intersects(Rectangle other)
{
return (other.Left < xmax && xmin < other.Right
&& other.Bottom < ymax && ymin < other.Top);
}
}
}