4

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

dardeshna
  • 627
  • 1
    Since your task is specifically to use a Monte-Carlo simulation, I don't think that an analytical soultion would be appropriate. - Do you know how to generate random points that are equidistributed on the surface of the unit sphere? – Hagen von Eitzen Dec 13 '14 at 23:44
  • No I don't at the moment... Thanks for the idea though, worth a quick google search. – dardeshna Dec 13 '14 at 23:45
  • E.g., generate three normally distributed coordinates and divide by the length. But there are other methods as well. – Hagen von Eitzen Dec 13 '14 at 23:48
  • 1
    There are many ways to choose random points on the surface of the unit sphere. One of the simplest is to choose $x,y,z$ between $-1$ and $1$ at random and discard if $x^2+y^2+z^2>1$ or $x^2+y^2+z^2<tolerance$ where tolerance is simply to avoid systematic noise near the "division by zero due to machine precision" point at $(0,0,0)$. Otherwise divide all three by $\sqrt{x^2+y^2+z^2}$ to normalize. – String Dec 13 '14 at 23:51
  • This was discussed in this thread among others ... – String Dec 13 '14 at 23:54
  • Thanks! The only thought I had was if it would be easier to use a spherical system... In the thread linked by String, the wolfram mathworld link had a formula for coordinates using a spherical system. Sorry, I don't know how to use mathjax yet :) – dardeshna Dec 14 '14 at 00:27
  • http://mathworld.wolfram.com/SpherePointPicking.html – dardeshna Dec 14 '14 at 00:30
  • 1
    You are correct that if you can calculate the solid angle of each face from the new viewpoint, you have an analytic solution to your problem. I don't know how to do that without calculus, however. I see two reasons why a Monte Carlo approach was suggested 1) calculus is beyond most of your class 2) you are in a programming class and if you are a hammer everything looks like a nail. – Ross Millikan Dec 14 '14 at 03:17

1 Answers1

1

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.

bubba
  • 43,483
  • 3
  • 61
  • 122
  • Thanks! It worked in conjunction with the formula to find random points on the unit sphere that was suggested earlier. 29.5% seems fairly reasonable as an answer. I really appreciate all the help! – dardeshna Dec 14 '14 at 21:59