Can anyone suggest a relatively simple way of finding the shortest dimension between two rectangles or a rectangle and circle?
Rectangle1
Centre = R1x,R1y
Width = W1
Height = H1
Rectangle2
Centre = R2x,R2y
Width = W2
Height = H2
Circle
origin =CX, CY
Radius =R
I'd be able to find it out myself but there'd be a page of ifs and buts. Hoping a mathematical mind might be able to rationalize it to a sensible formulae.
What do you mean exactly? The horizontal and vertical distances beteen the closest borders? The dimension of the overlapping area (it they do overlap)? ...?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
I really just need the shortest distance. Ideally this distance would become negative if the two objects overlap so that I can avoid calculations for that situation.
Attached is a drawing showing the two cases I need to check for. There's actually four cases but if I see how Case 1 or 2 is done then I could apply it to the rest.
Note that I've changed some of the variable names Diameter rather than radius. And the first object always has an centre of 0,0.
What kind of object is that centered at (0,0) in case 1 encircling the rectangle, looking liek a larger rectangle with round corners? What's it doing there?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Not much thinking about it. I sort of drew it to show myself what the perimeter length would be at any location around the fixed rectangle assuming the perimeter length was the same.
1. Make X1 and Y1 positive (use their absolute values). The distance will be the same because of symmetry.
2. Calculate the angle a between the center of the circle and the X axis:
a = arctan(Y1/X1)
3. Compare it to a0 = arctan(h/w), i.e. the angle between the line from the upper right corner of the rectangle to the origin and the X axis.
If a <= a0 then, calling D the distance:
D2 = [1 + (Y1/X1)2]*(2*X1 - w)2/4
else
D2 = [1 + (X1/Y1)2]*(2*Y1 - h)2/4
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Hold on... I've given the distance between the center of the circle and the border of the rectangle. To find the distance you need just substract the radius of the circle from it.
D = Sqr{[1 + (Y1/X1)2]*(2*X1 - w)2/4} - D/2
and
D = Sqr{[1 + (X1/Y1)2]*(2*Y1 - h)2/4} - D/2
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Extremely condense, most likely very correct and something that would have taken about 10 hours less time after debugging the ifs and buts. And I still wouldn't be all that confident.
Thank you so much but can you please show me the derivation of
D = Sqr{[1 + (Y1/X1)2]*(2*X1 - w)2/4} - D/2
so that I can understand it and have a go at the other three cases.
Even the symetry thing has helped, I was starting my way around the quadrants.
Extremely condense, most likely very correct and something that would have taken about 10 hours less time after debugging the ifs and buts. And I still wouldn't be all that confident.
Thank you so much but can you please show me the derivation of
D = Sqr{[1 + (Y1/X1)2]*(2*X1 - w)2/4} - D/2
so that I can understand it and have a go at the other three cases.
Even the symetry thing has helped, I was starting my way around the quadrants.
I've just engaged myself with a kind of demanding task so if it's not too urgent I can do it later on today. If you'd like to try on your own, what you must do is write down the equetion of the straight line going from the center of the circle to the origin and find the intercept point with the corresponding side of the rectangle (i.e., the vertical side if a<=a0 and the horizontal side otherwise). Then apply the formula for the distance between this point and the center of the circle... and don't forget to substract the radius!
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
I guess that this is an approximation because of the following for example?
Right, Y1-D/2 < h and X1 - D/2 < w have to be treated as special cases. Probably you can't get away from using a few IFs in your final code. Maybe I'll give it a try this evebning, maybe I can come up with some simplifying idea.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Got it nutted out. Thanks for the symetry idea and the logic. Made me think about it more clearly. Even though I didn't think about it too much at the time that first diagram I drew makes a lot of sense now.
Got it nutted out. Thanks for the symetry idea and the logic. Made me think about it more clearly. Even though I didn't think about it too much at the time that first diagram I drew makes a lot of sense now.
Thanks for the help again.
Great! I couldn't get at my home computer yesterday, someone else was using it all the time.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)