circleintersection.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getCenter = exports.circleCircleIntersection = exports.circleOverlap = exports.distance = exports.circleArea = exports.containedInCircles = exports.intersectionArea = void 0;
  4. var SMALL = 1e-10;
  5. /** Returns the intersection area of a bunch of circles (where each circle
  6. is an object having an x,y and radius property) */
  7. function intersectionArea(circles, stats) {
  8. // get all the intersection points of the circles
  9. var intersectionPoints = getIntersectionPoints(circles);
  10. // filter out points that aren't included in all the circles
  11. var innerPoints = intersectionPoints.filter(function (p) {
  12. return containedInCircles(p, circles);
  13. });
  14. var arcArea = 0, polygonArea = 0, i;
  15. var arcs = [];
  16. // if we have intersection points that are within all the circles,
  17. // then figure out the area contained by them
  18. if (innerPoints.length > 1) {
  19. // sort the points by angle from the center of the polygon, which lets
  20. // us just iterate over points to get the edges
  21. var center = getCenter(innerPoints);
  22. for (i = 0; i < innerPoints.length; ++i) {
  23. var p = innerPoints[i];
  24. p.angle = Math.atan2(p.x - center.x, p.y - center.y);
  25. }
  26. innerPoints.sort(function (a, b) {
  27. return b.angle - a.angle;
  28. });
  29. // iterate over all points, get arc between the points
  30. // and update the areas
  31. var p2 = innerPoints[innerPoints.length - 1];
  32. for (i = 0; i < innerPoints.length; ++i) {
  33. var p1 = innerPoints[i];
  34. // polygon area updates easily ...
  35. polygonArea += (p2.x + p1.x) * (p1.y - p2.y);
  36. // updating the arc area is a little more involved
  37. var midPoint = { x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2 };
  38. var arc = null;
  39. for (var j = 0; j < p1.parentIndex.length; ++j) {
  40. if (p2.parentIndex.indexOf(p1.parentIndex[j]) > -1) {
  41. // figure out the angle halfway between the two points
  42. // on the current circle
  43. var circle = circles[p1.parentIndex[j]], a1 = Math.atan2(p1.x - circle.x, p1.y - circle.y), a2 = Math.atan2(p2.x - circle.x, p2.y - circle.y);
  44. var angleDiff = a2 - a1;
  45. if (angleDiff < 0) {
  46. angleDiff += 2 * Math.PI;
  47. }
  48. // and use that angle to figure out the width of the
  49. // arc
  50. var a = a2 - angleDiff / 2;
  51. var width = distance(midPoint, {
  52. x: circle.x + circle.radius * Math.sin(a),
  53. y: circle.y + circle.radius * Math.cos(a),
  54. });
  55. // clamp the width to the largest is can actually be
  56. // (sometimes slightly overflows because of FP errors)
  57. if (width > circle.radius * 2) {
  58. width = circle.radius * 2;
  59. }
  60. // pick the circle whose arc has the smallest width
  61. if (arc === null || arc.width > width) {
  62. arc = { circle: circle, width: width, p1: p1, p2: p2 };
  63. }
  64. }
  65. }
  66. if (arc !== null) {
  67. arcs.push(arc);
  68. arcArea += circleArea(arc.circle.radius, arc.width);
  69. p2 = p1;
  70. }
  71. }
  72. }
  73. else {
  74. // no intersection points, is either disjoint - or is completely
  75. // overlapped. figure out which by examining the smallest circle
  76. var smallest = circles[0];
  77. for (i = 1; i < circles.length; ++i) {
  78. if (circles[i].radius < smallest.radius) {
  79. smallest = circles[i];
  80. }
  81. }
  82. // make sure the smallest circle is completely contained in all
  83. // the other circles
  84. var disjoint = false;
  85. for (i = 0; i < circles.length; ++i) {
  86. if (distance(circles[i], smallest) > Math.abs(smallest.radius - circles[i].radius)) {
  87. disjoint = true;
  88. break;
  89. }
  90. }
  91. if (disjoint) {
  92. arcArea = polygonArea = 0;
  93. }
  94. else {
  95. arcArea = smallest.radius * smallest.radius * Math.PI;
  96. arcs.push({
  97. circle: smallest,
  98. p1: { x: smallest.x, y: smallest.y + smallest.radius },
  99. p2: { x: smallest.x - SMALL, y: smallest.y + smallest.radius },
  100. width: smallest.radius * 2,
  101. });
  102. }
  103. }
  104. polygonArea /= 2;
  105. if (stats) {
  106. stats.area = arcArea + polygonArea;
  107. stats.arcArea = arcArea;
  108. stats.polygonArea = polygonArea;
  109. stats.arcs = arcs;
  110. stats.innerPoints = innerPoints;
  111. stats.intersectionPoints = intersectionPoints;
  112. }
  113. return arcArea + polygonArea;
  114. }
  115. exports.intersectionArea = intersectionArea;
  116. /** returns whether a point is contained by all of a list of circles */
  117. function containedInCircles(point, circles) {
  118. for (var i = 0; i < circles.length; ++i) {
  119. if (distance(point, circles[i]) > circles[i].radius + SMALL) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. exports.containedInCircles = containedInCircles;
  126. /** Gets all intersection points between a bunch of circles */
  127. function getIntersectionPoints(circles) {
  128. var ret = [];
  129. for (var i = 0; i < circles.length; ++i) {
  130. for (var j = i + 1; j < circles.length; ++j) {
  131. var intersect = circleCircleIntersection(circles[i], circles[j]);
  132. for (var k = 0; k < intersect.length; ++k) {
  133. var p = intersect[k];
  134. p.parentIndex = [i, j];
  135. ret.push(p);
  136. }
  137. }
  138. }
  139. return ret;
  140. }
  141. /** Circular segment area calculation. See http://mathworld.wolfram.com/CircularSegment.html */
  142. function circleArea(r, width) {
  143. return r * r * Math.acos(1 - width / r) - (r - width) * Math.sqrt(width * (2 * r - width));
  144. }
  145. exports.circleArea = circleArea;
  146. /** euclidean distance between two points */
  147. function distance(p1, p2) {
  148. return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
  149. }
  150. exports.distance = distance;
  151. /** Returns the overlap area of two circles of radius r1 and r2 - that
  152. have their centers separated by distance d. Simpler faster
  153. circle intersection for only two circles */
  154. function circleOverlap(r1, r2, d) {
  155. // no overlap
  156. if (d >= r1 + r2) {
  157. return 0;
  158. }
  159. // completely overlapped
  160. if (d <= Math.abs(r1 - r2)) {
  161. return Math.PI * Math.min(r1, r2) * Math.min(r1, r2);
  162. }
  163. var w1 = r1 - (d * d - r2 * r2 + r1 * r1) / (2 * d), w2 = r2 - (d * d - r1 * r1 + r2 * r2) / (2 * d);
  164. return circleArea(r1, w1) + circleArea(r2, w2);
  165. }
  166. exports.circleOverlap = circleOverlap;
  167. /** Given two circles (containing a x/y/radius attributes),
  168. returns the intersecting points if possible.
  169. note: doesn't handle cases where there are infinitely many
  170. intersection points (circles are equivalent):, or only one intersection point*/
  171. function circleCircleIntersection(p1, p2) {
  172. var d = distance(p1, p2), r1 = p1.radius, r2 = p2.radius;
  173. // if to far away, or self contained - can't be done
  174. if (d >= r1 + r2 || d <= Math.abs(r1 - r2)) {
  175. return [];
  176. }
  177. var a = (r1 * r1 - r2 * r2 + d * d) / (2 * d), h = Math.sqrt(r1 * r1 - a * a), x0 = p1.x + (a * (p2.x - p1.x)) / d, y0 = p1.y + (a * (p2.y - p1.y)) / d, rx = -(p2.y - p1.y) * (h / d), ry = -(p2.x - p1.x) * (h / d);
  178. return [
  179. { x: x0 + rx, y: y0 - ry },
  180. { x: x0 - rx, y: y0 + ry },
  181. ];
  182. }
  183. exports.circleCircleIntersection = circleCircleIntersection;
  184. /** Returns the center of a bunch of points */
  185. function getCenter(points) {
  186. var center = { x: 0, y: 0 };
  187. for (var i = 0; i < points.length; ++i) {
  188. center.x += points[i].x;
  189. center.y += points[i].y;
  190. }
  191. center.x /= points.length;
  192. center.y /= points.length;
  193. return center;
  194. }
  195. exports.getCenter = getCenter;
  196. //# sourceMappingURL=circleintersection.js.map