Standing at the center of a cube and walking halfway to a wall - field of vision

432 Views Asked by At

In my python programming class one of the bonus problems is this:

Suppose you are located at the exact center of a cube. If you could look all around you in every direction, each wall of the cube would occupy 1/6 of your field of vision. Suppose you move toward one of the walls so that you are now half-way between it and the center of the cube. What fraction of your field of vision is now taken up by the closest wall? Hint: use a Monte Carlo simulation that repeatedly “looks” in a random direction and counts how many times it sees the wall.

I have an idea of what the program should do thanks to the hint: 1. Define the boundaries of one wall 2. "Look" in a random direction 3. Count how many times the simulation "sees" the wall

The author states that this problem can be solved with fancy "analytical geometry," so if someone could explain to me how to solve this mathematically, I could maybe go about building the program off of that. I have some ideas but they are just ideas... Mainly, define field of vision as a sphere encompassing the cube. Then in the base case at the center, each wall takes up exactly 1/6 of your field of vision. Also, something about a "solid angle" might be part of it. http://en.wikipedia.org/wiki/Solid_angle

I could be on completely the wrong track here, so any help would greatly be appreciated by me (and my teacher). Oh, and by the way, I am in 9th grade in a pre-calculus class O_O

1

There are 1 best solutions below

1
On BEST ANSWER

Suppose we set up a coordinate system whose origin is at the center of the cube, the faces of the cube are $x=\pm2$, $y=\pm2$, $z=\pm2$, and the new eye-point is at $E=(1,0,0)$.

We're going to shoot "rays" outwards from $E$, and count the fraction that hit the face $F$ where $x=2$.

Suppose we use three random numbers $u$, $v$, $w$ to determine the direction vector of a sample ray. Then the equation of the ray line is: $$ \frac{x-1}{u} = \frac{y}{v} = \frac{z}{w} $$ Or, in parametric form, $$ R(t) = (1,0,0) + t(u,v,w) \quad (0 \le t < \infty) $$ Where this line hits the plane $x=2$, we have $1 + tu = 2$, so $t = 1/u$. At the point of intersection, we have $y = tv = v/u$ and $z = tw = w/u$. This point lies on the face $F$ if $|y| \le 2$ and $|z| \le 2$. We're only interested in intersection points where $t>0$ (i.e. where $u>0$).

So, in short, the random ray in the direction $(u,v,w)$ hits the face $F$ if $u>0$, $|v| \le 2u$, and $|w| \le 2u$.

Fire a large number of these random rays, and count the percentage that satisfy these three conditions.

The (pseudo) code will look something like this

total = some very large number
hits = 0

for i = 1 to total
    u = random()
    v = random()
    w = random()
    if ( u > 0 and abs(v) < 2*u and abs(w) < 2*u ) hits = hits + 1
end

fraction = hits/total

Actually, this is a pretty poor example of the use of Monte-Carlo methods, because it can easily be solved by simple analytic geometry.