diagram.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.intersectionAreaPath = exports.circleFromPath = exports.circlePath = exports.computeTextCentres = exports.computeTextCentre = void 0;
  4. var fmin_1 = require("fmin");
  5. var circleintersection_1 = require("./circleintersection");
  6. function circleMargin(current, interior, exterior) {
  7. var margin = interior[0].radius - (0, circleintersection_1.distance)(interior[0], current), i, m;
  8. for (i = 1; i < interior.length; ++i) {
  9. m = interior[i].radius - (0, circleintersection_1.distance)(interior[i], current);
  10. if (m <= margin) {
  11. margin = m;
  12. }
  13. }
  14. for (i = 0; i < exterior.length; ++i) {
  15. m = (0, circleintersection_1.distance)(exterior[i], current) - exterior[i].radius;
  16. if (m <= margin) {
  17. margin = m;
  18. }
  19. }
  20. return margin;
  21. }
  22. // compute the center of some circles by maximizing the margin of
  23. // the center point relative to the circles (interior) after subtracting
  24. // nearby circles (exterior)
  25. function computeTextCentre(interior, exterior) {
  26. // get an initial estimate by sampling around the interior circles
  27. // and taking the point with the biggest margin
  28. var points = [];
  29. var i;
  30. for (i = 0; i < interior.length; ++i) {
  31. var c = interior[i];
  32. points.push({ x: c.x, y: c.y });
  33. points.push({ x: c.x + c.radius / 2, y: c.y });
  34. points.push({ x: c.x - c.radius / 2, y: c.y });
  35. points.push({ x: c.x, y: c.y + c.radius / 2 });
  36. points.push({ x: c.x, y: c.y - c.radius / 2 });
  37. }
  38. var initial = points[0], margin = circleMargin(points[0], interior, exterior);
  39. for (i = 1; i < points.length; ++i) {
  40. var m = circleMargin(points[i], interior, exterior);
  41. if (m >= margin) {
  42. initial = points[i];
  43. margin = m;
  44. }
  45. }
  46. // maximize the margin numerically
  47. var solution = (0, fmin_1.nelderMead)(function (p) {
  48. return -1 * circleMargin({ x: p[0], y: p[1] }, interior, exterior);
  49. }, [initial.x, initial.y], { maxIterations: 500, minErrorDelta: 1e-10 }).x;
  50. var ret = { x: solution[0], y: solution[1] };
  51. // check solution, fallback as needed (happens if fully overlapped
  52. // etc)
  53. var valid = true;
  54. for (i = 0; i < interior.length; ++i) {
  55. if ((0, circleintersection_1.distance)(ret, interior[i]) > interior[i].radius) {
  56. valid = false;
  57. break;
  58. }
  59. }
  60. for (i = 0; i < exterior.length; ++i) {
  61. if ((0, circleintersection_1.distance)(ret, exterior[i]) < exterior[i].radius) {
  62. valid = false;
  63. break;
  64. }
  65. }
  66. if (!valid) {
  67. if (interior.length == 1) {
  68. ret = { x: interior[0].x, y: interior[0].y };
  69. }
  70. else {
  71. var areaStats = {};
  72. (0, circleintersection_1.intersectionArea)(interior, areaStats);
  73. if (areaStats.arcs.length === 0) {
  74. ret = { x: 0, y: -1000, disjoint: true };
  75. }
  76. else if (areaStats.arcs.length == 1) {
  77. ret = { x: areaStats.arcs[0].circle.x, y: areaStats.arcs[0].circle.y };
  78. }
  79. else if (exterior.length) {
  80. // try again without other circles
  81. ret = computeTextCentre(interior, []);
  82. }
  83. else {
  84. // take average of all the points in the intersection
  85. // polygon. this should basically never happen
  86. // and has some issues:
  87. // https://github.com/benfred/venn.js/issues/48#issuecomment-146069777
  88. ret = (0, circleintersection_1.getCenter)(areaStats.arcs.map(function (a) {
  89. return a.p1;
  90. }));
  91. }
  92. }
  93. }
  94. return ret;
  95. }
  96. exports.computeTextCentre = computeTextCentre;
  97. // given a dictionary of {setid : circle}, returns
  98. // a dictionary of setid to list of circles that completely overlap it
  99. function getOverlappingCircles(circles) {
  100. var ret = {}, circleids = [];
  101. for (var circleid in circles) {
  102. circleids.push(circleid);
  103. ret[circleid] = [];
  104. }
  105. for (var i = 0; i < circleids.length; i++) {
  106. var a = circles[circleids[i]];
  107. for (var j = i + 1; j < circleids.length; ++j) {
  108. var b = circles[circleids[j]], d = (0, circleintersection_1.distance)(a, b);
  109. if (d + b.radius <= a.radius + 1e-10) {
  110. ret[circleids[j]].push(circleids[i]);
  111. }
  112. else if (d + a.radius <= b.radius + 1e-10) {
  113. ret[circleids[i]].push(circleids[j]);
  114. }
  115. }
  116. }
  117. return ret;
  118. }
  119. function computeTextCentres(circles, areas) {
  120. var ret = {}, overlapped = getOverlappingCircles(circles);
  121. for (var i = 0; i < areas.length; ++i) {
  122. var area = areas[i].sets, areaids = {}, exclude = {};
  123. for (var j = 0; j < area.length; ++j) {
  124. areaids[area[j]] = true;
  125. var overlaps = overlapped[area[j]];
  126. // keep track of any circles that overlap this area,
  127. // and don't consider for purposes of computing the text
  128. // centre
  129. for (var k = 0; k < overlaps.length; ++k) {
  130. exclude[overlaps[k]] = true;
  131. }
  132. }
  133. var interior = [], exterior = [];
  134. for (var setid in circles) {
  135. if (setid in areaids) {
  136. interior.push(circles[setid]);
  137. }
  138. else if (!(setid in exclude)) {
  139. exterior.push(circles[setid]);
  140. }
  141. }
  142. var centre = computeTextCentre(interior, exterior);
  143. ret[area] = centre;
  144. if (centre.disjoint && areas[i].size > 0) {
  145. console.log('WARNING: area ' + area + ' not represented on screen');
  146. }
  147. }
  148. return ret;
  149. }
  150. exports.computeTextCentres = computeTextCentres;
  151. /**
  152. * 根据圆心(x, y) 半径 r 返回圆的绘制 path
  153. * @param x 圆心点 x
  154. * @param y 圆心点 y
  155. * @param r 圆的半径
  156. * @returns 圆的 path
  157. */
  158. function circlePath(x, y, r) {
  159. var ret = [];
  160. // ret.push('\nM', x, y);
  161. // ret.push('\nm', -r, 0);
  162. // ret.push('\na', r, r, 0, 1, 0, r * 2, 0);
  163. // ret.push('\na', r, r, 0, 1, 0, -r * 2, 0);
  164. var x0 = x - r;
  165. var y0 = y;
  166. ret.push('M', x0, y0);
  167. ret.push('A', r, r, 0, 1, 0, x0 + 2 * r, y0);
  168. ret.push('A', r, r, 0, 1, 0, x0, y0);
  169. return ret.join(' ');
  170. }
  171. exports.circlePath = circlePath;
  172. // inverse of the circlePath function, returns a circle object from an svg path
  173. function circleFromPath(path) {
  174. var tokens = path.split(' ');
  175. return { x: parseFloat(tokens[1]), y: parseFloat(tokens[2]), radius: -parseFloat(tokens[4]) };
  176. }
  177. exports.circleFromPath = circleFromPath;
  178. /** returns a svg path of the intersection area of a bunch of circles */
  179. function intersectionAreaPath(circles) {
  180. var stats = {};
  181. (0, circleintersection_1.intersectionArea)(circles, stats);
  182. var arcs = stats.arcs;
  183. if (arcs.length === 0) {
  184. return 'M 0 0';
  185. }
  186. else if (arcs.length == 1) {
  187. var circle = arcs[0].circle;
  188. return circlePath(circle.x, circle.y, circle.radius);
  189. }
  190. else {
  191. // draw path around arcs
  192. var ret = ['\nM', arcs[0].p2.x, arcs[0].p2.y];
  193. for (var i = 0; i < arcs.length; ++i) {
  194. var arc = arcs[i], r = arc.circle.radius, wide = arc.width > r;
  195. ret.push('\nA', r, r, 0, wide ? 1 : 0, 1, arc.p1.x, arc.p1.y);
  196. }
  197. return ret.join(' ');
  198. }
  199. }
  200. exports.intersectionAreaPath = intersectionAreaPath;
  201. //# sourceMappingURL=diagram.js.map