Find part of segment between two circle centers

283 Views Asked by At

I drew the following image to help me explaining the question:

Having two circles Source and Target, I want to build an arrow like in the image. The Source has coordinates $Source(sx, sy)$ and $Target(tx, ty)$. The $(0, 0)$ point is located in the left top corner and the positive directions are right and bottom sides.

The circles have radiuses $R_1$ and $R_2$.

I need the coordinates $x_1, y_1, x_2$ and $y_2$ for the arrow from image.

What's the optimal way to calculate them?

I tried to make a system of two ecuations containing the right equation and the circle equation. This becomes really complicated...

Is there a better way? For example, knowing the distance between the center of Source and the arrow source to compute the arrow source coordinates? Analogous, for target circle.

1

There are 1 best solutions below

1
On BEST ANSWER

Here is (pseudo) code to do the calculation. The approach is to construct a unit vector $(u_x, u_y)$ in the direction from one center to the other. After you have this, things are pretty easy.

// Calculate distance from source center to target center
dx = tx - sx
dy = ty - sy
d = sqrt(dx*dx + dy*dy)

// Construct unit vector between centers
ux = dx/d
uy = dy/d

// Point on source circle
x1 = sx + R1*ux
y1 = sy + R1*uy

// Point on target circle
x2 = sx + (d - R2)*ux
y2 = sy + (d - R2)*uy