<< Chapter < Page | Chapter >> Page > |
Rotate the translated newPoint object around the origin
Listing 16 implements the two rotation equations shown in Figure 6 to rotate the translated newPoint object around the origin.
Listing 16 . Rotate the translated newPoint object around the origin. |
---|
tempX = newPoint.getData(0);
tempY = newPoint.getData(1);newPoint.setData(//new x coordinate
0,tempX*Math.cos(angle*Math.PI/180) -
tempY*Math.sin(angle*Math.PI/180));newPoint.setData(//new y coordinate
1,tempX*Math.sin(angle*Math.PI/180) +
tempY*Math.cos(angle*Math.PI/180)); |
Note that the rotation angle is converted from degrees to radians to make it compatible with the sin and cos functions from the standard Java Mathlibrary.
Translate the rotated newPoint object back to the anchor point
Finally, Listing 17 uses the displacement vector that was created and saved earlier to translate the rotated newPoint object back to the anchor point.
Listing 17 . Translate the rotated newPoint object back to the anchor point. |
---|
//Translate back to anchorPoint
newPoint = newPoint.addVectorToPoint(tempVec);return newPoint;}//end rotate |
Then Listing 17 returns a reference to the rotated newPoint object.
Now back to the program named StringArt02
This is a 2D version of a string art program that supports rotation in two dimensions. This program produces a 2D string art image by connectingvarious points that are equally spaced on the circumference of a circle as shown in Figure 7 .
Figure 7 Initial graphic output from the program named StringArt02.
Initial conditions
Initially, the circle is centered on the origin and there are six points on the circle connected by lines forming a hexagon. The lines that connectthe points are different colors. The radius of the circle is 50 units. The points at the vertices of the hexagon are not drawn, but the lines thatconnect the vertices are drawn. The anchor point is drawn in black, resulting in the small black circle at the origin in Figure 7 .
A graphical user interface
A GUI is provided that allows the user to specify the following items and click a Replot button to cause the drawing to change:
Changing the number of points causes the number of vertices that describe the geometric object to change. Changing the number of loops causes the numberof lines that are drawn to connect the vertices to change.
For a value of 1, each vertex is connected to the one next to it. For a value of 2, additional lines are drawn connecting every other vertex. Fora value of 3, additional lines are drawn connecting every third vertex as shown in Figure 8 .
Figure 8 Graphic output from the program named StringArt02 with Loops set to 3.
Rotating the geometric object around the origin
The geometric object can be rotated around an anchor point. Entering a non-zero value in the Rotation field causes the geometric object to be rotated by the specified angle around the anchor point.
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?