# How to place rectangle so it envelopes two circles, one circle in the very right end of the rectangle and one in the other end.

How can i place a rectangle so it envelopes two circles, one circle in the very right end of the rectangle and one in the other end.

Say i have two circles with the xy coordinates and a radius:

c1.x = 10
c1.y = 3

c2.x = 5
c2.y = 10


So how can i find the coordinate of the rectangles origin (top left corner), its required rotation and its width and height so it will envelop both circles as depicted?

-
The two circles always have the same radius? –  Guess who it is. Jul 16 '12 at 17:44
I would like a solution that can handle varying radius, however it's not a requirement. –  netbrain Jul 16 '12 at 17:46
At least, the required rotation is easy; you can get the angle as the arctangent of the slope of the line joining the centers of your two circles. –  Guess who it is. Jul 16 '12 at 17:47
@Inquest unless you're okay with sticking a circle in one corner (instead of taking up a whole side), they need to have the same radius to form a rectangle at all. If the radii are different then an answer won't be unique, and you'll have to specify something extra. –  Robert Mastragostino Jul 16 '12 at 18:10

Calculate the middle point of the segment joining both centers: $$M=\left(\frac{10+5}{2}\,,\,\frac{10+3}{2}\right)=\left(\frac{15}{2}\,,\,\frac{13}{2}\right)$$

The point $\,M\,$ is the point of intersection of the rectangle-to-be's diagonals (can you see why?).

Now the distance between two centers: $$d=\sqrt{(10-5)^2+(3-10)^2}=\sqrt{74}$$ so the rectangle's long side is of length $\,\sqrt {74}+10\,$ and its width is, of course, $\,10\,$.

Can you take it from here? For example, both long sides of the rectangle are, of course, parallel but also parallel with the segment joining the circles' lengths. This last has slope equal to $$m=\frac{10-3}{5-10}=-\frac{7}{5}...$$

Added as answer to the OP: Point of origin?? You only need to find the equation of the line through, say the upper circle's center, and which is perpendicular to both long sides of the rectangle and then find both points on it at a distance of $\,5\,$ from the circle's center. These two points are the intersection points of the rectangle's long sides with that circle, and now you can easily find the red point on the rectangle by finding the points 5 units away from these intersection points and on the upper long side of the rectangle..

Another way: Draw the triangle between both upper red points and the intersection point of the circle with the upper long side of the rectangle. You get an isosceles straight-angle triangle with legs with lengths 5 (why?), so the red point on the rectangle is on the line forming an angle of $\,45^\circ\,$ with the long side of the rectangle (or with the radius you found above) and at distance of $\,5\sqrt 2\,$ (Pythagoras is our friend here) from the circle's center...I hope you're doing the maths and the drawings as you read this since it is waaaaaaayy harder to write it down than to understand it: it aall is very basic geometry + basic analytic geometry.

-
Just to clarify: The Midpoint method will work only if the circles are of equal radius is that correct? –  Inquest Jul 16 '12 at 18:00
Well, if the circles are not of equal radius then the problem, as posed by the OP, is not solvable to begin with...Note that as he asked and drew, the rectangle's side must be tangent to the circles. –  DonAntonio Jul 16 '12 at 18:14
Looks great, but how can find the rectangles point of origin? –  netbrain Jul 16 '12 at 18:19
1) First, you must find out the slope of the line connecting the two centres. $m = \dfrac{c1.y-c2.y}{c1.x-c2.x}$
2) The slope of the line perp to this line will have slope $\dfrac{-1}{m}$ (Be careful if m==0)
3) If $\theta = \arctan |m|, \delta(x) = c.radius \times \cos(\theta), \delta(y) = c.radius \times \sin(\theta)$ This is the distance from the centre of the circle to the point where the side of the rectangle will (tangentially) touch the circle.
You can generate the cosine and sine corresponding to the slope without having to use trigonometric functions; if $m=\tan\,\theta$, then $\sin\,\theta=\dfrac{m}{\sqrt{1+m^2}}$ and $\cos\,\theta=\dfrac1{\sqrt{1+m^2}}$. –  Guess who it is. Jul 16 '12 at 18:01