math_Box2.js.html 7.6 KB

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