Shortest path between two circles, but from their circumferences

291 Views Asked by At

I have the following problem. I am working on a program, which at some point draws a graph. It is basically a collection of circles. The next part of my job would be connecting some of them with arrows. The problem is, they are not lined up, or something like that. They are placed in a shape of a circle, like this:

enter image description here

On this picture, I just took random points from those circles and started drawing arrows from and to those points. Pretty easy, but as you can see, the lines are cutting through the circles, it just doesn't look nice. What I want to achieve is the situation like in the circle s2 - the arrow comes and stops at the circumference (and it should start from s1s circumference as well).

So my question here would be: Is there a way, an algorithm, to somehow get two points from two circles that have the lowest distance between them, and the line connecting them would not go through any of those circles?

P.S Have in mind that they will probably always be set up in a shape of a circle, so maybe that fact gives me some advantage?

1

There are 1 best solutions below

0
On

Suppose your two circles have centres $(x_0, y_0)$ and $(x_1 y_1)$ and radii $r_0$ and $r_1$ respectively. The unit vector in the direction from the first centre to the second is $u = ((x_1 - x_0)/d, (y_1 - y_0)/d)$ where $d = \sqrt{(x_1 - x_0)^2 + (y_1 - y_0)^2}$. You want to draw a line from $(x_0, y_0) + r_0 v$ to $(x_1, y_1) - r_1 v$.