I'm creating a game for kids. It's creating a triangle using 3 lines. How I approached this is I create two arcs(semi circle) from two end points of a base line. But I couldn't figure how to find the point of intersection of those two arc. I've search about it but only found point of intersection between two straight lines. Is there any method to find this point of intersection? Below is the figure of two arcs drawn from each end of the baseline.
Assume centers of the circle are (x1
, y1
) and (x2
, y2
), radii are R1
and R2
. Let the ends of the base be A
and B
and the target point be T
. We know that AT
= R1
and BT
= R2
. IMHO the simplest trick to find T
is to notice that difference of the squares of the distances is a known constant (R1^2 - R2^2)
. And it is easy to see that the line the contains points meeting this condition is actually a straight line perpendicular to the base. Circles equations:
(x - x1)^2 + (y-y1)^2 = R1^2
(x - x2)^2 + (y-y2)^2 = R2^2
If we subtract one from another we'll get:
(x2 - x1)(2*x - x1 - x2) + (y2 - y1)(2*y - y1 - y2) = R1^2 - R2^2
Let's x0 = (x1 + x2)/2
and y0 = (y1 + y2)/2
- the coordinates of the center. Let also the length of the base be L
and its projections dx = x2 - x1
and dy = y2 - y1
(i.e. L^2 = dx^2 + dy^2
). And let's Q = R1^2 - R2^2
So we can see that
2 * (dx * (x-x0) + dy*(y-y0)) = Q
So the line for all (x,y)
pairs with R1^2 - R2^2
= Q
= const is a straight line orthogonal to the base (because coefficients are exactly dx
and dy
).
Let's find the point C
on the base that is the intersection with that line. It is easy - it splits the base so that difference of the squares of the lengths is Q
. It is easy to find out that it is the point on a distance L/2 + Q/(2*L)
from A
and L/2 - Q/(2*L)
from B
. So now we can find that
TC^2 = R1^2 - (L/2 + Q/(2*L))^2
Substituting back Q
and simplifying a bit we can find that
TC^2 = (2*L^2*R1^2 + 2*L^2*R2^2 + 2*R1^2*R2^2 - L^4 - R1^4 - R2^4) / (4*L^2)
So let's
a = (R1^2 - R2^2)/(2*L)
b = sqrt(2*L^2*R1^2 + 2*L^2*R2^2 + 2*R1^2*R2^2 - L^4 - R1^4 - R2^4) / (2*L)
Note that formula for b
can also be written in a different form:
b = sqrt[(R1+R2+L)*(-R1+R2+L)*(R1-R2+L)*(R1+R2-L)] / (2*L)
which looks quite similar to the Heron's formula. And this is not a surprise because b
is effectively the length of the height to the base AB
from T
in the triangle ABT
so its length is 2*S/L
where S
is the area of the triangle. And the triangle ABT
obviously has sides of lengths L
, R1
and R2
respectively.
To find the target T
we need to move a
along the base and b
in a perpendicular direction. So coordinates of T
calculated from the middle of the segment are:
Xt = x0 + a * dx/L ± b * dy / L
Yt = y0 + a * dy/L ± b * dx / L
Here ±
means that there are two solutions: one on either side of the base line.
Partial case: if R1 = R2 = R
, then a = 0
and b = sqrt(R^2 - (L/2)^2)
which makes obvious sense: T
lies on the segment bisector on a length of sqrt(R^2 - (L/2)^2)
from the middle of the segment.
Hope this helps.