Box2.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { Vector2 } from './Vector2.js';
  2. /**
  3. * @author bhouston / http://clara.io
  4. */
  5. var _vector = new Vector2();
  6. function Box2( min, max ) {
  7. this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
  8. this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );
  9. }
  10. Object.assign( Box2.prototype, {
  11. set: function ( min, max ) {
  12. this.min.copy( min );
  13. this.max.copy( max );
  14. return this;
  15. },
  16. setFromPoints: function ( points ) {
  17. this.makeEmpty();
  18. for ( var i = 0, il = points.length; i < il; i ++ ) {
  19. this.expandByPoint( points[ i ] );
  20. }
  21. return this;
  22. },
  23. setFromCenterAndSize: function ( center, size ) {
  24. var halfSize = _vector.copy( size ).multiplyScalar( 0.5 );
  25. this.min.copy( center ).sub( halfSize );
  26. this.max.copy( center ).add( halfSize );
  27. return this;
  28. },
  29. clone: function () {
  30. return new this.constructor().copy( this );
  31. },
  32. copy: function ( box ) {
  33. this.min.copy( box.min );
  34. this.max.copy( box.max );
  35. return this;
  36. },
  37. makeEmpty: function () {
  38. this.min.x = this.min.y = + Infinity;
  39. this.max.x = this.max.y = - Infinity;
  40. return this;
  41. },
  42. isEmpty: function () {
  43. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  44. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  45. },
  46. getCenter: function ( target ) {
  47. if ( target === undefined ) {
  48. console.warn( 'THREE.Box2: .getCenter() target is now required' );
  49. target = new Vector2();
  50. }
  51. return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  52. },
  53. getSize: function ( target ) {
  54. if ( target === undefined ) {
  55. console.warn( 'THREE.Box2: .getSize() target is now required' );
  56. target = new Vector2();
  57. }
  58. return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );
  59. },
  60. expandByPoint: function ( point ) {
  61. this.min.min( point );
  62. this.max.max( point );
  63. return this;
  64. },
  65. expandByVector: function ( vector ) {
  66. this.min.sub( vector );
  67. this.max.add( vector );
  68. return this;
  69. },
  70. expandByScalar: function ( scalar ) {
  71. this.min.addScalar( - scalar );
  72. this.max.addScalar( scalar );
  73. return this;
  74. },
  75. containsPoint: function ( point ) {
  76. return point.x < this.min.x || point.x > this.max.x ||
  77. point.y < this.min.y || point.y > this.max.y ? false : true;
  78. },
  79. containsBox: function ( box ) {
  80. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  81. this.min.y <= box.min.y && box.max.y <= this.max.y;
  82. },
  83. getParameter: function ( point, target ) {
  84. // This can potentially have a divide by zero if the box
  85. // has a size dimension of 0.
  86. if ( target === undefined ) {
  87. console.warn( 'THREE.Box2: .getParameter() target is now required' );
  88. target = new Vector2();
  89. }
  90. return target.set(
  91. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  92. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  93. );
  94. },
  95. intersectsBox: function ( box ) {
  96. // using 4 splitting planes to rule out intersections
  97. return box.max.x < this.min.x || box.min.x > this.max.x ||
  98. box.max.y < this.min.y || box.min.y > this.max.y ? false : true;
  99. },
  100. clampPoint: function ( point, target ) {
  101. if ( target === undefined ) {
  102. console.warn( 'THREE.Box2: .clampPoint() target is now required' );
  103. target = new Vector2();
  104. }
  105. return target.copy( point ).clamp( this.min, this.max );
  106. },
  107. distanceToPoint: function ( point ) {
  108. var clampedPoint = _vector.copy( point ).clamp( this.min, this.max );
  109. return clampedPoint.sub( point ).length();
  110. },
  111. intersect: function ( box ) {
  112. this.min.max( box.min );
  113. this.max.min( box.max );
  114. return this;
  115. },
  116. union: function ( box ) {
  117. this.min.min( box.min );
  118. this.max.max( box.max );
  119. return this;
  120. },
  121. translate: function ( offset ) {
  122. this.min.add( offset );
  123. this.max.add( offset );
  124. return this;
  125. },
  126. equals: function ( box ) {
  127. return box.min.equals( this.min ) && box.max.equals( this.max );
  128. }
  129. } );
  130. export { Box2 };