Created
December 23, 2017 19:42
-
-
Save jacobjoaquin/8083f6261b9a58e2a0f39f544b8d959e to your computer and use it in GitHub Desktop.
Plotter Art: Oscillators in 3D 20171222
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Oscillators in 3D 20171222 | |
by Jacob Joaquin | |
email: [email protected] | |
twitter: https://twitter.com/JacobJoaquin | |
instagram: https://www.instagram.com/jacobjoaquin/ | |
*/ | |
import processing.pdf.*; | |
// User variables | |
boolean doGeneratePDF = true; // Create a PDF file? | |
int nPoints = 8192; // Resolution of path | |
int nRotations = 16; // Number of rotations of path | |
float o = 800; // Dimensions offset, bi-polar, [-o, o] | |
// Internal variables | |
ArrayList<PVector> path = new ArrayList<PVector>(); | |
ArrayList<PVector> pointsFlattened = new ArrayList<PVector>(); | |
void createPath(int nPoints) { | |
for (int i = 0; i < nPoints; i++) { | |
float n = (float) i / (float) nPoints; | |
float x = sin(n * TAU * 2) * o; | |
float y = cos(n * TAU) * o; | |
float z = sin(n * TAU) * o; | |
PVector p = new PVector(x, y, z); | |
path.add(p); | |
} | |
} | |
void settings() { | |
size(612, 792, P3D); | |
pixelDensity(displayDensity()); | |
} | |
void setup() { | |
createPath(nPoints); | |
noLoop(); | |
// Create figure | |
pushMatrix(); | |
translate(width / 2.0, height / 2.0, -2100); | |
for (int i = 0; i < nRotations / 2; i++) { | |
float n = (float) i / (float) nRotations; | |
pushMatrix(); | |
rotateY(n * TAU); | |
for (PVector p : path) { | |
pushMatrix(); | |
translate(p.x, p.y, p.z); | |
rotateY(-n * TAU); | |
// Modulate path | |
float d = map(p.y, -o, o, -40, 40); | |
float amp = 12 * sin(n * TAU * 4); | |
translate(0, d * sin((p.y / o * amp) * TAU * nRotations), 0); | |
translate(d * cos((p.y / o * amp) * TAU * nRotations), 0, 0); | |
// Flatten: Convert 3D path points to 2D points | |
float x = screenX(0, 0, 0); | |
float y = screenY(0, 0, 0); | |
float z = screenZ(0, 0, 0); | |
pointsFlattened.add(new PVector(x, y, z)); | |
popMatrix(); | |
} | |
popMatrix(); | |
} | |
popMatrix(); | |
} | |
void draw() { | |
// Display figure on screen | |
stroke(0, 128); | |
noFill(); | |
beginShape(); | |
for (PVector p : pointsFlattened) { | |
vertex(p.x, p.y); | |
} | |
endShape(CLOSE); | |
// Generate the PDF | |
if (doGeneratePDF) { | |
PGraphics pdf = createGraphics(width, height, PDF, "oscillatorsIn3D20171222.pdf"); | |
pdf.beginDraw(); | |
pdf.scale(1.0 / displayDensity(), 1.0 / displayDensity()); | |
pdf.beginShape(); | |
pdf.noFill(); | |
for (PVector p : pointsFlattened) { | |
pdf.vertex(p.x, p.y); | |
} | |
pdf.endShape(CLOSE); | |
pdf.dispose(); | |
pdf.endDraw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment