circleintersection.js 7.6 KB

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