Results 1 to 13 of 13

Thread: [RESOLVED] Smallest dimension between two objects

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [RESOLVED] Smallest dimension between two objects

    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.

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    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)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Smallest dimension between two objects

    Hi krtxmrtz,

    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.

    Hope you can help.
    Attached Images Attached Images  
    Last edited by sgrya1; Jan 9th, 2007 at 08:33 AM.

  4. #4
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    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)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Smallest dimension between two objects

    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.

  6. #6
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    At any rate, for case 1:

    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)

  7. #7
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    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)

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Smallest dimension between two objects

    OK. That's pretty much what I expected.

    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.

  9. #9
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    Quote Originally Posted by sgrya1
    OK. That's pretty much what I expected.

    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)

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Smallest dimension between two objects

    Thanks krtxmrtx,

    Think I got it, maybe just not reordered as you have it?

    D=sqrt((x1-w1/2)2+(y1-y1.w1/(2.x1)2)-D/2

    I guess that this is an approximation because of the following for example?
    Attached Images Attached Images  

  11. #11
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    Quote Originally Posted by sgrya1
    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)

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Smallest dimension between two objects

    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.

  13. #13
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Smallest dimension between two objects

    Quote Originally Posted by sgrya1
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width