Get distance from XY position unto the nearest point of a square

DRAUXEN

New member
Joined
Sep 18, 2018
Messages
3
(Sorry if I'm posting in the wrong section and my bad english)

So, I'm wondering what formula can I use to calculate the distance that there's from a XY position unto the nearest point (edge) of a square.

7cf0adf867664fe32e930d77fb0b4b97o.png


I have all the needed info: XY (red/green) points position, square's position and it's radius (size/2), I'm just needing the formula.
Regards.
 
Last edited:
(Sorry if I'm posting in the wrong section and my bad english)

So, I'm wondering what formula can I use to calculate the distance that there's from a XY position unto the nearest point (edge) of a square.




I have all the needed info: XY (red/green) points position, square's position and it's radius (size/2), I'm just needing the formula.
Regards.

Can it be inside?

I would extend all four sides of the square, defining 8 external regions. this gives only three things to do, depending on which region you are in.
1) Side regions (or on the boundary) - Horizontal is easy.
2) Top or bottom regions (or on the boundary) - vertical is easy.
3) Distance formula poses little additional difficulty.
 
Can it be inside?

I would extend all four sides of the square, defining 8 external regions. this gives only three things to do, depending on which region you are in.
1) Side regions (or on the boundary) - Horizontal is easy.
2) Top or bottom regions (or on the boundary) - vertical is easy.
3) Distance formula poses little additional difficulty.

Outside only.


I thought that there's was a formula to do it all at once, I means, without checking in what side is the point.
In that way, I think I have it, thanks you!
 
(Sorry if I'm posting in the wrong section and my bad english)

So, I'm wondering what formula can I use to calculate the distance that there's from a XY position unto the nearest point (edge) of a square.




I have all the needed info: XY (red/green) points position, square's position and it's radius (size/2), I'm just needing the formula.
Regards.


distance=[x2+y2]1/2
 
Top