Box2.js 3.2 KB

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