circleintersection.js 7.6 KB

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