<< Chapter < Page | Chapter >> Page > |
When the user clicks the OK button, the program draws the user-specified vector in black with the tail located at the origin in 3D space.It draws normalized versions of the perpendicular vectors in magenta with their tails also located at the origin. Each normalized vector is scaled by a factorof 50 before it is drawn to cause it to be long enough to be seen.
A short review
Before getting into the programming details, we need to review some material from the previous module. Recall that I did some algebraic manipulations in theprevious module and produced the equations shown in Figure 6 .
Figure 6 . A general formulation of 3D vector perpendicularity. |
---|
dot product = x1*x2 + y1*y2 + z1*z2
If the two vectors are perpendicular:x1*x2 + y1*y2 + z1*z2 = 0.0
x1*x2 = -(y1*y2 + z1*z2)x2 = -(y1*y2 + z1*z2)/x1
ory2 = -(x1*x2 + z1*z2)/y1
orz2 = -(x1*x2 + y1*y2)/z1 |
An infinite set of perpendicular vectors
The equations in Figure 6 describe an infinite set of vectors that are all perpendicular to a given vector. Given these equations, and given thecoordinates (x1, y1, and z1) of a vector for which we need to produce perpendicular vectors, we can assume values for any two of x2, y2, and z2. Wecan then determine the value for the other coordinate that will cause the new vector to be perpendicular to the given vector.
That is the procedure that is used by this program to produce three of the perpendicular vectors shown in Figure 4 . The remaining three perpendicular vectors shown in Figure 4 are produced by negating the three vectors that are created using the procedure described above.
Beginning of the actionPerformed method
The only code in this program that is new to this module is contained in the actionPerformed method. This method is called to respond to a click on the OK button in Figure 5 . Therefore, I will confine my explanation to portions of the actionPerformed method. You can view a complete listing of this program in Listing 11 near the end of the module.
The actionPerformed method begins in Listing 5 . Note that I deleted some of the code early in the method because it is very similar to code that Ihave explained before.
Listing 5 . Beginning of the actionPerformed method for the program named DotProd3D06. |
---|
public void actionPerformed(ActionEvent e){ |
Behavior of the actionPerformed method
The actionPerformed method contains three sections of code, each of which implements one of the equations in Figure 6 , to compute and draw one of the perpendicular vectors shown in Figure 4 . In addition, the code in the actionPerformed method draws the negative of those three vectors to produce the other three perpendicular vectors shown in Figure 4 .
Implement the last equation
Listing 5 implements the last equation from Figure 6 , provided that the z-axis coordinate value for the given vector is greater than 0.001. As mentionedearlier, if the z-axis coordinate value is not greater than 0.001, the code in Listing 5 is skipped and no effort is made to create and draw that particular vector. This is done to prevent an attempt to divide by a zero or near-zerovalue.
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?