Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created September 3, 2018 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanScottLewis/6c95ca21449cd6d22c383c36e9cedcb4 to your computer and use it in GitHub Desktop.
Save RyanScottLewis/6c95ca21449cd6d22c383c36e9cedcb4 to your computer and use it in GitHub Desktop.
// vim:ft=java
// Lissajous curve
//
// xF yF
// Mouse: 2.18, 2.72
// Owl: 5.14, 2.93
float xF = 1; // X angle factor
float xA = 0; // X modulator angle
float xS = 0.005; // X modulator scale
float xI = 0.0001; // X modulator angle increment
float yF = 1; // Y angle factor
float yA = 0; // Y modulator angle
float yS = 0.01; // Y modulator scale
float yI = 0.001; // Y modulator angle increment
float aI = 0.001; // X/Y angle increment per step through 0..TAU
boolean save = true; // Do not render canvas to window and save each frame
String path = "../images"; // Path to save frames, if save is enabled
int frameCount = 1800; // Number of frames to save before completing
int canvasWidth = 500; // Width of the canvas & window
int canvasHeight = 500; // Height of the canvas & window
int canvasPadding = 50; // Padding between the edge of the canvas and the curve
int pointRadius = 5; // The radius of each point in the curve
PGraphics canvas;
float a, x, y;
int frame = 0;
int canvasWidthHalf;
int canvasHeightHalf;
int canvasPaddingHalf;
int canvasLeft, canvasTop, canvasRight, canvasBottom;
void curveDraw() {
canvas.beginDraw();
canvas.background(0);
for (float a = 0; a < TAU; a += aI) {
x = map( cos(a * xF), -1, 1, canvasLeft, canvasRight );
y = map( cos(a * yF), -1, 1, canvasTop, canvasBottom );
canvas.ellipse(x, y, pointRadius, pointRadius);
}
canvas.endDraw();
}
// Curve angles are modulated by sines at different scales and incremented at different rates
// TODO: vars for these magic numbers
void curveUpdate() {
xF += cos(xA) * xS;
xA += xI;
xA %= TAU;
yF += sin(yA) * yS;
yA += yI;
yA %= TAU;
}
void canvasDraw() {
if (save) {
background(0);
text("Frame: " + nf(frame, 6) + " of " + nf(frameCount, 6), 0, 15*1);
canvas.save(path + "/" + nf(frame, 6) + ".png");
} else {
image(canvas, 0, 0);
}
}
void settings() {
size(canvasWidth, canvasHeight, P2D); // Must be placed here to use variables within size()
}
void setup() {
canvas = createGraphics(canvasWidth, canvasHeight, P2D);
// Pre-calculations
canvasPaddingHalf = canvasPadding / 2;
canvasLeft = canvasPaddingHalf;
canvasTop = canvasPaddingHalf;
canvasRight = canvasWidth - canvasPaddingHalf;
canvasBottom = canvasHeight - canvasPaddingHalf;
// Setup canvas
canvas.beginDraw();
canvas.blendMode(ADD);
canvas.noStroke();
canvas.fill(80, 80, 200);
canvas.endDraw();
}
void draw() {
if (frame == frameCount) exit();
curveDraw();
curveUpdate();
canvasDraw();
frame++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment