Box2.js 3.2 KB

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