Box2.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import {Vector2} from "./Vector2.js";
  2. /**
  3. * Box is described by a minimum and maximum points.
  4. *
  5. * Can be used for collision detection with points and other boxes.
  6. *
  7. * @class
  8. * @param {Vector2} min Minimum point of the box.
  9. * @param {Vector2} max Maximum point of the box.
  10. */
  11. function Box2(min, max)
  12. {
  13. /**
  14. * Minimum point of the box.
  15. *
  16. * @type {Vector2}
  17. */
  18. this.min = (min !== undefined) ? min : new Vector2();
  19. /**
  20. * Maximum point of the box.
  21. *
  22. * @type {Vector2}
  23. */
  24. this.max = (max !== undefined) ? max : new Vector2();
  25. }
  26. /**
  27. * Set the box values.
  28. *
  29. * @param {Vector2} min Minimum point of the box.
  30. * @param {Vector2} max Maximum point of the box.
  31. */
  32. Box2.prototype.set = function(min, max)
  33. {
  34. this.min.copy(min);
  35. this.max.copy(max);
  36. return this;
  37. };
  38. /**
  39. * Set the box from a list of Vector2 points.
  40. *
  41. * @param {Array} points
  42. */
  43. Box2.prototype.setFromPoints = function(points)
  44. {
  45. this.min = new Vector2(+Infinity, +Infinity);
  46. this.max = new Vector2(-Infinity, -Infinity);
  47. for(var i = 0, il = points.length; i < il; i++)
  48. {
  49. this.expandByPoint(points[i]);
  50. }
  51. return this;
  52. };
  53. /**
  54. * Set the box minimum and maximum from center point and size.
  55. *
  56. * @param {Vector2} center
  57. * @param {Vector2} size
  58. */
  59. Box2.prototype.setFromCenterAndSize = function(center, size)
  60. {
  61. var v1 = new Vector2();
  62. var halfSize = v1.copy(size).multiplyScalar(0.5);
  63. this.min.copy(center).sub(halfSize);
  64. this.max.copy(center).add(halfSize);
  65. return this;
  66. };
  67. /**
  68. * Clone the box into a new object.
  69. *
  70. * Should be used when it it necessary to make operations to this box.
  71. *
  72. * @return {Box2} New box object with the copy of this object.
  73. */
  74. Box2.prototype.clone = function()
  75. {
  76. var box = new Box2();
  77. box.copy(this);
  78. return box;
  79. };
  80. /**
  81. * Copy the box value from another box.
  82. *
  83. * @param {Box2} point
  84. */
  85. Box2.prototype.copy = function(box)
  86. {
  87. this.min.copy(box.min);
  88. this.max.copy(box.max);
  89. };
  90. /**
  91. * Check if the box is empty (size equals zero or is negative).
  92. *
  93. * The box size is condireded valid on two negative axis.
  94. *
  95. * @return {boolean} True if the box is empty.
  96. */
  97. Box2.prototype.isEmpty = function()
  98. {
  99. return (this.max.x < this.min.x) || (this.max.y < this.min.y);
  100. };
  101. /**
  102. * Calculate the center point of the box.
  103. *
  104. * @param {Vector2} [target] Vector to store the result.
  105. * @return {Vector2} Central point of the box.
  106. */
  107. Box2.prototype.getCenter = function(target)
  108. {
  109. if(target === undefined)
  110. {
  111. target = new Vector2();
  112. }
  113. this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  114. return target;
  115. };
  116. /**
  117. * Get the size of the box from its min and max points.
  118. *
  119. * @param {Vector2} [target] Vector to store the result.
  120. * @return {Vector2} Vector with the calculated size.
  121. */
  122. Box2.prototype.getSize = function(target)
  123. {
  124. if(target === undefined)
  125. {
  126. target = new Vector2();
  127. }
  128. this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  129. return target;
  130. };
  131. /**
  132. * Expand the box to contain a new point.
  133. *
  134. * @param {Vector2} point
  135. */
  136. Box2.prototype.expandByPoint = function(point)
  137. {
  138. this.min.min(point);
  139. this.max.max(point);
  140. return this;
  141. };
  142. /**
  143. * Expand the box by adding a border with the vector size.
  144. *
  145. * Vector is subtracted from min and added to the max points.
  146. *
  147. * @param {Vector2} vector
  148. */
  149. Box2.prototype.expandByVector = function(vector)
  150. {
  151. this.min.sub(vector);
  152. this.max.add(vector);
  153. };
  154. /**
  155. * Expand the box by adding a border with the scalar value.
  156. *
  157. * @param {number} scalar
  158. */
  159. Box2.prototype.expandByScalar = function(scalar)
  160. {
  161. this.min.addScalar(-scalar);
  162. this.max.addScalar(scalar);
  163. };
  164. /**
  165. * Check if the box contains a point inside.
  166. *
  167. * @param {Vector2} point
  168. * @return {boolean} True if the box contains point.
  169. */
  170. Box2.prototype.containsPoint = function(point)
  171. {
  172. return !(point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y);
  173. };
  174. /**
  175. * Check if the box fully contains another box inside (different from intersects box).
  176. *
  177. * Only returns true if the box is fully contained.
  178. *
  179. * @param {Box2} box
  180. * @return {boolean} True if the box contains box.
  181. */
  182. Box2.prototype.containsBox = function(box)
  183. {
  184. return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y;
  185. };
  186. /**
  187. * Check if two boxes intersect each other, using 4 splitting planes to rule out intersections.
  188. *
  189. * @param {Box2} box
  190. * @return {boolean} True if the boxes intersect each other.
  191. */
  192. Box2.prototype.intersectsBox = function(box)
  193. {
  194. return !(box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y);
  195. };
  196. /**
  197. * Calculate the distance to a point.
  198. *
  199. * @param {Vector2} point
  200. * @return {number} Distance to point calculated.
  201. */
  202. Box2.prototype.distanceToPoint = function(point)
  203. {
  204. var v = new Vector2();
  205. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  206. return clampedPoint.sub(point).length();
  207. };
  208. /**
  209. * Make a intersection between this box and another box.
  210. *
  211. * Store the result in this object.
  212. *
  213. * @param {Box2} box
  214. */
  215. Box2.prototype.intersect = function(box)
  216. {
  217. this.min.max(box.min);
  218. this.max.min(box.max);
  219. };
  220. /**
  221. * Make a union between this box and another box.
  222. *
  223. * Store the result in this object.
  224. *
  225. * @param {Box2} box
  226. */
  227. Box2.prototype.union = function(box)
  228. {
  229. this.min.min(box.min);
  230. this.max.max(box.max);
  231. };
  232. /**
  233. * Translate the box by a offset value, adds the offset to booth min and max.
  234. *
  235. * @param {Vector2} offset
  236. */
  237. Box2.prototype.translate = function(offset)
  238. {
  239. this.min.add(offset);
  240. this.max.add(offset);
  241. };
  242. /**
  243. * Checks if two boxes are equal.
  244. *
  245. * @param {Box2} box
  246. * @return {boolean} True if the two boxes are equal.
  247. */
  248. Box2.prototype.equals = function(box)
  249. {
  250. return box.min.equals(this.min) && box.max.equals(this.max);
  251. };
  252. /**
  253. * Store the box data into a numeric array.
  254. *
  255. * @return {number[]} Numeric array with box data min and max.
  256. */
  257. Box2.prototype.toArray = function()
  258. {
  259. return [this.min.x, this.min.y, this.max.x, this.max.y];
  260. };
  261. /**
  262. * Set box data min and max from numeric array.
  263. *
  264. * @param {number[]} array Numeric array with box data min and max.
  265. */
  266. Box2.prototype.fromArray = function(array)
  267. {
  268. this.min.set(array[0], array[1]);
  269. this.max.set(array[2], array[3]);
  270. };
  271. export {Box2};