Box2.js 3.2 KB

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