Java Reference
In-Depth Information
Figure 23. Code segment and result of DoubleHelix.java
1. int n = 360;
// number of vertices used
2. double a = 0.5;
// radius of helix
3. double i; int j;
// counting variables
4.
5. int[] strip = new int[2 + n/2];
6. strip[0] = n;
//for one of the helix
7. strip[1] = n;
//for the other helix
8. for ( int k=2; k<(2+n/2); k++ ) strip[k] = 2;
//for the lines connecting the two helix
9.
10. //there are n vertices on each helix, and n/2 lines connecting each
11. //helix, thus there are in total n+n+n vertices to be set.
12. LineStripArray lsa = new LineStripArray( (2*n + n ), GeometryArray.COORDINATES, strip );
13.
14. //for the first helix; i is in degrees. To do 720 degrees of revolution
15. for ( i=0,j=0; j<n; i=i+(720/n), j++ )
16. lsa.setCoordinate( j, new Point3d((double)a*Math.cos(i*Math.PI/180),
17.
(double)(-1.25 + (i*Math.PI/180)/5.0 ), (double)a*Math.sin(i*Math.PI/180)));
18.
19. //for the second helix
20. for ( i=0, j=0; j<n; i=i+(720/n), j++ )
21. lsa.setCoordinate( j+n, new Point3d( (double)-a*Math.cos(i*Math.PI/180),
22.
(double)(-1.25 + (i*Math.PI/180)/5.0 ), (double)-a*Math.sin(i*Math.PI/180) ) );
23.
24. //for the lines connecting the helix together
25. for ( i=0, j=0; j<n && i<720; i=i+(10*720/n) )
26. {
27. lsa.setCoordinate( j+(2*n), new Point3d((double)a*Math.cos(i*Math.PI/180),
28.
(double)(-1.25 + (i*Math.PI/180)/5.0 ), (double)a*Math.sin(i*Math.PI/180) ) );
29. j++;
30. lsa.setCoordinate( j+(2*n), new Point3d((double)-a*Math.cos(i*Math.PI/180),
31.
(double)(-1.25 + (i*Math.PI/180)/5.0 ), (double)-a*Math.sin(i*Math.PI/180) ) );
32. j++;
33. }
34.
Search WWH ::




Custom Search