Box2.js 3.1 KB

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