math_Box2.js.html 8.6 KB

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