Rectangle.js 3.9 KB

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