Box2.js 5.5 KB

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