MathUtility.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MathUtility.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. #endregion
  15. namespace CardsFramework
  16. {
  17. public static class MathUtility
  18. {
  19. /// <summary>
  20. /// Rotates a point around another specified point.
  21. /// </summary>
  22. /// <param name="point">The point to rotate.</param>
  23. /// <param name="origin">The rotation origin or "axis".</param>
  24. /// <param name="rotation">The rotation amount in radians.</param>
  25. /// <returns>The position of the point after rotating it.</returns>
  26. public static Vector2 RotateAboutOrigin(Vector2 point, Vector2 origin, float rotation)
  27. {
  28. // Point relative to origin
  29. Vector2 u = point - origin;
  30. if (u == Vector2.Zero)
  31. return point;
  32. // Angle relative to origin
  33. float a = (float)Math.Atan2(u.Y, u.X);
  34. // Rotate
  35. a += rotation;
  36. // U is now the new point relative to origin
  37. u = u.Length() * new Vector2((float)Math.Cos(a), (float)Math.Sin(a));
  38. return u + origin;
  39. }
  40. }
  41. }