In this chapter, you will develop algorithms that will tell you where lines and planes intersect a variety of objects. The techniques you develop will be useful later when you remove hidden lines and trace shadows cast by objects. You will also learn how to show the intersection of lines and planes with a sphere. As you will see, there is no one magic algorithm that will satisfy all situations; each requires its own methodology. While you may never need some of these algorithms, such as a line intersecting a circular sector, the procedures, which rely on vector-based geometry, are interesting and should give you the tools you will need when you encounter different situations.
Instead of using vectors, many of these solutions could be derived analytically. For example, the solution for a line intersecting a sphere can be obtained by combining the equation of a line with that of a sphere. The result is a quadratic equation that, when solved, yields the entrance and exit points. Such an approach can be fast and simple provided you are dealing with objects that can be represented by simple equations. However, the vector-based procedures, while they may seem more complex, are actually quite simple and intuitive. They can also be much more versatile and adaptable to unusual situations. They are the ones you will use here.
5.1 Line Intersecting a Rectangular Plane
The plane has corners at 0, 1, 2, and 3. These have local coordinates of (x0,y0,z0) - (x3,y3,z3) relative to the center of rotation at (xc,yc,zc). The line starts at x[4],y[4],z[4] and ends at x[5],y[5],z[5]. It intersects the plane at the hit point.
There are three unit vectors at corner 0; û,, and . Unit vector points from corner 0 to 1; û from 0 to 3. is normal to the plane. is a unit vector pointing along the line from 4 to 5. Q45 is the distance from 4 to 5. Q h is the distance from 4 to the hit point. Q n is the perpendicular distance from 4 to the plane. Your quest is to determine the location of the hit point (xh,yh,zh). Using vector geometry, you can write the following relations:
You can test to see if the hit point lies within the boundaries of the plane. Figure 5-2 shows the geometry. Vector V0h runs from corner 0 to the hit point h. up and vp are the projections of V0h on the 03 and 01 directions, respectively. To test for an in-bound or out-of-bound hit,
if up < 0 or up > Q03 hit is out of bounds
if vp < 0 or vp > Q01 hit is out of bounds
All of this has been incorporated in Listing 5-1, which has the same structure as Listing 3-5 in Chapter 3, although some of the functions and operations have been altered. As in that program, rotation directions and amounts are entered through the keyboard. Rotations are additive; for example, if the system is rotated first by Rx=40 degrees, followed by Rx=10, the total angle will be 50 degrees. Ry and Rz operate similarly.
Some data is hard-wired in Listing 5-1, such as definitions of the rectangular plane and the line intersecting it. They are shown in the lists in lines 18–20. There are six elements in each list numbered [0]-[5]: [0]-[3] are the four corners of the plane while [4] and [5] are the beginning and end of the line. They are coordinated with the diagrams in Figures 5-1 and 5-2. To modify the plane and line, just put new numbers in the lists. For example, item [5] is the end of the line. To drop it down in the +y direction, increase y[5]. The numbers in the lists are in local coordinates relative to the center of rotation (xc,yc,zc), which is at the center of the plane. The values are shown in Lines 14-16.
It takes only three points to define a plane. Here you have a four-corner rectangular plane. If you alter the plane’s corner coordinates, be sure they lie in the same plane. The easiest way to do so is to start off with a plane that lies in or is parallel to one of the coordinate planes. It can be rotated out of that coordinate plane later. In line 19, the first four elements of the y list are all zero. That describes a flat plane parallel to the x,z plane at y=0. Also, if altering the [x] or [z] lists, be sure the plane remains rectangular since the calculations of the hit point in this analysis assume that is the case.
Rotation functions rotx, roty, and rotz, which rotate coordinates around the coordinate directions, are included in lines 28-35. They are the same as used in prior programs so they have not been listed.
Line 45 plots a dot at the hit point (xhg,yhg) where the line intersects the plane. If the hit point lies within the plane’s boundaries, the color of the dot is red; if it’s outside, it is blue. If the line from [4] to [5] is too short and never reaches the plane, the color is changed to green and a dot is placed at [5], the end of the line. This is illustrated in Figure 5-5. The calculation of the hit point is carried out by function hitpoint(x,y,z), which begins in line 53. The program follows the analysis above in Equations 5-1 through 5-49 and should be self-explanatory.
Program LRP
5.2 Line Intersecting a Triangular Plane
Almost any flat surface can be formed by an array of triangular planes and a curved surface can be approximated by triangles, hence our interest in triangular planes .
Figure 5-6 shows the geometry for a line intersecting a triangular plane. The algorithms used in Listing 5-3 are mostly the same as in Listing 5-1. One difference is that the lengths of the list are, of course, shorter since the triangle has one less corner. Another is that the check on whether the hit point lies within the triangle or is out of bounds is different.
This relation is put to use in Listing 5-2 and later in Listing 5-3. In Listing 5-2, most of the program is concerned with evaluating the lengths of the lines shown in Figure 5-7. Heron’s formula is then used to calculate the three areas: A, A1, and A2. The decision whether the hit point is inside or outside of the base triangle is made in lines 114-117 of Listing 5-2. It produces Figure 5-8. Program THT2 (not shown) is the same as THT1 (Listing 5-2) but has the lists adjusted to put the hit point within the triangle. It produces Figure 5-9. The adjusted lists are
x=[40,30,80,55]
y=[60,10,60,45]
z=[0,0,0,0]
Program THT1
However, with a general non-right triangle, the angle is not 90 ° so the vector resulting from the cross product, while normal to the plane, does not have a value of 1; in other words, it is not a unit vector. The algorithm between lines 88 and 91 makes the correction by normalizing ’s components. It does this by dividing each of them by the magnitude of . In line 88, magn is the magnitude of before normalization of the vector’s components. Depending on the angle α, its value will be somewhere between 0 and 1. Dividing each component of by magn makes a unit vector.
Program LTP
5.3 Line Intersecting a Circle
The determination of whether the hit point of a line intersecting the plane of a circle is within the circle is trivial. As shown in Figure 5-13, if the distance from the circle’s center to the hit point is greater than the circle’s radius, it lies outside the circle:
We won’t bother writing a separate program to demonstrate this. You should be able to do that yourself by modifying Listing 5-1 or Listing 5-3. Simply fill the x[ ],y[ ], and z[ ] lists with the points defining the circle’s perimeter and the line coordinates and modify the functions plotsystem and hitpoint.
5.4 Line Intersecting a Circular Sector
There are five unit vectors at point 0: û points from 0 to 2; points from 0 to 1; and ĥ from 0 to the hit point at 3. is a unit vector normal to the plane of the sector. It is not shown since it points up and out of the plane. is the result of the cross product of û with ; is from the cross product of with .
Your strategy is to first determine if Rh>r, in which case the hit point is outside the sector in the radial direction. Then you take the dot product of ĥ with . If the result is positive, the hit point is outside the sector on the 0-2 side. Then you take the dot product of ĥ with . If it is positive, the hit point is out of bounds on the 0-1 side.
Program LCSTEST
5.5 Line Intersecting a Sphere
To find the entrance hit point, you start at B and move a point p incrementally along the line toward E. At each step, you calculate Qpc, the distance between p and c. If it is less than or equal to the sphere’s radius rs, you have made contact with the sphere and a red dot is plotted. You continue moving p along the line inside the sphere without plotting anything (you could plot a dotted line), calculating Qpc as you go, until Qpc becomes equal to or greater than rs. At that point, p leaves the sphere and another red dot is plotted. p continues moving along the line to E, plotting black dots along the way. Instead of plotting the line with dots, you could have used short line segments as was done in prior programs.
They are set in lines 30-32. ϕ is the angle around the z direction. It runs from -90 ° to +90 ° . You don’t need the back half of the longitudes so they are not plotted. This half circle will be rotated around the y direction to create the oval longitudes. They are 10 ° apart as set in line 74. Since they are rotated around the y direction only, the program contains just the rotation function roty: rotx and rotz are not needed in this model. Plotting of the longitudes takes place in lines 72-77.
This is calculated in line 89 of the program. When viewed from the front, the latitude appears as a straight line since you are not rotating the sphere in this program.
Program LS
5.6 Plane Intersecting a Sphere
In this section, you will work out a technique for plotting a flat rectangular plane intersecting a sphere. Figure 5-23 shows the output of Listing 5-6; Figure 5-24 shows the model used by that listing.
Incrementing down and across the plane with parameters t and s allows you to sweep across the surface of the plane. At each point p you calculate the distance from p to the center of the sphere. If it is equal to or less than the sphere’s radius, you have a hit.
Program PS
5.7 Summary
In this chapter, you learned how to predict whether a three-dimensional line or plane will intersect a three-dimensional surface or solid object. Why bother with this? Because it is fundamental to removing hidden lines, you will see in Chapter 6. When plotting surface A, which may be behind another surface or object B, you do so in small steps, plotting a scatter dot (or a short line segment) at each step. If the point on A is hidden by B, you do not plot it. To determine if it is hidden from view by an observer, you draw an imaginary line from the point on A to the observer (i.e. in the -z direction). If you can determine if that line from A intersects a surface or object B in front of it, then you will know whether or not it is hidden. While you cannot develop hidden line algorithms for every conceivable situation (you did rectangular planes, triangular planes, circular sectors, circles, and spheres here), by understanding how it is done for these objects you should, with a bit of creativity, be able to develop your own hidden line algorithms for other surfaces and objects. Perhaps the line-triangular plane is most useful since complex surfaces and objects can often by approximated by an assembly of triangles. You will see more about this in Chapter 6.