Blame mono/Assistance/Rectangle.cs

cfceb0
using System;
cfceb0
cfceb0
namespace Assistance {
cfceb0
	public struct Rectangle {
cfceb0
		public double x0, y0, x1, y1;
cfceb0
cfceb0
		public Point p0 {
cfceb0
			get { return new Point(x0, y0); }
cfceb0
			set { x0 = value.x; y0 = value.y; }
cfceb0
		}
cfceb0
cfceb0
		public Point p1 {
cfceb0
			get { return new Point(x1, y1); }
cfceb0
			set { x1 = value.x; y1 = value.y; }
cfceb0
		}
cfceb0
cfceb0
		public double width {
cfceb0
			get { return x1 - x0; }
cfceb0
			set { x1 = x0 + value; }
cfceb0
		}
cfceb0
cfceb0
		public double height {
cfceb0
			get { return y1 - y0; }
cfceb0
			set { y1 = y0 + value; }
cfceb0
		}
cfceb0
cfceb0
		public bool empty {
cfceb0
			get { return width <= Geometry.precision || height <= Geometry.precision; }
cfceb0
		}
cfceb0
cfceb0
		public Rectangle(double x0, double y0, double x1, double y1) {
cfceb0
			this.x0 = x0;
cfceb0
			this.y0 = y0;
cfceb0
			this.x1 = x1;
cfceb0
			this.y1 = y1;
cfceb0
		}
cfceb0
		public Rectangle(double x, double y):
cfceb0
			this(x, y, x, y) { }
cfceb0
		public Rectangle(Point p):
cfceb0
			this(p.x, p.y) { }
cfceb0
		public Rectangle(Point p0, Point p1):
cfceb0
			this(p0.x, p0.y, p1.x, p1.y) { }
cfceb0
cfceb0
		public Rectangle expand(Point p, double radius = 0.0) {
cfceb0
			return new Rectangle(
cfceb0
				Math.Min(x0, p.x),
cfceb0
				Math.Min(y0, p.y),
cfceb0
				Math.Max(x1, p.x),
cfceb0
				Math.Max(y1, p.y) );
cfceb0
		}
cfceb0
cfceb0
		public Rectangle inflate(double x, double y) {
cfceb0
			return new Rectangle(
cfceb0
			    x0 - x,
cfceb0
			    y0 - y,
cfceb0
			    x1 + x,
cfceb0
			    y1 + y );
cfceb0
		}
cfceb0
cfceb0
		public Rectangle inflate(double size) {
cfceb0
			return inflate(size, size);
cfceb0
		}
cfceb0
cfceb0
		public static Rectangle operator| (Rectangle a, Rectangle b) {
cfceb0
			Rectangle rect = a.empty ? b
cfceb0
				           : b.empty ? a
cfceb0
					       : new Rectangle(	Math.Min(a.x0, b.x0),
cfceb0
								        Math.Min(a.y0, b.y0),
cfceb0
								        Math.Max(a.x1, b.x1),
cfceb0
					                    Math.Max(a.y1, b.y1) );
cfceb0
			return rect.empty ? new Rectangle() : rect;
cfceb0
		}
cfceb0
cfceb0
		public static Rectangle operator& (Rectangle a, Rectangle b) {
cfceb0
			Rectangle rect = a.empty ? b
cfceb0
				           : b.empty ? a
cfceb0
					       : new Rectangle( Math.Max(a.x0, b.x0),
cfceb0
					                    Math.Max(a.y0, b.y0),
cfceb0
					                    Math.Min(a.x1, b.x1),
cfceb0
					                    Math.Min(a.y1, b.y1) );
cfceb0
			return rect.empty ? new Rectangle() : rect;
cfceb0
		}
cfceb0
	}
cfceb0
}
cfceb0