Rectangle.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. THREE.Rectangle = function () {
  5. var _left, _top, _right, _bottom,
  6. _width, _height, _isEmpty = true;
  7. function resize() {
  8. _width = _right - _left;
  9. _height = _bottom - _top;
  10. }
  11. this.getX = function () {
  12. return _left;
  13. };
  14. this.getY = function () {
  15. return _top;
  16. };
  17. this.getWidth = function () {
  18. return _width;
  19. };
  20. this.getHeight = function () {
  21. return _height;
  22. };
  23. this.getLeft = function() {
  24. return _left;
  25. };
  26. this.getTop = function() {
  27. return _top;
  28. };
  29. this.getRight = function() {
  30. return _right;
  31. };
  32. this.getBottom = function() {
  33. return _bottom;
  34. };
  35. this.set = function ( left, top, right, bottom ) {
  36. _isEmpty = false;
  37. _left = left; _top = top;
  38. _right = right; _bottom = bottom;
  39. resize();
  40. };
  41. this.addPoint = function ( x, y ) {
  42. if ( _isEmpty ) {
  43. _isEmpty = false;
  44. _left = x; _top = y;
  45. _right = x; _bottom = y;
  46. resize();
  47. } else {
  48. _left = _left < x ? _left : x; // Math.min( _left, x );
  49. _top = _top < y ? _top : y; // Math.min( _top, y );
  50. _right = _right > x ? _right : x; // Math.max( _right, x );
  51. _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y );
  52. resize();
  53. }
  54. };
  55. this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) {
  56. if (_isEmpty) {
  57. _isEmpty = false;
  58. _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 );
  59. _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 );
  60. _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 );
  61. _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 );
  62. resize();
  63. } else {
  64. _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) );
  65. _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) );
  66. _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) );
  67. _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) );
  68. resize();
  69. };
  70. };
  71. this.addRectangle = function ( r ) {
  72. if ( _isEmpty ) {
  73. _isEmpty = false;
  74. _left = r.getLeft(); _top = r.getTop();
  75. _right = r.getRight(); _bottom = r.getBottom();
  76. resize();
  77. } else {
  78. _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() );
  79. _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() );
  80. _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() );
  81. _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() );
  82. resize();
  83. }
  84. };
  85. this.inflate = function ( v ) {
  86. _left -= v; _top -= v;
  87. _right += v; _bottom += v;
  88. resize();
  89. };
  90. this.minSelf = function ( r ) {
  91. _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() );
  92. _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() );
  93. _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() );
  94. _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() );
  95. resize();
  96. };
  97. this.intersects = function ( r ) {
  98. // http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/
  99. if ( _right < r.getLeft() ) return false;
  100. if ( _left > r.getRight() ) return false;
  101. if ( _bottom < r.getTop() ) return false;
  102. if ( _top > r.getBottom() ) return false;
  103. return true;
  104. };
  105. this.empty = function () {
  106. _isEmpty = true;
  107. _left = 0; _top = 0;
  108. _right = 0; _bottom = 0;
  109. resize();
  110. };
  111. this.isEmpty = function () {
  112. return _isEmpty;
  113. };
  114. };
粤ICP备19079148号