Distance to point beyond a rectangle

Started by
5 comments, last by OmniRa 5 years, 9 months ago

Not sure how to phrase the question, made if harder for me to search, Never quite got the answer I wanted. Hoping you can answer here :)

Sorry if this is wrong forum. I think this section is ok?

 

Basically I want the distance from the point to the rectangle edge, pointing to the rectangle centre. When looking for answers I mostly got the closest point on the rectangle which I didnt want ( i think ).

I know how to get the lines total distance, not sure where to go from there ;[

Any help appreciated. I attached a picture ;]

.distance.png.a532e379f4d58d77d8076c27df694c69.png

Advertisement

It seems like the most general solution would be to intersect the rectangle with a ray starting at the point in question and passing through the rectangle center, and compute the distance to the intersection point. Depending on the circumstances though you might be able to simplify that. (Relevant details would be things like, is the rectangle axis aligned? Can the point be in the Voronoi region of a vertex of the rectangle, or only an edge? What will the information be used for? And so on.)

Hi, I've solved your problem as shown on the images.

In case there are uploaded not in correct order, the first one is the one with half paper in white, then the other one.

I've solved part for general cases, but for the final solution I've taken some random values as you will see. Have in mind that Rx is the left upper corner x value of the rectangle and Rx+w is that x plus the rectangle width.

You just need to adapt these to your data.

Hope it is clear and undersatble and it can help you.

 

WhatsApp Image 2018-07-20 at 04.11.13.jpeg

WhatsApp Image 2018-07-20 at 04.11.13(1).jpeg

And sorry for the image quality, it is lower than on my mobile

Use two equations named Ax+By+C = 0 and find intersection for all sides then find closest one

Note that positions better to be in worldspace.

So your firat equation comes from normalized(PointBeyoundRect-rectCP);

Second through all sides second equation normalized (v[i+1] - v)

If i == 3 then i+1 must be 0 

Solve linear equation.

Then i think just closest intersection point to point outside should match but you have to check whenever intersection point lies on line

Well let’s see, assuming your box is axis aligned…….

I guess I would first take the length of the line by Pythagorean. So

X_delta = (point.x – center.x)

Y_delta = (point.y – center.y)

dist_to_center = sqrt(x_delta^2 + y_delta^2)

Then I would take the aspect ratio of the box. So:

box_ratio = box_width / box_height

So now you can use your X  and Y deltas to find the aspect ratio of your point to center

point_ratio = abs(x_delta / y_delta)

  So this gives you a ratio that you can compare with your box ratio to see which edge you want to calculate against. If your point ratio is greater than the box ratio you calculate against the left/right ends. Otherwise you calculate against the top/bottom. If you really do this, you might need to take care divide by zero somehow and just set the result to some big number.

So now you can find a factor to multiply the original length of the line by. So guess that would be something like

If (point_ratio > box_ratio) {

    factor = (box_width / 2) / abs(x_delta).

} else {

    factor = (box_height / 2) / abs(y_delta)

}

Now you can find the length of the line inside the box:

dist_internal = dist_to_center * factor;

And then:

dist_external = dist_to_center - dist_internal

Anyway I think this works, there might be a better way though and hopefully I didn’t mess up anywhere.

Thank you for the replies; Will see if this works. Many thanks ! :]

This topic is closed to new replies.

Advertisement