Box2.js 4.1 KB

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