BoundingRectangle.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Microsoft.Xna.Framework;
  5. namespace MonoGame.Extended
  6. {
  7. // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 4.2; Bounding Volumes - Axis-aligned Bounding Boxes (AABBs). pg 77
  8. /// <summary>
  9. /// An axis-aligned, four sided, two dimensional box defined by a centre <see cref="Vector2" /> and a radii
  10. /// <see cref="Vector2" />.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// An <see cref="BoundingRectangle" /> is categorized by having its faces oriented in such a way that its
  15. /// face normals are at all times parallel with the axes of the given coordinate system.
  16. /// </para>
  17. /// <para>
  18. /// The <see cref="BoundingRectangle" /> of a rotated <see cref="BoundingRectangle" /> will be equivalent or larger
  19. /// in size
  20. /// than the original depending on the angle of rotation.
  21. /// </para>
  22. /// </remarks>
  23. /// <seealso cref="IEquatable{T}" />
  24. /// <seealso cref="IEquatableByRef{T}" />
  25. [DebuggerDisplay("{" + nameof(DebugDisplayString) + ",nq}")]
  26. public struct BoundingRectangle : IEquatable<BoundingRectangle>,
  27. IEquatableByRef<BoundingRectangle>
  28. {
  29. /// <summary>
  30. /// The <see cref="BoundingRectangle" /> with <see cref="Center" /> <see cref="Vector2.Zero"/> and
  31. /// <see cref="HalfExtents" /> set to <see cref="Vector2.Zero"/>.
  32. /// </summary>
  33. public static readonly BoundingRectangle Empty = new BoundingRectangle();
  34. /// <summary>
  35. /// The centre position of this <see cref="BoundingRectangle" />.
  36. /// </summary>
  37. public Vector2 Center;
  38. /// <summary>
  39. /// The distance from the <see cref="Center" /> point along both axes to any point on the boundary of this
  40. /// <see cref="BoundingRectangle" />.
  41. /// </summary>
  42. public Vector2 HalfExtents;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="BoundingRectangle" /> structure from the specified centre
  45. /// <see cref="Vector2" /> and the radii <see cref="SizeF" />.
  46. /// </summary>
  47. /// <param name="center">The centre <see cref="Vector2" />.</param>
  48. /// <param name="halfExtents">The radii <see cref="Vector2" />.</param>
  49. public BoundingRectangle(Vector2 center, SizeF halfExtents)
  50. {
  51. Center = center;
  52. HalfExtents = halfExtents;
  53. }
  54. /// <summary>
  55. /// Computes the <see cref="BoundingRectangle" /> from a minimum <see cref="Vector2" /> and maximum
  56. /// <see cref="Vector2" />.
  57. /// </summary>
  58. /// <param name="minimum">The minimum point.</param>
  59. /// <param name="maximum">The maximum point.</param>
  60. /// <param name="result">The resulting bounding rectangle.</param>
  61. public static void CreateFrom(Vector2 minimum, Vector2 maximum, out BoundingRectangle result)
  62. {
  63. result.Center = new Vector2((maximum.X + minimum.X) * 0.5f, (maximum.Y + minimum.Y) * 0.5f);
  64. result.HalfExtents = new Vector2((maximum.X - minimum.X) * 0.5f, (maximum.Y - minimum.Y) * 0.5f);
  65. }
  66. /// <summary>
  67. /// Computes the <see cref="BoundingRectangle" /> from a minimum <see cref="Vector2" /> and maximum
  68. /// <see cref="Vector2" />.
  69. /// </summary>
  70. /// <param name="minimum">The minimum point.</param>
  71. /// <param name="maximum">The maximum point.</param>
  72. /// <returns>The resulting <see cref="BoundingRectangle" />.</returns>
  73. public static BoundingRectangle CreateFrom(Vector2 minimum, Vector2 maximum)
  74. {
  75. BoundingRectangle result;
  76. CreateFrom(minimum, maximum, out result);
  77. return result;
  78. }
  79. /// <summary>
  80. /// Computes the <see cref="BoundingRectangle" /> from a list of <see cref="Vector2" /> structures.
  81. /// </summary>
  82. /// <param name="points">The points.</param>
  83. /// <param name="result">The resulting bounding rectangle.</param>
  84. public static void CreateFrom(IReadOnlyList<Vector2> points, out BoundingRectangle result)
  85. {
  86. Vector2 minimum;
  87. Vector2 maximum;
  88. PrimitivesHelper.CreateRectangleFromPoints(points, out minimum, out maximum);
  89. CreateFrom(minimum, maximum, out result);
  90. }
  91. /// <summary>
  92. /// Computes the <see cref="BoundingRectangle" /> from a list of <see cref="Vector2" /> structures.
  93. /// </summary>
  94. /// <param name="points">The points.</param>
  95. /// <returns>The resulting <see cref="BoundingRectangle" />.</returns>
  96. public static BoundingRectangle CreateFrom(IReadOnlyList<Vector2> points)
  97. {
  98. BoundingRectangle result;
  99. CreateFrom(points, out result);
  100. return result;
  101. }
  102. /// <summary>
  103. /// Computes the <see cref="BoundingRectangle" /> from the specified <see cref="BoundingRectangle" /> transformed by
  104. /// the
  105. /// specified <see cref="Matrix3x2" />.
  106. /// </summary>
  107. /// <param name="boundingRectangle">The bounding rectangle.</param>
  108. /// <param name="transformMatrix">The transform matrix.</param>
  109. /// <param name="result">The resulting bounding rectangle.</param>
  110. /// <returns>
  111. /// The <see cref="BoundingRectangle" /> from the <paramref name="boundingRectangle" /> transformed by the
  112. /// <paramref name="transformMatrix" />.
  113. /// </returns>
  114. /// <remarks>
  115. /// <para>
  116. /// If a transformed <see cref="BoundingRectangle" /> is used for <paramref name="boundingRectangle" /> then the
  117. /// resulting <see cref="BoundingRectangle" /> will have the compounded transformation, which most likely is
  118. /// not desired.
  119. /// </para>
  120. /// </remarks>
  121. public static void Transform(ref BoundingRectangle boundingRectangle,
  122. ref Matrix3x2 transformMatrix, out BoundingRectangle result)
  123. {
  124. PrimitivesHelper.TransformRectangle(ref boundingRectangle.Center, ref boundingRectangle.HalfExtents, ref transformMatrix);
  125. result.Center = boundingRectangle.Center;
  126. result.HalfExtents = boundingRectangle.HalfExtents;
  127. }
  128. /// <summary>
  129. /// Computes the <see cref="BoundingRectangle" /> from the specified <see cref="BoundingRectangle" /> transformed by
  130. /// the
  131. /// specified <see cref="Matrix3x2" />.
  132. /// </summary>
  133. /// <param name="boundingRectangle">The bounding rectangle.</param>
  134. /// <param name="transformMatrix">The transform matrix.</param>
  135. /// <returns>
  136. /// The <see cref="BoundingRectangle" /> from the <paramref name="boundingRectangle" /> transformed by the
  137. /// <paramref name="transformMatrix" />.
  138. /// </returns>
  139. /// <remarks>
  140. /// <para>
  141. /// If a transformed <see cref="BoundingRectangle" /> is used for <paramref name="boundingRectangle" /> then the
  142. /// resulting <see cref="BoundingRectangle" /> will have the compounded transformation, which most likely is
  143. /// not desired.
  144. /// </para>
  145. /// </remarks>
  146. public static BoundingRectangle Transform(BoundingRectangle boundingRectangle,
  147. ref Matrix3x2 transformMatrix)
  148. {
  149. BoundingRectangle result;
  150. Transform(ref boundingRectangle, ref transformMatrix, out result);
  151. return result;
  152. }
  153. /// <summary>
  154. /// Computes the <see cref="BoundingRectangle" /> that contains the two specified
  155. /// <see cref="BoundingRectangle" /> structures.
  156. /// </summary>
  157. /// <param name="first">The first bounding rectangle.</param>
  158. /// <param name="second">The second bounding rectangle.</param>
  159. /// <param name="result">The resulting bounding rectangle that contains both the <paramref name="first" /> and the
  160. /// <paramref name="second" />.</param>
  161. public static void Union(ref BoundingRectangle first, ref BoundingRectangle second, out BoundingRectangle result)
  162. {
  163. // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 6.5; Bounding Volume Hierarchies - Merging Bounding Volumes. pg 267
  164. var firstMinimum = first.Center - first.HalfExtents;
  165. var firstMaximum = first.Center + first.HalfExtents;
  166. var secondMinimum = second.Center - second.HalfExtents;
  167. var secondMaximum = second.Center + second.HalfExtents;
  168. var minimum = MathExtended.CalculateMinimumVector2(firstMinimum, secondMinimum);
  169. var maximum = MathExtended.CalculateMaximumVector2(firstMaximum, secondMaximum);
  170. result = CreateFrom(minimum, maximum);
  171. }
  172. /// <summary>
  173. /// Computes the <see cref="BoundingRectangle" /> that contains the two specified
  174. /// <see cref="BoundingRectangle" /> structures.
  175. /// </summary>
  176. /// <param name="first">The first bounding rectangle.</param>
  177. /// <param name="second">The second bounding rectangle.</param>
  178. /// <returns>
  179. /// A <see cref="BoundingRectangle" /> that contains both the <paramref name="first" /> and the
  180. /// <paramref name="second" />.
  181. /// </returns>
  182. public static BoundingRectangle Union(BoundingRectangle first, BoundingRectangle second)
  183. {
  184. BoundingRectangle result;
  185. Union(ref first, ref second, out result);
  186. return result;
  187. }
  188. /// <summary>
  189. /// Computes the <see cref="BoundingRectangle" /> that contains both the specified
  190. /// <see cref="BoundingRectangle" /> and this <see cref="BoundingRectangle" />.
  191. /// </summary>
  192. /// <param name="boundingRectangle">The bounding rectangle.</param>
  193. /// <returns>
  194. /// A <see cref="BoundingRectangle" /> that contains both the <paramref name="boundingRectangle" /> and
  195. /// this
  196. /// <see cref="BoundingRectangle" />.
  197. /// </returns>
  198. public BoundingRectangle Union(BoundingRectangle boundingRectangle)
  199. {
  200. return Union(this, boundingRectangle);
  201. }
  202. /// <summary>
  203. /// Computes the <see cref="BoundingRectangle" /> that is in common between the two specified
  204. /// <see cref="BoundingRectangle" /> structures.
  205. /// </summary>
  206. /// <param name="first">The first bounding rectangle.</param>
  207. /// <param name="second">The second bounding rectangle.</param>
  208. /// <param name="result">The resulting bounding rectangle that is in common between both the <paramref name="first" /> and
  209. /// the <paramref name="second" />, if they intersect; otherwise, <see cref="Empty"/>.</param>
  210. public static void Intersection(ref BoundingRectangle first,
  211. ref BoundingRectangle second, out BoundingRectangle result)
  212. {
  213. var firstMinimum = first.Center - first.HalfExtents;
  214. var firstMaximum = first.Center + first.HalfExtents;
  215. var secondMinimum = second.Center - second.HalfExtents;
  216. var secondMaximum = second.Center + second.HalfExtents;
  217. var minimum = MathExtended.CalculateMaximumVector2(firstMinimum, secondMinimum);
  218. var maximum = MathExtended.CalculateMinimumVector2(firstMaximum, secondMaximum);
  219. if ((maximum.X < minimum.X) || (maximum.Y < minimum.Y))
  220. result = new BoundingRectangle();
  221. else
  222. result = CreateFrom(minimum, maximum);
  223. }
  224. /// <summary>
  225. /// Computes the <see cref="BoundingRectangle" /> that is in common between the two specified
  226. /// <see cref="BoundingRectangle" /> structures.
  227. /// </summary>
  228. /// <param name="first">The first bounding rectangle.</param>
  229. /// <param name="second">The second bounding rectangle.</param>
  230. /// <returns>
  231. /// A <see cref="BoundingRectangle" /> that is in common between both the <paramref name="first" /> and
  232. /// the <paramref name="second" />, if they intersect; otherwise, <see cref="Empty"/>.
  233. /// </returns>
  234. public static BoundingRectangle Intersection(BoundingRectangle first,
  235. BoundingRectangle second)
  236. {
  237. BoundingRectangle result;
  238. Intersection(ref first, ref second, out result);
  239. return result;
  240. }
  241. /// <summary>
  242. /// Computes the <see cref="BoundingRectangle" /> that is in common between the specified
  243. /// <see cref="BoundingRectangle" /> and this <see cref="BoundingRectangle" />.
  244. /// </summary>
  245. /// <param name="boundingRectangle">The bounding rectangle.</param>
  246. /// <returns>
  247. /// A <see cref="BoundingRectangle" /> that is in common between both the <paramref name="boundingRectangle" /> and
  248. /// this <see cref="BoundingRectangle"/>, if they intersect; otherwise, <see cref="Empty"/>.
  249. /// </returns>
  250. public BoundingRectangle Intersection(BoundingRectangle boundingRectangle)
  251. {
  252. BoundingRectangle result;
  253. Intersection(ref this, ref boundingRectangle, out result);
  254. return result;
  255. }
  256. /// <summary>
  257. /// Determines whether the two specified <see cref="BoundingRectangle" /> structures intersect.
  258. /// </summary>
  259. /// <param name="first">The first bounding rectangle.</param>
  260. /// <param name="second">The second bounding rectangle.</param>
  261. /// <returns>
  262. /// <c>true</c> if the <paramref name="first" /> intersects with the <see cref="second" />; otherwise, <c>false</c>.
  263. /// </returns>
  264. public static bool Intersects(ref BoundingRectangle first, ref BoundingRectangle second)
  265. {
  266. // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 4.2; Bounding Volumes - Axis-aligned Bounding Boxes (AABBs). pg 80
  267. var distance = first.Center - second.Center;
  268. var radii = first.HalfExtents + second.HalfExtents;
  269. return Math.Abs(distance.X) <= radii.X && Math.Abs(distance.Y) <= radii.Y;
  270. }
  271. /// <summary>
  272. /// Determines whether the two specified <see cref="BoundingRectangle" /> structures intersect.
  273. /// </summary>
  274. /// <param name="first">The first bounding rectangle.</param>
  275. /// <param name="second">The second bounding rectangle.</param>
  276. /// <returns>
  277. /// <c>true</c> if the <paramref name="first" /> intersects with the <see cref="second" />; otherwise, <c>false</c>.
  278. /// </returns>
  279. public static bool Intersects(BoundingRectangle first, BoundingRectangle second)
  280. {
  281. return Intersects(ref first, ref second);
  282. }
  283. /// <summary>
  284. /// Determines whether the specified <see cref="BoundingRectangle" /> intersects with this
  285. /// <see cref="BoundingRectangle" />.
  286. /// </summary>
  287. /// <param name="boundingRectangle">The bounding rectangle.</param>
  288. /// <returns>
  289. /// <c>true</c> if the <paramref name="boundingRectangle" /> intersects with this
  290. /// <see cref="BoundingRectangle" />; otherwise,
  291. /// <c>false</c>.
  292. /// </returns>
  293. public bool Intersects(ref BoundingRectangle boundingRectangle)
  294. {
  295. return Intersects(ref this, ref boundingRectangle);
  296. }
  297. /// <summary>
  298. /// Determines whether the specified <see cref="BoundingRectangle" /> intersects with this
  299. /// <see cref="BoundingRectangle" />.
  300. /// </summary>
  301. /// <param name="boundingRectangle">The bounding rectangle.</param>
  302. /// <returns>
  303. /// <c>true</c> if the <paramref name="boundingRectangle" /> intersects with this
  304. /// <see cref="BoundingRectangle" />; otherwise,
  305. /// <c>false</c>.
  306. /// </returns>
  307. public bool Intersects(BoundingRectangle boundingRectangle)
  308. {
  309. return Intersects(ref this, ref boundingRectangle);
  310. }
  311. /// <summary>
  312. /// Updates this <see cref="BoundingRectangle" /> from a list of <see cref="Vector2" /> structures.
  313. /// </summary>
  314. /// <param name="points">The points.</param>
  315. public void UpdateFromPoints(IReadOnlyList<Vector2> points)
  316. {
  317. var boundingRectangle = CreateFrom(points);
  318. Center = boundingRectangle.Center;
  319. HalfExtents = boundingRectangle.HalfExtents;
  320. }
  321. /// <summary>
  322. /// Determines whether the specified <see cref="BoundingRectangle" /> contains the specified
  323. /// <see cref="Vector2" />.
  324. /// </summary>
  325. /// <param name="boundingRectangle">The bounding rectangle.</param>
  326. /// <param name="point">The point.</param>
  327. /// <returns>
  328. /// <c>true</c> if the <paramref name="boundingRectangle" /> contains the <paramref name="point" />; otherwise,
  329. /// <c>false</c>.
  330. /// </returns>
  331. public static bool Contains(ref BoundingRectangle boundingRectangle, ref Vector2 point)
  332. {
  333. // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 4.2; Bounding Volumes - Axis-aligned Bounding Boxes (AABBs). pg 78
  334. var distance = boundingRectangle.Center - point;
  335. var radii = boundingRectangle.HalfExtents;
  336. return (Math.Abs(distance.X) <= radii.X) && (Math.Abs(distance.Y) <= radii.Y);
  337. }
  338. /// <summary>
  339. /// Determines whether the specified <see cref="BoundingRectangle" /> contains the specified
  340. /// <see cref="Vector2" />.
  341. /// </summary>
  342. /// <param name="boundingRectangle">The bounding rectangle.</param>
  343. /// <param name="point">The point.</param>
  344. /// <returns>
  345. /// <c>true</c> if the <paramref name="boundingRectangle" /> contains the <paramref name="point" />; otherwise,
  346. /// <c>false</c>.
  347. /// </returns>
  348. public static bool Contains(BoundingRectangle boundingRectangle, Vector2 point)
  349. {
  350. return Contains(ref boundingRectangle, ref point);
  351. }
  352. /// <summary>
  353. /// Determines whether this <see cref="BoundingRectangle" /> contains the specified <see cref="Vector2" />.
  354. /// </summary>
  355. /// <param name="point">The point.</param>
  356. /// <returns>
  357. /// <c>true</c> if this <see cref="BoundingRectangle" /> contains the <paramref name="point" />; otherwise,
  358. /// <c>false</c>.
  359. /// </returns>
  360. public bool Contains(Vector2 point)
  361. {
  362. return Contains(this, point);
  363. }
  364. /// <summary>
  365. /// Computes the squared distance from this <see cref="BoundingRectangle"/> to a <see cref="Vector2"/>.
  366. /// </summary>
  367. /// <param name="point">The point.</param>
  368. /// <returns>The squared distance from this <see cref="BoundingRectangle"/> to the <paramref name="point"/>.</returns>
  369. public float SquaredDistanceTo(Vector2 point)
  370. {
  371. return PrimitivesHelper.SquaredDistanceToPointFromRectangle(Center - HalfExtents, Center + HalfExtents, point);
  372. }
  373. /// <summary>
  374. /// Computes the closest <see cref="Vector2" /> on this <see cref="BoundingRectangle" /> to a specified
  375. /// <see cref="Vector2" />.
  376. /// </summary>
  377. /// <param name="point">The point.</param>
  378. /// <returns>The closest <see cref="Vector2" /> on this <see cref="BoundingRectangle" /> to the <paramref name="point" />.</returns>
  379. public Vector2 ClosestPointTo(Vector2 point)
  380. {
  381. Vector2 result;
  382. PrimitivesHelper.ClosestPointToPointFromRectangle(Center - HalfExtents, Center + HalfExtents, point, out result);
  383. return result;
  384. }
  385. /// <summary>
  386. /// Compares two <see cref="BoundingRectangle" /> structures. The result specifies whether the values of the
  387. /// <see cref="Center" /> and <see cref="HalfExtents" /> fields of the two <see cref="BoundingRectangle" /> structures
  388. /// are equal.
  389. /// </summary>
  390. /// <param name="first">The first bounding rectangle.</param>
  391. /// <param name="second">The second bounding rectangle.</param>
  392. /// <returns>
  393. /// <c>true</c> if the <see cref="Center" /> and <see cref="HalfExtents" /> fields of the two
  394. /// <see cref="BoundingRectangle" /> structures are equal; otherwise, <c>false</c>.
  395. /// </returns>
  396. public static bool operator ==(BoundingRectangle first, BoundingRectangle second)
  397. {
  398. return first.Equals(ref second);
  399. }
  400. /// <summary>
  401. /// Compares two <see cref="BoundingRectangle" /> structures. The result specifies whether the values of the
  402. /// <see cref="Center" /> and <see cref="HalfExtents" /> fields of the two <see cref="BoundingRectangle" /> structures
  403. /// are unequal.
  404. /// </summary>
  405. /// <param name="first">The first bounding rectangle.</param>
  406. /// <param name="second">The second bounding rectangle.</param>
  407. /// <returns>
  408. /// <c>true</c> if the <see cref="Center" /> and <see cref="HalfExtents" /> fields of the two
  409. /// <see cref="BoundingRectangle" /> structures are unequal; otherwise, <c>false</c>.
  410. /// </returns>
  411. public static bool operator !=(BoundingRectangle first, BoundingRectangle second)
  412. {
  413. return !(first == second);
  414. }
  415. /// <summary>
  416. /// Indicates whether this <see cref="BoundingRectangle" /> is equal to another
  417. /// <see cref="BoundingRectangle" />.
  418. /// </summary>
  419. /// <param name="boundingRectangle">The bounding rectangle.</param>
  420. /// <returns>
  421. /// <c>true</c> if this <see cref="BoundingRectangle" /> is equal to the <paramref name="boundingRectangle" />;
  422. /// otherwise, <c>false</c>.
  423. /// </returns>
  424. public bool Equals(BoundingRectangle boundingRectangle)
  425. {
  426. return Equals(ref boundingRectangle);
  427. }
  428. /// <summary>
  429. /// Indicates whether this <see cref="BoundingRectangle" /> is equal to another <see cref="BoundingRectangle" />.
  430. /// </summary>
  431. /// <param name="boundingRectangle">The bounding rectangle.</param>
  432. /// <returns>
  433. /// <c>true</c> if this <see cref="BoundingRectangle" /> is equal to the <paramref name="boundingRectangle" />;
  434. /// otherwise,
  435. /// <c>false</c>.
  436. /// </returns>
  437. public bool Equals(ref BoundingRectangle boundingRectangle)
  438. {
  439. return (boundingRectangle.Center == Center) && (boundingRectangle.HalfExtents == HalfExtents);
  440. }
  441. /// <summary>
  442. /// Returns a value indicating whether this <see cref="BoundingRectangle" /> is equal to a specified object.
  443. /// </summary>
  444. /// <param name="obj">The object to make the comparison with.</param>
  445. /// <returns>
  446. /// <c>true</c> if this <see cref="BoundingRectangle" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
  447. /// </returns>
  448. public override bool Equals(object obj)
  449. {
  450. if (obj is BoundingRectangle)
  451. return Equals((BoundingRectangle)obj);
  452. return false;
  453. }
  454. /// <summary>
  455. /// Returns a hash code of this <see cref="BoundingRectangle" /> suitable for use in hashing algorithms and data
  456. /// structures like a hash table.
  457. /// </summary>
  458. /// <returns>
  459. /// A hash code of this <see cref="BoundingRectangle" />.
  460. /// </returns>
  461. public override int GetHashCode()
  462. {
  463. unchecked
  464. {
  465. return (Center.GetHashCode() * 397) ^ HalfExtents.GetHashCode();
  466. }
  467. }
  468. /// <summary>
  469. /// Performs an implicit conversion from a <see cref="Rectangle" /> to a <see cref="BoundingRectangle" />.
  470. /// </summary>
  471. /// <param name="rectangle">The rectangle.</param>
  472. /// <returns>
  473. /// The resulting <see cref="BoundingRectangle" />.
  474. /// </returns>
  475. public static implicit operator BoundingRectangle(Rectangle rectangle)
  476. {
  477. var radii = new SizeF(rectangle.Width * 0.5f, rectangle.Height * 0.5f);
  478. var centre = new Vector2(rectangle.X + radii.Width, rectangle.Y + radii.Height);
  479. return new BoundingRectangle(centre, radii);
  480. }
  481. /// <summary>
  482. /// Performs an implicit conversion from a <see cref="BoundingRectangle" /> to a <see cref="Rectangle" />.
  483. /// </summary>
  484. /// <param name="boundingRectangle">The bounding rectangle.</param>
  485. /// <returns>
  486. /// The resulting <see cref="Rectangle" />.
  487. /// </returns>
  488. public static implicit operator Rectangle(BoundingRectangle boundingRectangle)
  489. {
  490. var minimum = boundingRectangle.Center - boundingRectangle.HalfExtents;
  491. return new Rectangle((int)minimum.X, (int)minimum.Y, (int)boundingRectangle.HalfExtents.X * 2,
  492. (int)boundingRectangle.HalfExtents.Y * 2);
  493. }
  494. /// <summary>
  495. /// Performs an implicit conversion from a <see cref="RectangleF" /> to a <see cref="BoundingRectangle" />.
  496. /// </summary>
  497. /// <param name="rectangle">The rectangle.</param>
  498. /// <returns>
  499. /// The resulting <see cref="BoundingRectangle" />.
  500. /// </returns>
  501. public static implicit operator BoundingRectangle(RectangleF rectangle)
  502. {
  503. var radii = new SizeF(rectangle.Width * 0.5f, rectangle.Height * 0.5f);
  504. var centre = new Vector2(rectangle.X + radii.Width, rectangle.Y + radii.Height);
  505. return new BoundingRectangle(centre, radii);
  506. }
  507. /// <summary>
  508. /// Performs an implicit conversion from a <see cref="BoundingRectangle" /> to a <see cref="RectangleF" />.
  509. /// </summary>
  510. /// <param name="boundingRectangle">The bounding rectangle.</param>
  511. /// <returns>
  512. /// The resulting <see cref="Rectangle" />.
  513. /// </returns>
  514. public static implicit operator RectangleF(BoundingRectangle boundingRectangle)
  515. {
  516. var minimum = boundingRectangle.Center - boundingRectangle.HalfExtents;
  517. return new RectangleF(minimum.X, minimum.Y, boundingRectangle.HalfExtents.X * 2,
  518. boundingRectangle.HalfExtents.Y * 2);
  519. }
  520. /// <summary>
  521. /// Returns a <see cref="string" /> that represents this <see cref="BoundingRectangle" />.
  522. /// </summary>
  523. /// <returns>
  524. /// A <see cref="string" /> that represents this <see cref="BoundingRectangle" />.
  525. /// </returns>
  526. public override string ToString()
  527. {
  528. return $"Centre: {Center}, Radii: {HalfExtents}";
  529. }
  530. internal string DebugDisplayString => ToString();
  531. }
  532. }