Reputation: 4934
I'm trying to figure out how to get the distance from two circles relative to the corners of their square container boxes. I need some help with the maths here.
How can I work out the number of pixels for the line marked with a question mark?
Appreciate the help as always.
Upvotes: 10
Views: 38199
Reputation: 6029
tldr: Calculate the distance between each circles center point, then subtract the radius' of each circle from that.
For the purpose of a demonstration, we will assume the following:
r1 = 100
) circle is at the (x, y) coordinates of (0, 0)
, andr2 = 50
) circle is at (x, y) coordinates of (150, -150)
.Given that the distance between their centers is:
To find the distance between their boundaries, we subtract the radius of each circle from the distance between their centers.
This leaves us with the equation:
sqrt((x2 − x1)^2 + (y2 − y1)^2) − (r2 + r1)
Inserting your values into the above gives:
sqrt((150 − 0)^2 + (-150 − 0)^2) − (100 + 50) = 62.132034356px
Upvotes: 15
Reputation: 189646
The algebraically simplified version of Daniel's answer is
(r1 + r2) * (sqrt(2) - 1)
= (s1 + s2) * (sqrt(2) - 1)/2
where r1,r2 are the two radii and s1,s2 are the two square sides. This is easily seen by looking at each square individually and noticing that the distance d1 from the circle/square center to the square corner is sqrt(2) * r, and the desired distance within that square is d1 - the circle radius r.
Upvotes: 2
Reputation: 1242
Do you have the middle point of each circles? If you do, first calculate the distance from the centre of circles.
distance² = center1² + center2²
Then, you will need to minus the radius of both circles. In your case, it will be 150 (100 + 50)
Upvotes: 3
Reputation: 210427
Let's see... each radius is half each side length, and subtracting the sum of the radii from the distance between the center gives you the amount that's left.
Hope that helps?
Upvotes: 2