#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 { /// A two-dimensional circle public class Disc2 : IArea2 { /// Initializes a new circle /// The center of the circle /// Radius the circle will have [DebuggerStepThrough] public Disc2(Vector2 center, float radius) { this.Center = center; this.Radius = radius; } /// Surface area that the shape contains public float Area { get { return MathHelper.Pi * (this.Radius * this.Radius); } } /// The total length of the area's circumference public float CircumferenceLength { get { return 2.0f * MathHelper.Pi * this.Radius; } } /// The center of mass within the shape public Vector2 CenterOfMass { get { return Center; } } /// Smallest rectangle that encloses the shape in its entirety public AxisAlignedRectangle2 BoundingBox { get { return new AxisAlignedRectangle2( new Vector2(this.Center.X - this.Radius, this.Center.Y - this.Radius), new Vector2(this.Center.X + this.Radius, this.Center.Y + this.Radius) ); } } /// Locates the nearest point on the disc to some arbitrary location /// Location to which the closest point is determined /// The closest point on the disc to the specified location public Vector2 ClosestPointTo(Vector2 location) { Vector2 offset = location - Center; float distance = offset.Length(); if(distance > this.Radius) { float distanceFactor = this.Radius / offset.Length(); return this.Center + offset * distanceFactor; } else { return location; } } /// 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) { return this.Center + PointGenerators.Disc2PointGenerator.GenerateRandomPointOnPerimeter( randomNumberGenerator, this.Radius ); } /// 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) { return this.Center + PointGenerators.Disc2PointGenerator.GenerateRandomPointWithin( randomNumberGenerator, this.Radius ); } // TODO: Use matrix. Solves elipsis. XNA Matrix is intended for both 2D and 3D. /// The center of the disc public Vector2 Center; /// Radius of the disc public float Radius; } } // namespace Nuclex.Geometry.Areas