Box2.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { Vector2 } from './Vector2';
  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. Box2.prototype = {
  10. constructor: Box2,
  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 () {
  24. var v1 = new Vector2();
  25. return function setFromCenterAndSize( center, size ) {
  26. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  27. this.min.copy( center ).sub( halfSize );
  28. this.max.copy( center ).add( halfSize );
  29. return this;
  30. };
  31. }(),
  32. clone: function () {
  33. return new this.constructor().copy( this );
  34. },
  35. copy: function ( box ) {
  36. this.min.copy( box.min );
  37. this.max.copy( box.max );
  38. return this;
  39. },
  40. makeEmpty: function () {
  41. this.min.x = this.min.y = + Infinity;
  42. this.max.x = this.max.y = - Infinity;
  43. return this;
  44. },
  45. isEmpty: function () {
  46. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  47. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  48. },
  49. getCenter: function ( optionalTarget ) {
  50. var result = optionalTarget || new Vector2();
  51. return this.isEmpty() ? result.set( 0, 0 ) : result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  52. },
  53. getSize: function ( optionalTarget ) {
  54. var result = optionalTarget || new Vector2();
  55. return this.isEmpty() ? result.set( 0, 0 ) : result.subVectors( this.max, this.min );
  56. },
  57. expandByPoint: function ( point ) {
  58. this.min.min( point );
  59. this.max.max( point );
  60. return this;
  61. },
  62. expandByVector: function ( vector ) {
  63. this.min.sub( vector );
  64. this.max.add( vector );
  65. return this;
  66. },
  67. expandByScalar: function ( scalar ) {
  68. this.min.addScalar( - scalar );
  69. this.max.addScalar( scalar );
  70. return this;
  71. },
  72. containsPoint: function ( point ) {
  73. if ( point.x < this.min.x || point.x > this.max.x ||
  74. point.y < this.min.y || point.y > this.max.y ) {
  75. return false;
  76. }
  77. return true;
  78. },
  79. containsBox: function ( box ) {
  80. if ( ( 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. return true;
  83. }
  84. return false;
  85. },
  86. getParameter: function ( point, optionalTarget ) {
  87. // This can potentially have a divide by zero if the box
  88. // has a size dimension of 0.
  89. var result = optionalTarget || new Vector2();
  90. return result.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 6 splitting planes to rule out intersections.
  97. if ( 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 ) {
  99. return false;
  100. }
  101. return true;
  102. },
  103. clampPoint: function ( point, optionalTarget ) {
  104. var result = optionalTarget || new Vector2();
  105. return result.copy( point ).clamp( this.min, this.max );
  106. },
  107. distanceToPoint: function () {
  108. var v1 = new Vector2();
  109. return function distanceToPoint( point ) {
  110. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  111. return clampedPoint.sub( point ).length();
  112. };
  113. }(),
  114. intersect: function ( box ) {
  115. this.min.max( box.min );
  116. this.max.min( box.max );
  117. return this;
  118. },
  119. union: function ( box ) {
  120. this.min.min( box.min );
  121. this.max.max( box.max );
  122. return this;
  123. },
  124. translate: function ( offset ) {
  125. this.min.add( offset );
  126. this.max.add( offset );
  127. return this;
  128. },
  129. equals: function ( box ) {
  130. return box.min.equals( this.min ) && box.max.equals( this.max );
  131. }
  132. };
  133. export { Box2 };
粤ICP备19079148号