Rectangle.js 2.7 KB

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