Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active August 1, 2023 13:17
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 KrabCode/4f4567ff21c5a0f5c80a610df6d203a7 to your computer and use it in GitHub Desktop.
Save KrabCode/4f4567ff21c5a0f5c80a610df6d203a7 to your computer and use it in GitHub Desktop.
Simple spirograph editor using the LazyGui library and Processing 4
import com.krab.lazy.*;
LazyGui gui;
int shapeCount = 3;
int shapeCountMax = 10;
void setup() {
size(800, 800, P3D);
smooth(4);
gui = new LazyGui(this, new LazyGuiSettings()
.setHideBuiltInFolders(true)
.setLoadLatestSaveOnStartup(true)
.setStartGuiHidden(true));
}
void draw() {
hint(DISABLE_DEPTH_TEST);
background(gui.colorPicker("background", color(20)).hex);
translate(width*0.5f, height*0.5f);
if (gui.button("add shape")) {
gui.sliderIntSet("shape count", shapeCount + 1);
}
shapeCount = gui.sliderInt("shape count", shapeCount);
shapeCountMax = max(shapeCount, shapeCountMax);
gui.pushFolder("shapes");
for (int shapeIndex = 0; shapeIndex < shapeCountMax; shapeIndex++) {
gui.pushFolder("shape #" + shapeIndex);
if (shapeIndex < shapeCount) {
gui.showCurrentFolder();
pushMatrix();
drawShape(shapeIndex);
popMatrix();
} else {
gui.hideCurrentFolder();
}
gui.popFolder();
}
gui.popFolder();
//if(frameCount < 361){
//save("out/" + frameCount + ".jpg");
//}
}
void drawShape(int shapeIndex) {
stroke(gui.colorPicker("color", color(255)).hex);
strokeWeight(gui.slider("weight", 3f));
noFill();
float radiusBig = gui.sliderInt("radius a", 100 + shapeIndex * 100);
float radiusSmall = radiusBig / gui.sliderInt("radius b = a \\/ n", 10);
float holeDistance = gui.slider("hole distance", 15);
int detail = gui.sliderInt("detail", 1000);
boolean shouldDrawGradually = gui.toggle("draw gradually");
float gradualDrawSpeed = gui.slider("draw speed", 1);
float gradualMaxAngle = radians(gui.slider("draw length", 0.99) * 360);
if (shouldDrawGradually) {
float startAngle = radians(frameCount * gradualDrawSpeed);
beginShape();
for (int i = 0; i < detail; i++) {
float bigAngle = map(i, 0, detail-1, 0, TAU);
if (bigAngle > gradualMaxAngle) {
break;
}
float finalAngle = startAngle + bigAngle;
PVector p = getSpirographPoint(radiusBig, radiusSmall, finalAngle, holeDistance);
vertex(p.x, p.y);
}
endShape();
} else {
beginShape();
for (int i = 0; i < detail; i++) {
float bigAngle = map(i, 0, detail-1, 0, TAU);
PVector p = getSpirographPoint(radiusBig, radiusSmall, bigAngle, holeDistance);
vertex(p.x, p.y);
}
endShape(CLOSE);
}
}
PVector getSpirographPoint(float radiusBig, float radiusSmall, float bigAngle, float holeDistance) {
float smallCenterRadius = radiusBig - radiusSmall;
float smallCenterX = smallCenterRadius * cos(bigAngle);
float smallCenterY = smallCenterRadius * sin(bigAngle);
float smallRotation = - (smallCenterRadius / radiusSmall) * bigAngle;
float holeX = smallCenterX + holeDistance * cos(smallRotation);
float holeY = smallCenterY + holeDistance * sin(smallRotation);
return new PVector(holeX, holeY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment