@@ -16,17 +16,24 @@ public Py5Utilities(Sketch sketch) {
1616 public PShape createTorus (float radius , float tube , int radialSegments , int tubularSegments ) {
1717 // Torus geometry algorithm adapted from Three.js (https://threejs.org/).
1818 // https://github.com/mrdoob/three.js/blob/dev/src/geometries/TorusGeometry.js
19+
20+ // create lookup tables
21+ float thetaCosLUT [] = new float [tubularSegments + 1 ];
22+ float thetaSinLUT [] = new float [tubularSegments + 1 ];
23+ for (int j = 0 ; j <= tubularSegments ; j ++) {
24+ thetaCosLUT [j ] = Sketch .cos (j * Sketch .TWO_PI / tubularSegments );
25+ thetaSinLUT [j ] = Sketch .sin (j * Sketch .TWO_PI / tubularSegments );
26+ }
27+
1928 float vertices [][] = new float [(radialSegments + 1 ) * (tubularSegments + 1 )][3 ];
2029 for (int i = 0 ; i <= radialSegments ; i ++) {
2130 float phi = i * Sketch .TWO_PI / radialSegments ;
2231 float sin_phi = Sketch .sin (phi );
2332 float cos_phi = Sketch .cos (phi );
2433
2534 for (int j = 0 ; j <= tubularSegments ; j ++) {
26- float theta = j * Sketch .TWO_PI / tubularSegments ;
27-
28- vertices [i * (tubularSegments + 1 ) + j ][0 ] = (radius + tube * cos_phi ) * Sketch .cos (theta );
29- vertices [i * (tubularSegments + 1 ) + j ][1 ] = (radius + tube * cos_phi ) * Sketch .sin (theta );
35+ vertices [i * (tubularSegments + 1 ) + j ][0 ] = (radius + tube * cos_phi ) * thetaCosLUT [j ];
36+ vertices [i * (tubularSegments + 1 ) + j ][1 ] = (radius + tube * cos_phi ) * thetaSinLUT [j ];
3037 vertices [i * (tubularSegments + 1 ) + j ][2 ] = tube * sin_phi ;
3138 }
3239 }
0 commit comments