Box2.js 3.2 KB

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