Skip to content

Commit 8837d37

Browse files
committed
use lookup tables
1 parent ca1668c commit 8837d37

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

hybrid-programming/example4/java/src/main/java/py5utils/Py5Utilities.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)