VRect.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. public struct VRect
  3. {
  4. private int m_XMin;
  5. private int m_YMin;
  6. private int m_Width;
  7. private int m_Height;
  8. public int x
  9. {
  10. get
  11. {
  12. return this.m_XMin;
  13. }
  14. set
  15. {
  16. this.m_XMin = value;
  17. }
  18. }
  19. public int y
  20. {
  21. get
  22. {
  23. return this.m_YMin;
  24. }
  25. set
  26. {
  27. this.m_YMin = value;
  28. }
  29. }
  30. public VInt2 position
  31. {
  32. get
  33. {
  34. return new VInt2(this.m_XMin, this.m_YMin);
  35. }
  36. set
  37. {
  38. this.m_XMin = value.x;
  39. this.m_YMin = value.y;
  40. }
  41. }
  42. public VInt2 center
  43. {
  44. get
  45. {
  46. return new VInt2(this.x + (this.m_Width >> 1), this.y + (this.m_Height >> 1));
  47. }
  48. set
  49. {
  50. this.m_XMin = value.x - (this.m_Width >> 1);
  51. this.m_YMin = value.y - (this.m_Height >> 1);
  52. }
  53. }
  54. public VInt2 min
  55. {
  56. get
  57. {
  58. return new VInt2(this.xMin, this.yMin);
  59. }
  60. set
  61. {
  62. this.xMin = value.x;
  63. this.yMin = value.y;
  64. }
  65. }
  66. public VInt2 max
  67. {
  68. get
  69. {
  70. return new VInt2(this.xMax, this.yMax);
  71. }
  72. set
  73. {
  74. this.xMax = value.x;
  75. this.yMax = value.y;
  76. }
  77. }
  78. public int width
  79. {
  80. get
  81. {
  82. return this.m_Width;
  83. }
  84. set
  85. {
  86. this.m_Width = value;
  87. }
  88. }
  89. public int height
  90. {
  91. get
  92. {
  93. return this.m_Height;
  94. }
  95. set
  96. {
  97. this.m_Height = value;
  98. }
  99. }
  100. public VInt2 size
  101. {
  102. get
  103. {
  104. return new VInt2(this.m_Width, this.m_Height);
  105. }
  106. set
  107. {
  108. this.m_Width = value.x;
  109. this.m_Height = value.y;
  110. }
  111. }
  112. public int xMin
  113. {
  114. get
  115. {
  116. return this.m_XMin;
  117. }
  118. set
  119. {
  120. int xMax = this.xMax;
  121. this.m_XMin = value;
  122. this.m_Width = xMax - this.m_XMin;
  123. }
  124. }
  125. public int yMin
  126. {
  127. get
  128. {
  129. return this.m_YMin;
  130. }
  131. set
  132. {
  133. int yMax = this.yMax;
  134. this.m_YMin = value;
  135. this.m_Height = yMax - this.m_YMin;
  136. }
  137. }
  138. public int xMax
  139. {
  140. get
  141. {
  142. return this.m_Width + this.m_XMin;
  143. }
  144. set
  145. {
  146. this.m_Width = value - this.m_XMin;
  147. }
  148. }
  149. public int yMax
  150. {
  151. get
  152. {
  153. return this.m_Height + this.m_YMin;
  154. }
  155. set
  156. {
  157. this.m_Height = value - this.m_YMin;
  158. }
  159. }
  160. public VRect(int left, int top, int width, int height)
  161. {
  162. this.m_XMin = left;
  163. this.m_YMin = top;
  164. this.m_Width = width;
  165. this.m_Height = height;
  166. }
  167. public VRect(VRect source)
  168. {
  169. this.m_XMin = source.m_XMin;
  170. this.m_YMin = source.m_YMin;
  171. this.m_Width = source.m_Width;
  172. this.m_Height = source.m_Height;
  173. }
  174. public static VRect MinMaxRect(int left, int top, int right, int bottom)
  175. {
  176. return new VRect(left, top, right - left, bottom - top);
  177. }
  178. public void Set(int left, int top, int width, int height)
  179. {
  180. this.m_XMin = left;
  181. this.m_YMin = top;
  182. this.m_Width = width;
  183. this.m_Height = height;
  184. }
  185. public override string ToString()
  186. {
  187. object[] array = new object[]
  188. {
  189. this.x,
  190. this.y,
  191. this.width,
  192. this.height
  193. };
  194. return string.Format("(x:{0:F2}, y:{1:F2}, width:{2:F2}, height:{3:F2})", array);
  195. }
  196. public string ToString(string format)
  197. {
  198. object[] array = new object[]
  199. {
  200. this.x.ToString(format),
  201. this.y.ToString(format),
  202. this.width.ToString(format),
  203. this.height.ToString(format)
  204. };
  205. return string.Format("(x:{0}, y:{1}, width:{2}, height:{3})", array);
  206. }
  207. public bool Contains(VInt2 point)
  208. {
  209. return point.x >= this.xMin && point.x < this.xMax && point.y >= this.yMin && point.y < this.yMax;
  210. }
  211. public bool Contains(VInt3 point)
  212. {
  213. return point.x >= this.xMin && point.x < this.xMax && point.y >= this.yMin && point.y < this.yMax;
  214. }
  215. public bool Contains(VInt3 point, bool allowInverse)
  216. {
  217. if (!allowInverse)
  218. {
  219. return this.Contains(point);
  220. }
  221. bool flag = false;
  222. if (((float)this.width < 0f && point.x <= this.xMin && point.x > this.xMax) || ((float)this.width >= 0f && point.x >= this.xMin && point.x < this.xMax))
  223. {
  224. flag = true;
  225. }
  226. return flag && (((float)this.height < 0f && point.y <= this.yMin && point.y > this.yMax) || ((float)this.height >= 0f && point.y >= this.yMin && point.y < this.yMax));
  227. }
  228. private static VRect OrderMinMax(VRect rect)
  229. {
  230. if (rect.xMin > rect.xMax)
  231. {
  232. int xMin = rect.xMin;
  233. rect.xMin = rect.xMax;
  234. rect.xMax = xMin;
  235. }
  236. if (rect.yMin > rect.yMax)
  237. {
  238. int yMin = rect.yMin;
  239. rect.yMin = rect.yMax;
  240. rect.yMax = yMin;
  241. }
  242. return rect;
  243. }
  244. public bool Overlaps(VRect other)
  245. {
  246. return other.xMax > this.xMin && other.xMin < this.xMax && other.yMax > this.yMin && other.yMin < this.yMax;
  247. }
  248. public bool Overlaps(VRect other, bool allowInverse)
  249. {
  250. VRect rect = this;
  251. if (allowInverse)
  252. {
  253. rect = VRect.OrderMinMax(rect);
  254. other = VRect.OrderMinMax(other);
  255. }
  256. return rect.Overlaps(other);
  257. }
  258. public override int GetHashCode()
  259. {
  260. return this.x.GetHashCode() ^ this.width.GetHashCode() << 2 ^ this.y.GetHashCode() >> 2 ^ this.height.GetHashCode() >> 1;
  261. }
  262. public override bool Equals(object other)
  263. {
  264. if (!(other is VRect))
  265. {
  266. return false;
  267. }
  268. VRect vRect = (VRect)other;
  269. return this.x.Equals(vRect.x) && this.y.Equals(vRect.y) && this.width.Equals(vRect.width) && this.height.Equals(vRect.height);
  270. }
  271. public static bool operator !=(VRect lhs, VRect rhs)
  272. {
  273. return lhs.x != rhs.x || lhs.y != rhs.y || lhs.width != rhs.width || lhs.height != rhs.height;
  274. }
  275. public static bool operator ==(VRect lhs, VRect rhs)
  276. {
  277. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.width == rhs.width && lhs.height == rhs.height;
  278. }
  279. }