I'm trying to construct a line segment between two circles. Given each radius and x, y center of each circle, how can I find the endpoints for the blue line segment?
Answer
Let the centres of the two circles be (x1,y1) and (x2,y2), where either x1<x2 or x1=x2 and y1<y2, and radii r1, r2.
Suppose x1=x2. In this case, the blue line segment is vertical, and its endpoints can easily be seen to be (x1,y1+r1) and (x2,y2−r2)=(x1,y2−r2).
Now suppose x1<x2. By shifting horizontally and vertically, we can assume the two circles are centred at (0,0) and (a,b) a=x2−x1 and b=y2−y1. The slope of the line passing through the two centres is b/a; note, a≠0 as x1≠x2. As the y-intercept of the line is 0, the equation of the line which passes through them is y=bax; note that the endpoints of the blue line segment lie on this line.
As can be see from the right-angled triangle in the image below, sinθ=b√a2+b2, cosθ=a√a2+b2, and tanθ=ba where θ is the angle the line y=bax makes with the positive x-axis.
In order to determine the first endpoint of the blue line segment, consider the following image.
The coordinates of the first endpoint are (a1,b1); we just need to determine what a1 and b1 are. Note that R1=r1, so using the small right-angled triangle, we see that cosθ=a1r1 so a1=r1cosθ; likewise, b1=r1sinθ.
In order to determine the second endpoint of the blue line segment, consider the following image.
The coordinates of the second endpoint are (a2,b2); we just need to determine what a2 and b2 are. Note that R2=√a2+b2−r2, so using the smaller of the two right-angled triangles, we get the following expressions for a2 and b2 as we did above: a2=(√a2+b2−r2)cosθ, b2=(√a2+b2−r2)sinθ.
Combining the information deduced from the three images above, we see that, after a little bit of algebra, the endpoints of the blue segment are
(ar1√a2+b2,br1√a2+b2) and (a(1−r2√a2+b2),b(1−r2√a2+b2)).
We can undo the original horizontal and vertical shifts we did at the beginning and well as express a and b in terms of the original data to obtain the endpoints
(x1+(x2−x1)r1√(x2−x1)2+(y2−y1)2,y1+(y2−y1)r1√(x2−x1)2+(y2−y1)2)
and
(x2−(x2−x1)r2√(x2−x1)2+(y2−y1)2,y2−(y2−y1)r2√(x2−x1)2+(y2−y1)2).
Note, these formulae give the correct endpoints even in the case where x1=x2.
No comments:
Post a Comment