#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.Collections.Generic; using Microsoft.Xna.Framework; namespace Nuclex.Geometry.Lines.Collisions { /// /// Detects intersections of infinite 2D lines with 2D discs / circles /// public static class Line2Disc2Collider { /// Determines the contact location between a line and a disc /// /// Offset of the line relative to the disc's center /// /// Direction and length of the line /// Radius of the disc /// The point of intersection of the line with the disc, if any /// /// /// Shamelessly lifted from the FreeMagic library at http://www.magic-software.com /// and used as a supporting function for the other line/sphere contact finders. /// /// internal static LineContacts FindContacts( Vector2 lineOffset, Vector2 lineDirection, float discRadius ) { float a0 = lineOffset.LengthSquared() - discRadius * discRadius; float a1 = Vector2.Dot(lineDirection, lineOffset); float discrete = a1 * a1 - a0; if(discrete > 0.0f) { discrete = (float)Math.Sqrt(discrete); return new LineContacts(-a1 - discrete, -a1 + discrete); } else { return LineContacts.None; } } /// Determines the contact location between a line and a disc /// /// Offset of the line from the coordinate system's center /// /// Direction and length of the line /// Position of the disc /// Radius of the disc /// The point of intersection of the line with the disc, if any internal static LineContacts FindContacts( Vector2 lineOffset, Vector2 lineDirection, Vector2 discCenter, float discRadius ) { return FindContacts(lineOffset - discCenter, lineDirection, discRadius); } } } // namespace Nuclex.Geometry.Lines.Collisions