math_Box2.js.html 9.4 KB

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