math_Box2.js.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Box2.prototype.isEmpty = function()
  84. {
  85. // this is a more robust check for empty than ( volume &lt;= 0 ) because volume can get positive with two negative axes
  86. return (this.max.x &lt; this.min.x) || (this.max.y &lt; this.min.y);
  87. };
  88. Box2.prototype.getCenter = function(target)
  89. {
  90. return this.isEmpty() ? target.set(0, 0) : target.addVectors(this.min, this.max).multiplyScalar(0.5);
  91. };
  92. Box2.prototype.getSize = function(target)
  93. {
  94. return this.isEmpty() ? target.set(0, 0) : target.subVectors(this.max, this.min);
  95. };
  96. Box2.prototype.expandByPoint = function(point)
  97. {
  98. this.min.min(point);
  99. this.max.max(point);
  100. return this;
  101. };
  102. Box2.prototype.expandByVector = function(vector)
  103. {
  104. this.min.sub(vector);
  105. this.max.add(vector);
  106. };
  107. Box2.prototype.expandByScalar = function(scalar)
  108. {
  109. this.min.addScalar(-scalar);
  110. this.max.addScalar(scalar);
  111. };
  112. Box2.prototype.containsPoint = function(point)
  113. {
  114. 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;
  115. };
  116. Box2.prototype.containsBox = function(box)
  117. {
  118. 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;
  119. };
  120. /**
  121. * Check if two boxes intersect each other, using 4 splitting planes to rule out intersections.
  122. *
  123. * @param {Box2} box
  124. */
  125. Box2.prototype.intersectsBox = function(box)
  126. {
  127. 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;
  128. };
  129. Box2.prototype.clampPoint = function(point, target)
  130. {
  131. return target.copy(point).clamp(this.min, this.max);
  132. };
  133. /**
  134. * Calculate the distance to a point.
  135. *
  136. * @param {Vector2} point
  137. */
  138. Box2.prototype.distanceToPoint = function(point)
  139. {
  140. var v = new Vector2();
  141. var clampedPoint = v.copy(point).clamp(this.min, this.max);
  142. return clampedPoint.sub(point).length();
  143. };
  144. /**
  145. * Make a intersection between this box and another box.
  146. *
  147. * Store the result in this object.
  148. *
  149. * @param {Box2} box
  150. */
  151. Box2.prototype.intersect = function(box)
  152. {
  153. this.min.max(box.min);
  154. this.max.min(box.max);
  155. };
  156. /**
  157. * Make a union between this box and another box.
  158. *
  159. * Store the result in this object.
  160. *
  161. * @param {Box2} box
  162. */
  163. Box2.prototype.union = function(box)
  164. {
  165. this.min.min(box.min);
  166. this.max.max(box.max);
  167. };
  168. /**
  169. * Translate the box by a offset value, adds the offset to booth min and max.
  170. *
  171. * @param {Vector2} offset
  172. */
  173. Box2.prototype.translate = function(offset)
  174. {
  175. this.min.add(offset);
  176. this.max.add(offset);
  177. };
  178. /**
  179. * Checks if two boxes are equal.
  180. *
  181. * @param {Box2} box
  182. * @return {boolean} True if the two boxes are equal.
  183. */
  184. Box2.prototype.equals = function(box)
  185. {
  186. return box.min.equals(this.min) &amp;&amp; box.max.equals(this.max);
  187. };
  188. export {Box2};
  189. </code></pre>
  190. </article>
  191. </section>
  192. </div>
  193. <nav>
  194. <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="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</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="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="Stencil.html">Stencil</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>
  195. </nav>
  196. <br class="clear">
  197. <footer>
  198. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.2</a> on Thu Jun 06 2019 12:05:48 GMT+0100 (Western European Summer Time)
  199. </footer>
  200. <script> prettyPrint(); </script>
  201. <script src="scripts/linenumber.js"> </script>
  202. </body>
  203. </html>