Box2.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. THREE.Box2 = function ( min, max ) {
  5. if ( min == undefined && max === undefined ) {
  6. this.min = new THREE.Vector2();
  7. this.max = new THREE.Vector2();
  8. this.makeEmpty();
  9. } else {
  10. this.min = min || new THREE.Vector2();
  11. this.max = max || new THREE.Vector2().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
  12. }
  13. };
  14. THREE.Box2.prototype = {
  15. constructor: THREE.Box2,
  16. set: function ( min, max ) {
  17. this.min = min;
  18. this.max = max;
  19. return this;
  20. },
  21. setFromPoints: function ( points ) {
  22. if ( points.length > 0 ) {
  23. var p = points[ 0 ];
  24. this.min.copy( p );
  25. this.max.copy( p );
  26. for ( var i = 1, il = points.length; i < il; i ++ ) {
  27. p = points[ i ];
  28. if ( p.x < this.min.x ) {
  29. this.min.x = p.x;
  30. } else if ( p.x > this.max.x ) {
  31. this.max.x = p.x;
  32. }
  33. if ( p.y < this.min.y ) {
  34. this.min.y = p.y;
  35. } else if ( p.y > this.max.y ) {
  36. this.max.y = p.y;
  37. }
  38. }
  39. } else {
  40. this.makeEmpty();
  41. }
  42. return this;
  43. },
  44. setFromCenterAndSize: function ( center, size ) {
  45. var halfSize = THREE.Box2.__v1.copy( size ).multiplyScalar( 0.5 );
  46. this.min.copy( center ).subSelf( halfSize );
  47. this.max.copy( center ).addSelf( halfSize );
  48. return this;
  49. },
  50. copy: function ( box ) {
  51. this.min.copy( box.min );
  52. this.max.copy( box.max );
  53. return this;
  54. },
  55. makeEmpty: function () {
  56. this.min.x = this.min.y = Infinity;
  57. this.max.x = this.max.y = -Infinity;
  58. return this;
  59. },
  60. empty: function () {
  61. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  62. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  63. },
  64. volume: function () {
  65. return ( this.max.x - this.min.x ) * ( this.max.y - this.min.y );
  66. },
  67. center: function () {
  68. return new THREE.Vector2().add( this.min, this.max ).multiplyScalar( 0.5 );
  69. },
  70. size: function () {
  71. return new THREE.Vector2().sub( this.max, this.min );
  72. },
  73. expandByPoint: function ( point ) {
  74. this.min.minSelf( point );
  75. this.max.maxSelf( point );
  76. return this;
  77. },
  78. expandByVector: function ( vector ) {
  79. this.min.subSelf( vector );
  80. this.max.addSelf( vector );
  81. return this;
  82. },
  83. expandByScalar: function ( scalar ) {
  84. this.min.addScalar( -scalar );
  85. this.max.addScalar( scalar );
  86. return this;
  87. },
  88. containsPoint: function ( point ) {
  89. if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
  90. ( this.min.y <= point.y ) && ( point.y <= this.max.y ) ) {
  91. return true;
  92. }
  93. return false;
  94. },
  95. containsBox: function ( box ) {
  96. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  97. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) {
  98. return true;
  99. }
  100. return false;
  101. },
  102. getParameter: function ( point ) {
  103. // This can potentially have a divide by zero if the box
  104. // has a size dimension of 0.
  105. return new THREE.Vector2(
  106. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  107. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  108. );
  109. },
  110. isIntersection: function ( box ) {
  111. // using 6 splitting planes to rule out intersections.
  112. if ( ( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
  113. ( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ) {
  114. return false;
  115. }
  116. return true;
  117. },
  118. clampPoint: function ( point ) {
  119. return new THREE.Vector2().copy( point ).clampSelf( this.min, this.max );
  120. },
  121. distanceToPoint: function ( point ) {
  122. return this.clampPoint( point ).subSelf( point ).length();
  123. },
  124. intersect: function ( box ) {
  125. this.min.maxSelf( box.min );
  126. this.max.minSelf( box.max );
  127. return this;
  128. },
  129. union: function ( box ) {
  130. this.min.minSelf( box.min );
  131. this.max.maxSelf( box.max );
  132. return this;
  133. },
  134. translate: function ( offset ) {
  135. this.min.addSelf( offset );
  136. this.max.addSelf( offset );
  137. return this;
  138. },
  139. scale: function ( factor ) {
  140. var sizeDeltaHalf = this.size().multiplyScalar( ( 1 - factor ) * 0.5 );
  141. this.expandByVector( sizeDeltaHalf );
  142. return this;
  143. },
  144. equals: function ( box ) {
  145. return box.min.equals( this.min ) && box.max.equals( this.max );
  146. },
  147. clone: function () {
  148. return new THREE.Box2().copy( this );
  149. }
  150. };
  151. THREE.Box2.__v1 = new THREE.Vector2();
粤ICP备19079148号