Circle.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Circle.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using Microsoft.Xna.Framework;
  11. namespace Platformer
  12. {
  13. /// <summary>
  14. /// Represents a 2D circle.
  15. /// </summary>
  16. struct Circle
  17. {
  18. /// <summary>
  19. /// Center position of the circle.
  20. /// </summary>
  21. public Vector2 Center;
  22. /// <summary>
  23. /// Radius of the circle.
  24. /// </summary>
  25. public float Radius;
  26. /// <summary>
  27. /// Constructs a new circle.
  28. /// </summary>
  29. public Circle(Vector2 position, float radius)
  30. {
  31. Center = position;
  32. Radius = radius;
  33. }
  34. /// <summary>
  35. /// Determines if a circle intersects a rectangle.
  36. /// </summary>
  37. /// <returns>True if the circle and rectangle overlap. False otherwise.</returns>
  38. public bool Intersects(Rectangle rectangle)
  39. {
  40. Vector2 v = new Vector2(MathHelper.Clamp(Center.X, rectangle.Left, rectangle.Right),
  41. MathHelper.Clamp(Center.Y, rectangle.Top, rectangle.Bottom));
  42. Vector2 direction = Center - v;
  43. float distanceSquared = direction.LengthSquared();
  44. return ((distanceSquared > 0) && (distanceSquared < Radius * Radius));
  45. }
  46. }
  47. }