PointEx.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace OpenVIII
  6. {
  7. /// <summary>
  8. /// More Extensions
  9. /// </summary>
  10. public static class PointEx
  11. {
  12. #region Methods
  13. /// <see cref="https://stackoverflow.com/questions/4615410/random-element-of-listt-from-linq-sql"/>
  14. public static T Random<T>(this IEnumerable<T> input) => input.ElementAt(Memory.Random.Next(input.Count()));
  15. public static T RandomOrDefault<T>(this IEnumerable<T> input) => input.ElementAtOrDefault(Memory.Random.Next(input.Count()));
  16. /// <summary>
  17. /// Rotate a Vector2 by degrees
  18. /// </summary>
  19. /// <param name="v"></param>
  20. /// <param name="degrees"></param>
  21. /// <returns></returns>
  22. /// <see cref="https://answers.unity.com/questions/661383/whats-the-most-efficient-way-to-rotate-a-vector2-o.html"/>
  23. public static Vector2 Rotate(this Vector2 v, float degrees)
  24. {
  25. float rad = MathHelper.ToRadians(degrees);
  26. float sin = (float)Math.Sin(rad);
  27. float cos = (float)Math.Cos(rad);
  28. float tx = v.X;
  29. float ty = v.Y;
  30. v.X = (cos * tx) - (sin * ty);
  31. v.Y = (sin * tx) + (cos * ty);
  32. return v;
  33. }
  34. public static Point Offset(this ref Point source, Point offset)
  35. {
  36. source = (source.ToVector2() + offset.ToVector2()).ToPoint();
  37. return source;
  38. }
  39. public static Point Offset(this ref Point source, int x,int y)
  40. {
  41. var offset = new Point(x, y);
  42. return source.Offset(offset);
  43. }
  44. public static Point Offset(this ref Point source, Vector2 offset)
  45. {
  46. source = (source.ToVector2() + offset).ToPoint();
  47. return source;
  48. }
  49. public static Point Transform(this Point point, Matrix matrix)
  50. {
  51. point = Vector2.Transform(point.ToVector2(), Matrix.Invert(matrix)).RoundedPoint();
  52. return point;
  53. }
  54. public static Rectangle Scale(this Rectangle source, Matrix matrix)
  55. {
  56. Vector2 scale = Memory.Scale();
  57. Vector2 loc = source.Location.ToVector2();
  58. source.Offset(matrix.Translation.X, matrix.Translation.Y);
  59. source.Location = Vector2.Transform(loc, Matrix.Invert(matrix)).RoundedPoint();
  60. source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
  61. return source;
  62. }
  63. public static Rectangle Scale(this Rectangle source, Vector2 scale)
  64. {
  65. source.Location = (source.Location.ToVector2() * scale).RoundedPoint();
  66. source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
  67. return source;
  68. }
  69. public static Rectangle Scale(this Rectangle source)
  70. {
  71. Vector2 scale = Memory.Scale();
  72. source.Location = (source.Location.ToVector2() * scale).RoundedPoint();
  73. source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
  74. return source;
  75. }
  76. public static Point Scale(this Point source, Vector2 scale)
  77. {
  78. source = (source.ToVector2() * scale).RoundedPoint();
  79. return source;
  80. }
  81. public static Point Scale(this Point source)
  82. {
  83. Vector2 scale = Memory.Scale();
  84. source = (source.ToVector2() * scale).RoundedPoint();
  85. return source;
  86. }
  87. public static Point RoundedPoint(this Vector2 v) => new Point((int)Math.Round(v.X), (int)Math.Round(v.Y));
  88. public static Point CeilingPoint(this Vector2 v) => v.Ceiling().ToPoint();
  89. public static Point FloorPoint(this Vector2 v) => v.Floor().ToPoint();
  90. public static Vector2 Round(this Vector2 v) => new Vector2((float)Math.Round(v.X), (float)Math.Round(v.Y));
  91. public static Vector2 Ceiling(this Vector2 v) => new Vector2((float)Math.Ceiling(v.X), (float)Math.Ceiling(v.Y));
  92. public static Vector2 Abs(this Vector2 v) => new Vector2((float)Math.Abs(v.X), (float)Math.Abs(v.Y));
  93. public static Vector2 Floor(this Vector2 v) => new Vector2((float)Math.Floor(v.X), (float)Math.Floor(v.Y));
  94. public static Vector2 FloorOrCeiling(this Vector2 v, Vector2 target)
  95. {
  96. float X, Y;
  97. X = v.X < target.X ? (float)Math.Ceiling(v.X) : (float)Math.Floor(v.X);
  98. Y = v.Y < target.Y ? (float)Math.Ceiling(v.Y) : (float)Math.Floor(v.Y);
  99. return new Vector2(X, Y);
  100. }
  101. /// <summary>
  102. /// Count how many flags set in enum.
  103. /// </summary>
  104. /// <param name="statuses">varible you need to number of flags set.</param>
  105. /// <returns>count</returns>
  106. /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
  107. public static uint Count(this Kernel_bin.J_Statuses statuses)
  108. {
  109. uint v = (uint)statuses;
  110. v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
  111. v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
  112. return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
  113. }
  114. /// <summary>
  115. /// Count how many flags set in enum.
  116. /// </summary>
  117. /// <param name="element">varible you need to number of flags set.</param>
  118. /// <returns>count</returns>
  119. /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
  120. public static uint Count(this Kernel_bin.Element element)
  121. {
  122. uint v = (uint)element;
  123. v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
  124. v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
  125. return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
  126. }
  127. /// <summary>
  128. /// Count how many flags set in enum.
  129. /// </summary>
  130. /// <param name="element">varible you need to number of flags set.</param>
  131. /// <returns>count</returns>
  132. /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
  133. public static uint Count(this Kernel_bin.Battle_Only_Statuses element)
  134. {
  135. uint v = (uint)element;
  136. v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
  137. v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
  138. return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
  139. }
  140. /// <summary>
  141. /// Count how many flags set in enum.
  142. /// </summary>
  143. /// <param name="element">varible you need to number of flags set.</param>
  144. /// <returns>count</returns>
  145. /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
  146. public static uint Count(this Kernel_bin.Persistent_Statuses element)
  147. {
  148. uint v = (uint)element;
  149. v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
  150. v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
  151. return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
  152. }
  153. public static Characters ToCharacters(this Faces.ID id)
  154. {
  155. if ((byte)id > 10)
  156. return Characters.Blank;
  157. return (Characters)id;
  158. }
  159. public static GFs ToGFs(this Faces.ID id)
  160. {
  161. if ((byte)id < 16 || (byte)id > 31)
  162. return GFs.Blank;
  163. return (GFs)(id - 16);
  164. }
  165. public static Faces.ID ToFacesID(this Characters id) => (Faces.ID)id;
  166. public static Faces.ID ToFacesID(this GFs id)
  167. {
  168. if (GFs.All == id || GFs.Blank == id)
  169. return Faces.ID.Blank;
  170. return (Faces.ID)(id + 16);
  171. }
  172. #endregion Methods
  173. }
  174. }