Skip to content

Commit 3037031

Browse files
committed
example 4
1 parent 9503904 commit 3037031

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import py5
2+
3+
4+
torus = None
5+
6+
def setup():
7+
global torus
8+
py5.size(500, 500, py5.P3D)
9+
py5.smooth(4)
10+
11+
torus = py5.utils.createTorus(200, 25, 20, 40)
12+
13+
py5.stroke(0)
14+
py5.stroke_weight(3)
15+
py5.fill(255)
16+
17+
18+
def draw():
19+
py5.background(128)
20+
py5.translate(py5.width / 2, py5.height / 2, 0)
21+
py5.shape(torus)
22+
23+
24+
py5.run_sketch()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>py5</groupId>
8+
<artifactId>py5-hybrid-programming-example4</artifactId>
9+
<version>0.1</version>
10+
11+
<name>py5-hybrid-programming-example4</name>
12+
<url>https://py5coding.org/</url>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<jarlocation>${env.CONDA_PREFIX}/lib/python3.10/site-packages/py5/jars</jarlocation>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>py5</groupId>
23+
<artifactId>py5-processing4</artifactId>
24+
<version>0.9.0.dev0</version>
25+
<scope>system</scope>
26+
<systemPath>${jarlocation}/core.jar</systemPath>
27+
</dependency>
28+
<dependency>
29+
<groupId>py5</groupId>
30+
<artifactId>py5-jogl</artifactId>
31+
<version>0.9.0.dev0</version>
32+
<scope>system</scope>
33+
<systemPath>${jarlocation}/jogl-all.jar</systemPath>
34+
</dependency>
35+
<dependency>
36+
<groupId>py5</groupId>
37+
<artifactId>py5</artifactId>
38+
<version>0.9.0.dev0</version>
39+
<scope>system</scope>
40+
<systemPath>${jarlocation}/py5.jar</systemPath>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-dependency-plugin</artifactId>
49+
<version>3.2.0</version>
50+
<executions>
51+
<execution>
52+
<id>copy</id>
53+
<phase>package</phase>
54+
<goals>
55+
<goal>copy</goal>
56+
</goals>
57+
</execution>
58+
</executions>
59+
<configuration>
60+
<artifactItems>
61+
<artifactItem>
62+
<groupId>py5</groupId>
63+
<artifactId>py5-hybrid-programming-example4</artifactId>
64+
<version>0.1</version>
65+
<type>jar</type>
66+
<overWrite>true</overWrite>
67+
<outputDirectory>${project.basedir}/../jars</outputDirectory>
68+
<destFileName>py5-hybrid-programming-example4.jar</destFileName>
69+
</artifactItem>
70+
</artifactItems>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package py5utils;
2+
3+
import processing.core.PShape;
4+
import py5.core.Sketch;
5+
6+
public class Py5Utilities {
7+
8+
protected Sketch sketch;
9+
10+
public Py5Utilities(Sketch sketch) {
11+
// This constructor is called before the sketch starts running. DO NOT use
12+
// Processing methods here, as they may not work correctly.
13+
this.sketch = sketch;
14+
}
15+
16+
public PShape createTorus(float radius, float tube, int radialSegments, int tubularSegments) {
17+
// Torus algorithm adapted from Three.js (https://threejs.org/).
18+
// https://github.com/mrdoob/three.js/blob/dev/src/geometries/TorusGeometry.js
19+
float vertices[][] = new float[(radialSegments + 1) * (tubularSegments + 1)][3];
20+
for (int i = 0; i <= radialSegments; i++) {
21+
for (int j = 0; j <= tubularSegments; j++) {
22+
float theta = j * Sketch.TWO_PI / tubularSegments;
23+
float phi = i * Sketch.TWO_PI / radialSegments;
24+
25+
vertices[i * (tubularSegments + 1) + j][0] = (radius + tube * Sketch.cos(phi)) * Sketch.cos(theta);
26+
vertices[i * (tubularSegments + 1) + j][1] = (radius + tube * Sketch.cos(phi)) * Sketch.sin(theta);
27+
vertices[i * (tubularSegments + 1) + j][2] = tube * Sketch.sin(phi);
28+
}
29+
}
30+
31+
PShape torus = sketch.createShape();
32+
torus.beginShape(Sketch.QUADS);
33+
for (int i = 1; i <= radialSegments; i++) {
34+
for (int j = 1; j <= tubularSegments; j++) {
35+
int a = (tubularSegments + 1) * i + j - 1;
36+
int b = (tubularSegments + 1) * (i - 1) + j - 1;
37+
int c = (tubularSegments + 1) * (i - 1) + j;
38+
int d = (tubularSegments + 1) * i + j;
39+
40+
torus.vertex(vertices[a][0], vertices[a][1], vertices[a][2]);
41+
torus.vertex(vertices[b][0], vertices[b][1], vertices[b][2]);
42+
torus.vertex(vertices[c][0], vertices[c][1], vertices[c][2]);
43+
torus.vertex(vertices[d][0], vertices[d][1], vertices[d][2]);
44+
}
45+
}
46+
torus.endShape();
47+
48+
return torus;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)