circleintersection.js 8.1 KB

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