Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2012-12-06 08:53:53

White_Owl
Member
Registered: 2010-03-03
Posts: 106

Distance to the wall of the box

The problem:
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?

Offline

#2 2012-12-06 18:23:53

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: Distance to the wall of the box

Hi my friend.  If you are allowed to use the absolute value operator, I do believe an elegant solution will arrive.  For starters, let's couple together the dbottom with the dtop in an expression to find the one that is positive.  And also let's couple together the dleft with the dright in an expression to find the one that is positive meaning non-negative.  In these two cases, we simply need a "maximum finder" equation that uses the absolute value signs.  After these two terms are resolved, they can be embedded in the larger single equation in which the "minimum of two values" is found, and that is the answer.  Now let me give you an idea as to how one would find the max value of two numbers with the absolute value sign.  See next post for more details...


igloo myrtilles fourmis

Offline

#3 2012-12-06 18:29:47

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: Distance to the wall of the box

max value of a and b = (a + b + |a - b|) / 2

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...


igloo myrtilles fourmis

Offline

#4 2012-12-06 19:11:58

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: Distance to the wall of the box

Here's an awkward way to avoid absolute value signs:

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

Basically square root the square of the interested value.


igloo myrtilles fourmis

Offline

#5 2012-12-07 05:42:15

White_Owl
Member
Registered: 2010-03-03
Posts: 106

Re: Distance to the wall of the box

John E. Franklin wrote:

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.

Offline

Board footer

Powered by FluxBB