# Get 2D points normalized distance between an axis-aligned inner & outer rect? [SOLVED]

Is there a clean way to get a points normalized distance between the closest edges of an inner and outer rect?

Here’s an example image of what I’m talking about:

The red dots are example points and the grey lines are the shortest paths between the two rects that the points lie on. The numbers are the points’ normalized distance along said line.

I’m pretty sure I can solve this by projecting the point onto the closest side of the inner and outer rect and getting the distance between the points, but that seems like a very heavy-handed solution.

[SOLVED]

For axis-aligned rectangles, calculating distance to a side can be done just by subtracting coordinates on the appropriate axis.

For example, if the inner rectangle stretches from x = 50 to x = 100, then the distance of point P to the closer vertical edge is

if (P.x < 50) return 50 - P.x;
if (P.x > 100) return P.x - 100;
else P is inside the inner box OR the closest side is definitely not one of the vertical ones

I don’t think this would handle a point being closest to a corner from the outside of a rect would it?

1 Like

I ended up figuring out a good solution using sdfs.

Using this SDF function

``````// Equation used from https://www.ronja-tutorials.com/2018/11/10/2d-sdf-basics.html
private float GetDistanceToRect(Rect rect, Vector2 point)
{
Vector2 halfSize = rect.size / 2f;
point -= rect.center;
Vector2 cwed = new Vector2(Mathf.Abs(point.x), Mathf.Abs(point.y)) - halfSize;
float outsideDistance = new Vector2(Mathf.Max(cwed.x, 0f), Mathf.Max(cwed.y, 0f)).magnitude;
float insideDistance = Mathf.Min(0f, Mathf.Max(cwed.x, cwed.y));
return outsideDistance + insideDistance;
}
``````

I could get the points distance to the inner and outer rect. Dividing the distance to the inner rect by the difference in distance to both rects you can get the normalized distance between the two rects.

``````float innerDist = GetDistanceToRect(innerRect, screenPosition);
float outerDist = GetDistanceToRect(outerRect, screenPosition);
float t = Mathf.Clamp01(innerDist / (innerDist - outerDist));
``````

t = 0 when the point is on the inner rect and t = 1 when it’s on the outer rect

Fair point. Now that you call my attention to it, your original diagram doesn’t match your description of the problem; for the diagonal line in the top-right of your diagram, you are not going to the closest point on the outer rectangle (for that, you should always go horizontally or vertically). Looks like you are instead projecting a ray from the closest point on the inner rectangle through the red dot.

Ah yea you’re right, apologies