RectangleF.cs 29 KB

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