MathUtility.cs 1.4 KB

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