| 514 | } |
| 515 | |
| 516 | function preventCollisions() { |
| 517 | var quadtree = d3.quadtree() |
| 518 | .x(function (d) { return d['x']; }) |
| 519 | .y(function (d) { return d['y']; }) |
| 520 | .addAll(graph.simulation.nodes()); |
| 521 | |
| 522 | for (var name in graph.data) { |
| 523 | var obj = graph.data[name], |
| 524 | ox1 = obj.x + obj.extent.left, |
| 525 | ox2 = obj.x + obj.extent.right, |
| 526 | oy1 = obj.y + obj.extent.top, |
| 527 | oy2 = obj.y + obj.extent.bottom; |
| 528 | |
| 529 | /* eslint-disable-next-line no-unused-vars */ |
| 530 | quadtree.visit(function (quad, x1, y1, x2, y2) { |
| 531 | if (!quad.length) { |
| 532 | do { |
| 533 | if (quad.data && quad.data !== obj) { |
| 534 | // Check if the rectangles intersect |
| 535 | var p = quad.data, |
| 536 | px1 = p.x + p.extent.left, |
| 537 | px2 = p.x + p.extent.right, |
| 538 | py1 = p.y + p.extent.top, |
| 539 | py2 = p.y + p.extent.bottom, |
| 540 | ix = (px1 <= ox2 && ox1 <= px2 && py1 <= oy2 && oy1 <= py2); |
| 541 | if (ix) { |
| 542 | var xa1 = ox2 - px1, // shift obj left , p right |
| 543 | xa2 = px2 - ox1, // shift obj right, p left |
| 544 | ya1 = oy2 - py1, // shift obj up , p down |
| 545 | ya2 = py2 - oy1, // shift obj down , p up |
| 546 | adj = Math.min(xa1, xa2, ya1, ya2); |
| 547 | |
| 548 | if (adj == xa1) { |
| 549 | obj.x -= (adj / 2) + 20; |
| 550 | if (obj.x < 0) { obj.x = 0; } |
| 551 | //if (obj.x > graph.width) { obj.x = graph.width - 20;} |
| 552 | p.x += (adj / 2) + 20; |
| 553 | //if (p.x > graph.width) { p.x = p.x - 20;} |
| 554 | } else if (adj == xa2) { |
| 555 | obj.x += (adj / 2) + 20; |
| 556 | p.x -= (adj / 2) + 20; |
| 557 | if (p.x < 0) { p.x = 0; } |
| 558 | //if (obj.x > graph.width) { obj.x = graph.width - 20;} |
| 559 | //if (p.x > graph.width) { p.x = graph.width - 20;} |
| 560 | } else if (adj == ya1) { |
| 561 | obj.y -= adj / 2; |
| 562 | if (obj.y < 0) { obj.y = 0; } |
| 563 | p.y += adj / 2; |
| 564 | } else if (adj == ya2) { |
| 565 | obj.y += adj / 2; |
| 566 | p.y -= adj / 2; |
| 567 | if (p.y < 0) { p.y = 0; } |
| 568 | } |
| 569 | } |
| 570 | return ix; |
| 571 | } |
| 572 | /* eslint-disable-next-line no-cond-assign */ |
| 573 | } while (quad = quad.next) |