minValue(maxValue(dTop,dBottom),maxValue(dLeft,dRight))

Yes, I am allowed to use min() and max() functions, and right now I actually have an equation similar to the one you showed here. It works perfectly but I still believe there is a way to avoid individual calculations of these distances.

I think, the ray tracing algorithms are dealing with the similar problem. Do we have any experts in 2.5D graphics here? I remember reading some articles on that subject, but it was too many years ago.

Abs(X) = (x**2)**(1/2) = (x^2)^(1/2)

Basically square root the square of the interested value.

]]>minValue(C,D)= (C + D - |C - D|) / 2

Put it all together in one large equation and get...

minValue(maxValue(dTop,dBottom),maxValue(dLeft,dRight))

Now just expand this with you trigonometric equations you have

mentioned in your original post and I believe it is possible.

I hope you are allowed to use the absolute value operator though!!!!

Have a super week...

]]>We have a squared box of size w*w.

Inside the box, we have a point at (x,y).

From the point we draw a line at some angle d.

If x, y, d, and w are known, find the distance to the closest wall of the box.

x and y are always positive and less then w (point is always inside the box). The d can be anything.

On the attached image I put two points. I need to know the length of green lines.

From a simple definitions of sin and cos, I have four distances to the four walls of the box

dl = (0 - x) / cos d

db = (0 - y) / sin d

dr = (w - x) / cos d

dt = (w - y) / sin d

From the set {dl, db, dr, dt} remove negative values (those are distances along the same line but in opposite directions).

And the final answer is the minimal from two remaining values.

Looks like the final answer is correct, but solutiona is fairly complicated. Maybe I am not seeing the obvious, but is it possible to reduce this algorithm to a one formula?

]]>