SizeF.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended
  4. {
  5. /// <summary>
  6. /// A two dimensional size defined by two real numbers, a width and a height.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// A size is a subspace of two-dimensional space, the area of which is described in terms of a two-dimensional
  11. /// coordinate system, given by a reference point and two coordinate axes.
  12. /// </para>
  13. /// </remarks>
  14. /// <seealso cref="IEquatable{T}" />
  15. /// <seealso cref="IEquatableByRef{SizeF}" />
  16. public struct SizeF : IEquatable<SizeF>, IEquatableByRef<SizeF>
  17. {
  18. /// <summary>
  19. /// Returns a <see cref="SizeF" /> with <see cref="Width" /> and <see cref="Height" /> equal to <c>0.0f</c>.
  20. /// </summary>
  21. public static readonly SizeF Empty = new SizeF();
  22. /// <summary>
  23. /// The horizontal component of this <see cref="SizeF" />.
  24. /// </summary>
  25. public float Width;
  26. /// <summary>
  27. /// The vertical component of this <see cref="SizeF" />.
  28. /// </summary>
  29. public float Height;
  30. /// <summary>
  31. /// Gets a value that indicates whether this <see cref="SizeF" /> is empty.
  32. /// </summary>
  33. // ReSharper disable CompareOfFloatsByEqualityOperator
  34. public bool IsEmpty => (Width == 0) && (Height == 0);
  35. // ReSharper restore CompareOfFloatsByEqualityOperator
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="SizeF" /> structure from the specified dimensions.
  38. /// </summary>
  39. /// <param name="width">The width.</param>
  40. /// <param name="height">The height.</param>
  41. public SizeF(float width, float height)
  42. {
  43. Width = width;
  44. Height = height;
  45. }
  46. /// <summary>
  47. /// Compares two <see cref="SizeF" /> structures. The result specifies
  48. /// whether the values of the <see cref="Width" /> and <see cref="Height" />
  49. /// fields of the two <see cref="Vector2" /> structures are equal.
  50. /// </summary>
  51. /// <param name="first">The first size.</param>
  52. /// <param name="second">The second size.</param>
  53. /// <returns>
  54. /// <c>true</c> if the <see cref="Width" /> and <see cref="Height" />
  55. /// fields of the two <see cref="Vector2" /> structures are equal; otherwise, <c>false</c>.
  56. /// </returns>
  57. public static bool operator ==(SizeF first, SizeF second)
  58. {
  59. return first.Equals(ref second);
  60. }
  61. /// <summary>
  62. /// Indicates whether this <see cref="SizeF" /> is equal to another <see cref="SizeF" />.
  63. /// </summary>
  64. /// <param name="size">The size.</param>
  65. /// <returns>
  66. /// <c>true</c> if this <see cref="Vector2" /> is equal to the <paramref name="size" /> parameter; otherwise,
  67. /// <c>false</c>.
  68. /// </returns>
  69. public bool Equals(SizeF size)
  70. {
  71. return Equals(ref size);
  72. }
  73. /// <summary>
  74. /// Indicates whether this <see cref="SizeF" /> is equal to another <see cref="SizeF" />.
  75. /// </summary>
  76. /// <param name="size">The size.</param>
  77. /// <returns>
  78. /// <c>true</c> if this <see cref="Vector2" /> is equal to the <paramref name="size" />; otherwise,
  79. /// <c>false</c>.
  80. /// </returns>
  81. public bool Equals(ref SizeF size)
  82. {
  83. // ReSharper disable CompareOfFloatsByEqualityOperator
  84. return (Width == size.Width) && (Height == size.Height);
  85. // ReSharper restore CompareOfFloatsByEqualityOperator
  86. }
  87. /// <summary>
  88. /// Returns a value indicating whether this <see cref="SizeF" /> is equal to a specified object.
  89. /// </summary>
  90. /// <param name="obj">The object to make the comparison with.</param>
  91. /// <returns>
  92. /// <c>true</c> if this <see cref="SizeF" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
  93. /// </returns>
  94. public override bool Equals(object obj)
  95. {
  96. if (obj is SizeF)
  97. return Equals((SizeF) obj);
  98. return false;
  99. }
  100. /// <summary>
  101. /// Compares two <see cref="SizeF" /> structures. The result specifies
  102. /// whether the values of the <see cref="Width" /> or <see cref="Height" />
  103. /// fields of the two <see cref="SizeF" /> structures are unequal.
  104. /// </summary>
  105. /// <param name="first">The first size.</param>
  106. /// <param name="second">The second size.</param>
  107. /// <returns>
  108. /// <c>true</c> if the <see cref="Width" /> or <see cref="Height" />
  109. /// fields of the two <see cref="SizeF" /> structures are unequal; otherwise, <c>false</c>.
  110. /// </returns>
  111. public static bool operator !=(SizeF first, SizeF second)
  112. {
  113. return !(first == second);
  114. }
  115. /// <summary>
  116. /// Calculates the <see cref="SizeF" /> representing the vector addition of two <see cref="SizeF" /> structures as if
  117. /// they
  118. /// were <see cref="Vector2" /> structures.
  119. /// </summary>
  120. /// <param name="first">The first size.</param>
  121. /// <param name="second">The second size.</param>
  122. /// <returns>
  123. /// The <see cref="SizeF" /> representing the vector addition of two <see cref="SizeF" /> structures as if they
  124. /// were <see cref="Vector2" /> structures.
  125. /// </returns>
  126. public static SizeF operator +(SizeF first, SizeF second)
  127. {
  128. return Add(first, second);
  129. }
  130. /// <summary>
  131. /// Calculates the <see cref="SizeF" /> representing the vector addition of two <see cref="SizeF" /> structures.
  132. /// </summary>
  133. /// <param name="first">The first size.</param>
  134. /// <param name="second">The second size.</param>
  135. /// <returns>
  136. /// The <see cref="SizeF" /> representing the vector addition of two <see cref="SizeF" /> structures.
  137. /// </returns>
  138. public static SizeF Add(SizeF first, SizeF second)
  139. {
  140. SizeF size;
  141. size.Width = first.Width + second.Width;
  142. size.Height = first.Height + second.Height;
  143. return size;
  144. }
  145. /// <summary>
  146. /// Calculates the <see cref="SizeF" /> representing the vector subtraction of two <see cref="SizeF" /> structures.
  147. /// </summary>
  148. /// <param name="first">The first size.</param>
  149. /// <param name="second">The second size.</param>
  150. /// <returns>
  151. /// The <see cref="SizeF" /> representing the vector subtraction of two <see cref="SizeF" /> structures.
  152. /// </returns>
  153. public static SizeF operator -(SizeF first, SizeF second)
  154. {
  155. return Subtract(first, second);
  156. }
  157. public static Vector2 operator -(Vector2 first, SizeF second)
  158. {
  159. return Subtract(first, second);
  160. }
  161. public static SizeF operator /(SizeF size, float value)
  162. {
  163. return new SizeF(size.Width / value, size.Height / value);
  164. }
  165. public static SizeF operator *(SizeF size, float value)
  166. {
  167. return new SizeF(size.Width * value, size.Height * value);
  168. }
  169. /// <summary>
  170. /// Calculates the <see cref="SizeF" /> representing the vector subtraction of two <see cref="SizeF" /> structures.
  171. /// </summary>
  172. /// <param name="first">The first size.</param>
  173. /// <param name="second">The second size.</param>
  174. /// <returns>
  175. /// The <see cref="SizeF" /> representing the vector subtraction of two <see cref="SizeF" /> structures.
  176. /// </returns>
  177. public static SizeF Subtract(SizeF first, SizeF second)
  178. {
  179. SizeF size;
  180. size.Width = first.Width - second.Width;
  181. size.Height = first.Height - second.Height;
  182. return size;
  183. }
  184. /// <summary>
  185. /// Returns a hash code of this <see cref="SizeF" /> suitable for use in hashing algorithms and data
  186. /// structures like a hash table.
  187. /// </summary>
  188. /// <returns>
  189. /// A hash code of this <see cref="Vector2" />.
  190. /// </returns>
  191. public override int GetHashCode()
  192. {
  193. unchecked
  194. {
  195. return (Width.GetHashCode()*397) ^ Height.GetHashCode();
  196. }
  197. }
  198. /// <summary>
  199. /// Performs an implicit conversion from a <see cref="Vector2" /> to a <see cref="SizeF" />.
  200. /// </summary>
  201. /// <param name="point">The point.</param>
  202. /// <returns>
  203. /// The resulting <see cref="SizeF" />.
  204. /// </returns>
  205. public static implicit operator SizeF(Vector2 point)
  206. {
  207. return new SizeF(point.X, point.Y);
  208. }
  209. /// <summary>
  210. /// Performs an implicit conversion from a <see cref="Point" /> to a <see cref="SizeF" />.
  211. /// </summary>
  212. /// <param name="point">The point.</param>
  213. /// <returns>
  214. /// The resulting <see cref="SizeF" />.
  215. /// </returns>
  216. public static implicit operator SizeF(Point point)
  217. {
  218. return new SizeF(point.X, point.Y);
  219. }
  220. /// <summary>
  221. /// Performs an implicit conversion from a <see cref="Vector2" /> to a <see cref="SizeF" />.
  222. /// </summary>
  223. /// <param name="size">The size.</param>
  224. /// <returns>
  225. /// The resulting <see cref="Vector2" />.
  226. /// </returns>
  227. public static implicit operator Vector2(SizeF size)
  228. {
  229. return new Vector2(size.Width, size.Height);
  230. }
  231. ///// <summary>
  232. ///// Performs an implicit conversion from a <see cref="Size" /> to a <see cref="Size2" />.
  233. ///// </summary>
  234. ///// <param name="size">The size.</param>
  235. ///// <returns>
  236. ///// The resulting <see cref="Size2" />.
  237. ///// </returns>
  238. //public static implicit operator Size2(Size size)
  239. //{
  240. // return new Size2(size.Width, size.Height);
  241. //}
  242. /// <summary>
  243. /// Performs an explicit conversion from a <see cref="Size2" /> to a <see cref="Point" />.
  244. /// </summary>
  245. /// <param name="size">The size.</param>
  246. /// <returns>
  247. /// The resulting <see cref="Size2" />.
  248. /// </returns>
  249. public static explicit operator Point(SizeF size)
  250. {
  251. return new Point((int)size.Width, (int)size.Height);
  252. }
  253. /// <summary>
  254. /// Returns a <see cref="string" /> that represents this <see cref="SizeF" />.
  255. /// </summary>
  256. /// <returns>
  257. /// A <see cref="string" /> that represents this <see cref="SizeF" />.
  258. /// </returns>
  259. public override string ToString()
  260. {
  261. return $"Width: {Width}, Height: {Height}";
  262. }
  263. internal string DebugDisplayString => ToString();
  264. }
  265. }