Box2.js 4.1 KB

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