math_Box2.js.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: math/Box2.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: math/Box2.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. import {Vector2} from "./Vector2.js";
  21. /**
  22. * Box is described by a minimum and maximum points.
  23. *
  24. * Can be used for collision detection with points and other boxes.
  25. *
  26. * @class
  27. */
  28. function Box2(min, max)
  29. {
  30. this.min = (min !== undefined) ? min : new Vector2();
  31. this.max = (max !== undefined) ? max : new Vector2();
  32. }
  33. /**
  34. * Set the box values.
  35. */
  36. Box2.prototype.set = function(min, max)
  37. {
  38. this.min.copy(min);
  39. this.max.copy(max);
  40. return this;
  41. };
  42. /**
  43. * Set the box from a list of Vector2 points.
  44. */
  45. Box2.prototype.setFromPoints = function(points)
  46. {
  47. this.min = new Vector2(+Infinity, +Infinity);
  48. this.max = new Vector2(-Infinity, -Infinity);
  49. for(var i = 0, il = points.length; i &lt; il; i++)
  50. {
  51. this.expandByPoint(points[i]);
  52. }
  53. return this;
  54. };
  55. /**
  56. * Set the box minimum and maximum from center point and size.
  57. */
  58. Box2.prototype.setFromCenterAndSize = function(center, size)
  59. {
  60. var v1 = new Vector2();
  61. var halfSize = v1.copy(size).multiplyScalar(0.5);
  62. this.min.copy(center).sub(halfSize);
  63. this.max.copy(center).add(halfSize);
  64. return this;
  65. };
  66. /**
  67. * Clone the box into a new object.
  68. */
  69. Box2.prototype.clone = function()
  70. {
  71. var box = new Box2();
  72. box.copy(this);
  73. return box;
  74. };
  75. /**
  76. * Copy the box value from another box.
  77. */
  78. Box2.prototype.copy = function(box)
  79. {
  80. this.min.copy(box.min);
  81. this.max.copy(box.max);
  82. };
  83. /**
  84. * Check if the box is empty (size equals zero or is negative).
  85. *
  86. * The box size is condireded valid on two negative axis.
  87. */
  88. Box2.prototype.isEmpty = function()
  89. {
  90. return (this.max.x &lt; this.min.x) || (this.max.y &lt; this.min.y);
  91. };
  92. /**
  93. * Calculate the center point of the box.
  94. */
  95. Box2.prototype.getCenter = function(target)
  96. {
  97. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  98. };
  99. /**
  100. * Get the size of the box.
  101. */
  102. Box2.prototype.getSize = function(target)
  103. {
  104. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  105. };
  106. /**
  107. * Expand the box to contain a new point.
  108. */
  109. Box2.prototype.expandByPoint = function(point)
  110. {
  111. this.min.min(point);
  112. this.max.max(point);
  113. return this;
  114. };
  115. /**
  116. * Expand the box by adding a border with the vector size.
  117. *
  118. * Vector is subtracted from min and added to the max points.
  119. */
  120. Box2.prototype.expandByVector = function(vector)
  121. {
  122. this.min.sub(vector);
  123. this.max.add(vector);
  124. };
  125. /**
  126. * Expand the box by adding a border with the scalar value.
  127. */
  128. Box2.prototype.expandByScalar = function(scalar)
  129. {
  130. this.min.addScalar(-scalar);
  131. this.max.addScalar(scalar);
  132. };
  133. /**
  134. * Check if the box contains a point inside.
  135. *
  136. * @param {Vector2} point
  137. * @return {boolean} True if the box contains point.
  138. */
  139. Box2.prototype.containsPoint = function(point)
  140. {
  141. return point.x &lt; this.min.x || point.x > this.max.x || point.y &lt; this.min.y || point.y > this.max.y ? false : true;
  142. };
  143. /**
  144. * Check if the box fully contains another box inside (different from intersects box).
  145. *
  146. * Only returns true if the box is fully contained.
  147. *
  148. * @param {Box2} box
  149. * @return {boolean} True if the box contains box.
  150. */
  151. Box2.prototype.containsBox = function(box)
  152. {
  153. return this.min.x &lt;= box.min.x &amp;&amp; box.max.x &lt;= this.max.x &amp;&amp; this.min.y &lt;= box.min.y &amp;&amp; box.max.y &lt;= this.max.y;
  154. };
  155. /**
  156. * Check if two boxes intersect each other, using 4 splitting planes to rule out intersections.
  157. *
  158. * @param {Box2} box
  159. * @return {boolean} True if the boxes intersect each other.
  160. */
  161. Box2.prototype.intersectsBox = function(box)
  162. {
  163. return box.max.x &lt; this.min.x || box.min.x > this.max.x || box.max.y &lt; this.min.y || box.min.y > this.max.y ? false : true;
  164. };
  165. Box2.prototype.clampPoint = function(point, target)
  166. {
  167. return target.copy(point).clamp(this.min, this.max);
  168. };
  169. /**
  170. * Calculate the distance to a point.
  171. *
  172. * @param {Vector2} point
  173. */
  174. Box2.prototype.distanceToPoint = function(point)
  175. {
  176. var v = new Vector2();
  177. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  178. return clampedPoint.sub(point).length();
  179. };
  180. /**
  181. * Make a intersection between this box and another box.
  182. *
  183. * Store the result in this object.
  184. *
  185. * @param {Box2} box
  186. */
  187. Box2.prototype.intersect = function(box)
  188. {
  189. this.min.max(box.min);
  190. this.max.min(box.max);
  191. };
  192. /**
  193. * Make a union between this box and another box.
  194. *
  195. * Store the result in this object.
  196. *
  197. * @param {Box2} box
  198. */
  199. Box2.prototype.union = function(box)
  200. {
  201. this.min.min(box.min);
  202. this.max.max(box.max);
  203. };
  204. /**
  205. * Translate the box by a offset value, adds the offset to booth min and max.
  206. *
  207. * @param {Vector2} offset
  208. */
  209. Box2.prototype.translate = function(offset)
  210. {
  211. this.min.add(offset);
  212. this.max.add(offset);
  213. };
  214. /**
  215. * Checks if two boxes are equal.
  216. *
  217. * @param {Box2} box
  218. * @return {boolean} True if the two boxes are equal.
  219. */
  220. Box2.prototype.equals = function(box)
  221. {
  222. return box.min.equals(this.min) &amp;&amp; box.max.equals(this.max);
  223. };
  224. export {Box2};
  225. </code></pre>
  226. </article>
  227. </section>
  228. </div>
  229. <nav>
  230. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li></ul>
  231. </nav>
  232. <br class="clear">
  233. <footer>
  234. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Fri Jun 07 2019 11:44:40 GMT+0100 (Western European Summer Time)
  235. </footer>
  236. <script> prettyPrint(); </script>
  237. <script src="scripts/linenumber.js"> </script>
  238. </body>
  239. </html>