Rectangle.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // -----------------------------------------------------------------------
  2. // <copyright file="Rectangle.cs" company="">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Geometry
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. /// <summary>
  12. /// A simple rectangle class.
  13. /// </summary>
  14. internal class Rectangle
  15. {
  16. double xmin, ymin, xmax, ymax;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="Rectangle" /> class.
  19. /// </summary>
  20. public Rectangle()
  21. {
  22. this.xmin = this.ymin = double.MaxValue;
  23. this.xmax = this.ymax = -double.MaxValue;
  24. }
  25. public Rectangle(Rectangle other)
  26. : this(other.Left, other.Bottom, other.Right, other.Top)
  27. {
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="Rectangle" /> class
  31. /// with predefined bounds.
  32. /// </summary>
  33. /// <param name="x">Minimum x value (left).</param>
  34. /// <param name="y">Minimum y value (bottom).</param>
  35. /// <param name="width">Width of the rectangle.</param>
  36. /// <param name="height">Height of the rectangle.</param>
  37. public Rectangle(double x, double y, double width, double height)
  38. {
  39. this.xmin = x;
  40. this.ymin = y;
  41. this.xmax = x + width;
  42. this.ymax = y + height;
  43. }
  44. /// <summary>
  45. /// Gets the minimum x value (left boundary).
  46. /// </summary>
  47. public double Left
  48. {
  49. get { return xmin; }
  50. }
  51. /// <summary>
  52. /// Gets the maximum x value (right boundary).
  53. /// </summary>
  54. public double Right
  55. {
  56. get { return xmax; }
  57. }
  58. /// <summary>
  59. /// Gets the minimum y value (bottom boundary).
  60. /// </summary>
  61. public double Bottom
  62. {
  63. get { return ymin; }
  64. }
  65. /// <summary>
  66. /// Gets the maximum y value (top boundary).
  67. /// </summary>
  68. public double Top
  69. {
  70. get { return ymax; }
  71. }
  72. /// <summary>
  73. /// Gets the width of the rectangle.
  74. /// </summary>
  75. public double Width
  76. {
  77. get { return xmax - xmin; }
  78. }
  79. /// <summary>
  80. /// Gets the height of the rectangle.
  81. /// </summary>
  82. public double Height
  83. {
  84. get { return ymax - ymin; }
  85. }
  86. /// <summary>
  87. /// Update bounds.
  88. /// </summary>
  89. /// <param name="dx">Add dx to left and right bounds.</param>
  90. /// <param name="dy">Add dy to top and bottom bounds.</param>
  91. public void Resize(double dx, double dy)
  92. {
  93. xmin -= dx;
  94. xmax += dx;
  95. ymin -= dy;
  96. ymax += dy;
  97. }
  98. /// <summary>
  99. /// Expand rectangle to include given point.
  100. /// </summary>
  101. /// <param name="p">Point.</param>
  102. public void Expand(Point p)
  103. {
  104. xmin = Math.Min(xmin, p.x);
  105. ymin = Math.Min(ymin, p.y);
  106. xmax = Math.Max(xmax, p.x);
  107. ymax = Math.Max(ymax, p.y);
  108. }
  109. /// <summary>
  110. /// Expand rectangle to include a list of points.
  111. /// </summary>
  112. public void Expand(IEnumerable<Point> points)
  113. {
  114. foreach (var p in points)
  115. {
  116. Expand(p);
  117. }
  118. }
  119. /// <summary>
  120. /// Expand rectangle to include given rectangle.
  121. /// </summary>
  122. /// <param name="x">X coordinate.</param>
  123. /// <param name="y">Y coordinate.</param>
  124. public void Expand(Rectangle other)
  125. {
  126. xmin = Math.Min(xmin, other.xmin);
  127. ymin = Math.Min(ymin, other.ymin);
  128. xmax = Math.Max(xmax, other.xmax);
  129. ymax = Math.Max(ymax, other.ymax);
  130. }
  131. /// <summary>
  132. /// Check if given point is inside rectangle.
  133. /// </summary>
  134. /// <param name="x">Point to check.</param>
  135. /// <param name="y">Point to check.</param>
  136. /// <returns>Return true, if rectangle contains given point.</returns>
  137. public bool Contains(double x, double y)
  138. {
  139. return ((x >= xmin) && (x <= xmax) && (y >= ymin) && (y <= ymax));
  140. }
  141. /// <summary>
  142. /// Check if given point is inside rectangle.
  143. /// </summary>
  144. /// <param name="pt">Point to check.</param>
  145. /// <returns>Return true, if rectangle contains given point.</returns>
  146. public bool Contains(Point pt)
  147. {
  148. return Contains(pt.x, pt.y);
  149. }
  150. /// <summary>
  151. /// Check if this rectangle contains other rectangle.
  152. /// </summary>
  153. /// <param name="other">Rectangle to check.</param>
  154. /// <returns>Return true, if this rectangle contains given rectangle.</returns>
  155. public bool Contains(Rectangle other)
  156. {
  157. return (xmin <= other.Left && other.Right <= xmax
  158. && ymin <= other.Bottom && other.Top <= ymax);
  159. }
  160. /// <summary>
  161. /// Check if this rectangle intersects other rectangle.
  162. /// </summary>
  163. /// <param name="other">Rectangle to check.</param>
  164. /// <returns>Return true, if given rectangle intersects this rectangle.</returns>
  165. public bool Intersects(Rectangle other)
  166. {
  167. return (other.Left < xmax && xmin < other.Right
  168. && other.Bottom < ymax && ymin < other.Top);
  169. }
  170. }
  171. }