#region CPL License /* Nuclex Framework Copyright (C) 2002-2009 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Diagnostics; using Microsoft.Xna.Framework; namespace Nuclex.Geometry.Areas { /// Two-dimensional axis aligned rectangle public class AxisAlignedRectangle2 : IArea2 { /// Initializes the axis aligned rectangle /// Lower left bounds of the rectangle /// Upper right bounds of the rectangle [DebuggerStepThrough] public AxisAlignedRectangle2(Vector2 min, Vector2 max) { Min = min; Max = max; } /// The width of the rectangle public float Width { get { return this.Max.X - this.Min.X; } } /// The height of the rectangle public float Height { get { return this.Max.Y - this.Min.Y; } } /// Surface area that the shape contains public float Area { get { return Width * Height; } } /// The total length of the area's circumference public float CircumferenceLength { get { return Width * 2 + Height * 2; } } /// The center of mass within the shape public Vector2 CenterOfMass { get { return (this.Min + this.Max) / 2.0f; } } /// Smallest rectangle that encloses the shape in its entirety public AxisAlignedRectangle2 BoundingBox { get { return this; } } /// Locates the nearest point in the area to some arbitrary location /// Location to which the closest point is determined /// The closest point in the area to the specified location public Vector2 ClosestPointTo(Vector2 location) { return new Vector2( Math.Min(Math.Max(location.X, Min.X), Max.X), Math.Min(Math.Max(location.Y, Min.Y), Max.Y) ); } /// Returns a random point on the area's perimeter /// Random number generator that will be used /// A random point on the area's perimeter public Vector2 RandomPointOnPerimeter(IRandom randomNumberGenerator) { Vector2 center = (this.Max + this.Min) / 2.0f; Vector2 extents = (this.Max - center); return PointGenerators.Rectangle2PointGenerator.GenerateRandomPointOnPerimeter( randomNumberGenerator, this.Min, this.Max ); } /// Returns a random point inside the area /// Random number generator that will be used /// A random point inside the area public Vector2 RandomPointWithin(IRandom randomNumberGenerator) { Vector2 center = (this.Max + this.Min) / 2.0f; Vector2 extents = (this.Max - center); return center + PointGenerators.Rectangle2PointGenerator.GenerateRandomPointWithin( randomNumberGenerator, extents ); } /// Top left bounds of the box public Vector2 Min; /// Bottom right bounds of the box public Vector2 Max; } } // namespace Nuclex.Geometry.Areas