HelperMath.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // HelperMath.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 RobotGameData.Helper
  16. {
  17. /// <summary>
  18. /// Useful functions about math
  19. /// </summary>
  20. public static class HelperMath
  21. {
  22. #region Fields
  23. public static Random Random = new Random();
  24. public const float Epsilon = 1E-5f; //for numerical imprecision
  25. #endregion
  26. /// <summary>
  27. /// Return a random integer.
  28. /// </summary>
  29. /// <returns>Random integer</returns>
  30. public static int Randomi()
  31. {
  32. return Random.Next();
  33. }
  34. /// <summary>
  35. /// It specifies the maximum value and returns a random integer.
  36. /// </summary>
  37. /// <param name="max">Max integer</param>
  38. /// <returns>Random integer</returns>
  39. public static int Randomi(int max)
  40. {
  41. return Random.Next(max);
  42. }
  43. /// <summary>
  44. /// It specifies the maximum and the minimum and returns a random integer.
  45. /// </summary>
  46. /// <param name="min">Min integer</param>
  47. /// <param name="max">Max integer</param>
  48. /// <returns>Random integer</returns>
  49. public static int Randomi(int min, int max)
  50. {
  51. return Random.Next(min, max);
  52. }
  53. /// <summary>
  54. /// Return a random floating point between 0.0 to 1.0
  55. /// </summary>
  56. /// <returns>Random floating point</returns>
  57. public static float RandomNormal()
  58. {
  59. // randomed 0.0 to 1.0
  60. return (float)Random.NextDouble();
  61. }
  62. /// <summary>
  63. /// Return a random floating point between -1.0 to 1.0
  64. /// </summary>
  65. /// <returns>Random floating point</returns>
  66. public static float RandomNormal2()
  67. {
  68. // randomed -1.0 to 1.0
  69. return (float)Random.Next(-1, 2) * (float)Random.NextDouble();
  70. }
  71. /// <summary>
  72. /// Return a randomed vector3 between 0.0 to 1.0
  73. /// </summary>
  74. public static Vector3 RandomVector()
  75. {
  76. return new Vector3(RandomNormal(), RandomNormal(), RandomNormal());
  77. }
  78. /// <summary>
  79. /// Return a randomed vector3 between -1.0 to 1.0
  80. /// </summary>
  81. public static Vector3 RandomVector2()
  82. {
  83. return new Vector3(RandomNormal2(), RandomNormal2(), RandomNormal2());
  84. }
  85. /// <summary>
  86. /// It calculates the remainder of the division of value1 by value2.
  87. /// </summary>
  88. /// <param name="val1">value 1</param>
  89. /// <param name="val2">value 2</param>
  90. /// <returns></returns>
  91. public static float CalculateModulo(float value1, float value2)
  92. {
  93. while (value1 - value2 >= 0)
  94. {
  95. value1 -= value2;
  96. }
  97. return value1;
  98. }
  99. /// <summary>
  100. /// Vector value change to integer, and reduce 0.5 coordinate
  101. /// </summary>
  102. public static Vector3 Make2DCoord(Vector3 vec)
  103. {
  104. int x = (int)vec.X;
  105. int y = (int)vec.Y;
  106. int z = (int)vec.Z;
  107. return new Vector3((float)x - 0.5f,
  108. (float)y - 0.5f,
  109. (float)z - 0.5f);
  110. }
  111. /// <summary>
  112. /// Vector value change to integer, and reduce 0.5 coordinate
  113. /// </summary>
  114. public static Vector2 Make2DCoord(Vector2 vec)
  115. {
  116. int x = (int)vec.X;
  117. int y = (int)vec.Y;
  118. return new Vector2((float)x - 0.5f,
  119. (float)y - 0.5f);
  120. }
  121. }
  122. }