# CS 248: Midterm Solutions

## Question 2

There were three things to figure out on this question.

First, what face does the ray intersect? There are many ways to get the correct face. By far the simplest way is to simply take the component of the direction vector with the largest magnitude, and that will narrow it down to two planes, and you can then take the sign of that component to distinguish between the two planes.

Really what is going on here is you're finding the face that forms the smallest angle between the direction vector and the normal to the face (i.e., finding the maximum dot product). However, since the normals to the faces are all unit vectors with two of their components equal to zero, these two methods are equivalent.

A second method is to intersect the ray with all six infinite planes forming the cube by substituting the plane equation into the appropriate part of the parametric representation of the vector, and pick the plane that gives an intersection point lying within the boundaries of the cube face. Some care must be taken to make sure you don't intersect two faces of the cube (treating the direction vector as a line instead of a ray), but this method works out just fine.

A third (very complex) method is as follows. For each face of the cube, project the direction vector onto the other two perpendicular planes at 0. For instance, for the z=1 plane, project onto the x=0 and y=0 planes. Now take the angle between the two projected vectors and the normal to the plane in question. If they are both between 0 and 45 degrees, we have our plane.

There were two very common wrong answers to this part of the question. One wrong answer simply said to normalize the vector and take its dot product with each of the six normals. Choose the one that's less than 45 degrees, and there's your face. The problem is that this method does not work near the corners of the cube, since if you draw a vector from the center of the cube to one of the corners, it forms an angle of more than 45 degrees with ALL SIX normals. An easy way to see why this doesn't work is to realize that what you're doing is essentially the spotlight test from assignment 4, and it's pretty clear that you can't completely cover the cube with six 45 degree spotlights positioned at the center.

Another wrong way to do this was to make the length of the vector equal to sqrt(3) (the length of a half-diagonal of the cube), and then simply use the component whose magnitude was greater than 1 to pick the side. This is almost the right idea, but there are certainly vectors of length sqrt(3) with more than one component greater than 1. This method doesn't work near the edges of the cube.

This part of the question was worth 6 points.

Now that we have the correct face, what are the coordinates of intersection? If you used the second method outlined above, you already have this information. If not, you simply divide each component of the vector by the magnitude of the largest component, which places the endpoint of the vector on the cube. You can then read the intersection point directly from the other two components.

It is possible to get these values using complex trigonometric properties of the problem, but it's not necessary.

This part of the question was worth 6 points.

Finally, now that we have the intersection point, how do we get the (u,v) coordinates for that texture?

This is simple: Since each component of our intersection is known to lie in the range (-1,1), we simply add 1 to it, and divide by 2 to get a value in the range (0,1).

This part of the question was worth 3 points.