Rectangle.js 2.2 KB

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