Extensions.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //---------------------------------------------------------------------------------
  2. // Written by Michael Hoffman
  3. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  4. //----------------------------------------------------------------------------------
  5. using System;
  6. using AtomicEngine;
  7. namespace AtomicBlaster
  8. {
  9. static class Extensions
  10. {
  11. public static float ToAngle(this Vector2 vector)
  12. {
  13. return (float)Math.Atan2(vector.Y, vector.X);
  14. }
  15. public static Vector2 ScaleTo(this Vector2 vector, float length)
  16. {
  17. return vector * (length / vector.Length);
  18. }
  19. /*
  20. public static Point ToPoint(this Vector2 vector)
  21. {
  22. return new Point((int)vector.X, (int)vector.Y);
  23. }
  24. */
  25. public static float NextFloat(this Random rand, float minValue, float maxValue)
  26. {
  27. return (float)rand.NextDouble() * (maxValue - minValue) + minValue;
  28. }
  29. public static Vector2 NextVector2(this Random rand, float minLength, float maxLength)
  30. {
  31. double theta = rand.NextDouble() * 2 * Math.PI;
  32. float length = rand.NextFloat(minLength, maxLength);
  33. return new Vector2(length * (float)Math.Cos(theta), length * (float)Math.Sin(theta));
  34. }
  35. }
  36. }