<< Chapter < Page | Chapter >> Page > |
Listing 16 . Three visual manifestations of the same vector. |
---|
g2Db.setColor(Color.GREEN);
vecB.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix(vecA.getData(0),vecA.getData(1))));
vecB.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix(vecA.getData(0)-10,vecA.getData(1)+15)));
vecB.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix(vecA.getData(0)+10,vecA.getData(1)-60))); |
This is a perfectly legitimate thing to do since the underlying data encapsulated in the Vector object does not contain any information relating to the position of the visual manifestation.
In one visual manifestation, Listing 16 causes the tail of vecB to coincide with the head of vecA . This is one of those cases where drawing the vector in one position is preferable to drawing it in a differentposition. Later on when we get into the topic of vector addition, I will explain that one way to perform graphical addition of vectors is to position thevectors in a tail-to-head relationship as indicated by the red and green vectors in Figure 1 . The sum of the vectors can then be determined graphically by drawing a line from the tail of the first vector to the head of the last vectoras indicated by the blue line in Figure 1 . The length of the sum (or resultant) vector is indicated by the length of that line, and the direction of the resultant vector is indicated by the orientation ofthat line.
A practical example
This is the solution to a classical problem in a freshman engineering class. Assume that a boat is traveling diagonally across a river with a strong current.Assume that the green vector in Figure 1 represents the speed and direction of the current in the river. Assume that the red vector represents the speedand direction that the boat would travel if there were no current. Because of the effect of the current on the boat, the actual speed and direction of theboat will be given by the blue vector, which is different from the speed and direction indicated by the red vector. If the blue vector is pointing atthe desired landing point on the other side of the river, everything is okay. Otherwise, the captain needs to change the speed and/or the direction of theboat to compensate for the effect of the current on the boat.
Draw the blue vector
Listing 17 produces the blue visual manifestation of the Vector object referred to by vecC as shown in Figure 1 .
Listing 17 . Draw the blue vector. |
---|
g2Db.setColor(Color.BLUE);
vecC.draw(g2Db,new GM2D02.Point(new GM2D02.ColMatrix(0,0)));
}//end drawOffscreen |
Listing 17 also signals the end of the method named drawOffscreen .
The MyCanvas class and the overridden paint method
Listing 18 shows the entire inner class named MyCanvas including the overridden paint method belonging to that class.
Listing 18 . The MyCanvas class and the overridden paint method. |
---|
//This is an inner class of the GUI class.
class MyCanvas extends Canvas{public void paint(Graphics g){
g.drawImage(osiA,0,0,this);g.drawImage(osiB,this.getWidth()/2,0,this);
}//end overridden paint()}//end inner class MyCanvas}//end class GUI |
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?