Last active
October 16, 2021 10:08
-
-
Save KrabCode/91068fe4c64cc75fd3de88d4c4416ea0 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.Date; | |
int sketchWidth = 600; | |
int sketchHeight = 600; | |
int rulerWidth = 400; | |
int rulerHeightOuter = 130; | |
float rulerHeightInner = 130; | |
float waveHeight = 200; | |
float waveHeightHalf = waveHeight / 2; | |
int xLeft = sketchWidth / 2 - rulerWidth / 2; | |
int xRight = sketchWidth / 2 + rulerWidth / 2; | |
float xMid = sketchWidth/2; | |
float yMid = sketchHeight/2; | |
float xMidLeft = lerp(xLeft, xRight, 0.25); | |
float xMidRight = lerp(xLeft, xRight, 0.75); | |
ArrayList<PFont> fonts = new ArrayList<PFont>(); | |
int currentFontIndex = 189; | |
float textSize = 32; | |
float xWaveTextOffset = -24; | |
float yWaveTextOffset = -10; | |
float yRulerTextOffset = -80; | |
float t; | |
int rulerColor = 120; | |
int textColor = 255; | |
int pieGraphFillColor = 120; | |
int pieGraphOutlineColor = 255; | |
void settings() { | |
size(sketchWidth, sketchHeight, P2D); | |
smooth(16); | |
} | |
void setup() { | |
loadAvailableFonts(); | |
background(10); | |
noFill(); | |
stroke(150); | |
strokeCap(SQUARE); | |
textSize(textSize); | |
render(); | |
savePng(); | |
} | |
void draw() { | |
render(); | |
} | |
void render() { | |
background(10); | |
translate(0, height * 0.035); | |
stroke(rulerColor); | |
strokeWeight(2); | |
drawRuler(); | |
stroke(textColor); | |
noFill(); | |
drawWaves(); | |
// hide the wave line overlap on the sides | |
stroke(0); | |
strokeWeight(2.5); | |
drawRulerVerticalOuter(); | |
stroke(rulerColor); | |
strokeWeight(2); | |
drawRulerVerticalOuter(); | |
noFill(); | |
stroke(rulerColor); | |
rulerPieGraphs(); | |
fill(255); | |
noStroke(); | |
rulerText(); | |
} | |
void drawRuler() { | |
drawHorizontalLine(); | |
drawRulerVerticalOuter(); | |
drawRulerVerticalInner(); | |
} | |
void drawHorizontalLine() { | |
line(xLeft, yMid, xRight, yMid); | |
} | |
void drawRulerVerticalOuter() { | |
pushMatrix(); | |
translate(0, yMid); | |
float n = rulerHeightOuter; | |
line(xLeft, n, xLeft, -n); | |
line(xRight, n, xRight, -n); | |
popMatrix(); | |
} | |
void drawRulerVerticalInner() { | |
pushMatrix(); | |
translate(0, yMid); | |
float n = rulerHeightInner; | |
line(xMidLeft, n, xMidLeft, -n); | |
line(xMid, n, xMid, -n); | |
line(xMidRight, n, xMidRight, -n); | |
popMatrix(); | |
} | |
void rulerText() { | |
pushMatrix(); | |
translate(0, yMid); | |
textAlign(CENTER, CENTER); | |
//text("", xLeft, yRulerTextOffset - rulerHeightOuter); | |
//text("1/4τ", xMidLeft, yRulerTextOffset - rulerHeightInner); | |
//text("π", xMid, yRulerTextOffset - rulerHeightInner); | |
//text("3/4τ", xMidRight, yRulerTextOffset - rulerHeightInner); // trailing spaces to center the slash | |
text("τ", xRight, yRulerTextOffset - rulerHeightOuter); | |
popMatrix(); | |
} | |
void rulerPieGraphs() { | |
float diameter = 40; | |
pushMatrix(); | |
translate(0, yMid+textSize*0.2); // align to the text | |
drawRulerPieGraph(0, xLeft, yRulerTextOffset - rulerHeightInner, diameter); | |
drawRulerPieGraph(0.25, xMidLeft, yRulerTextOffset - rulerHeightInner, diameter); | |
drawRulerPieGraph(0.5, xMid, yRulerTextOffset - rulerHeightInner, diameter); | |
drawRulerPieGraph(0.75, xMidRight, yRulerTextOffset - rulerHeightInner, diameter); | |
drawRulerPieGraph(1, xRight, yRulerTextOffset - rulerHeightOuter, diameter); | |
popMatrix(); | |
} | |
void drawRulerPieGraph(float maxNorm, float x, float y, float size) { | |
pushMatrix(); | |
translate(x, y); | |
noStroke(); | |
fill(pieGraphFillColor); | |
arc(0, 0, size, size, 0, maxNorm * TAU); | |
stroke(pieGraphOutlineColor); | |
noFill(); | |
ellipse(0, 0, size, size); | |
popMatrix(); | |
} | |
void drawWaves() { | |
for (int i = 0; i < 2; i++) { | |
beginShape(); | |
drawWave(i == 0); | |
endShape(); | |
} | |
textAlign(RIGHT, CENTER); | |
text("sin", xLeft + xWaveTextOffset, yMid + yWaveTextOffset); | |
text("cos", xLeft + xWaveTextOffset, yMid - waveHeightHalf + yWaveTextOffset); | |
} | |
void drawWave(boolean sineWave) { | |
for (int x = xLeft; x <= xRight; x++) { | |
float xNorm = norm(x, xLeft, xRight) + t; | |
float waveNorm = (sineWave ? sin(TAU * xNorm) : cos(TAU * xNorm)); | |
waveNorm *= -1; // because negative Y is up in processing | |
float y = yMid + waveHeightHalf * waveNorm; | |
vertex(x, y); | |
} | |
} | |
void setCurrentFont() { | |
PFont font = fonts.get(currentFontIndex); | |
textFont(font); | |
println("Font set to " + currentFontIndex + " " + font.getName()); | |
} | |
void loadAvailableFonts() { | |
String[] fontNames = PFont.list(); | |
for (String fontName : fontNames) { | |
fonts.add(createFont(fontName, textSize)); | |
println(fonts.size() - 1 + " " + fontName); | |
} | |
println("\nFonts loaded\nChange font using the mouse wheel\n"); | |
setCurrentFont(); | |
} | |
void mouseWheel(MouseEvent event) { | |
float e = event.getCount(); | |
if (e > 0) { | |
currentFontIndex -= 1; | |
} else if (e < 0) { | |
currentFontIndex += 1; | |
} | |
if (currentFontIndex < 0) { | |
currentFontIndex = fonts.size() - 1; | |
} | |
currentFontIndex %= fonts.size(); | |
setCurrentFont(); | |
} | |
void keyPressed() { | |
if (key == 'i') { | |
savePng(); | |
} | |
} | |
void savePng() { | |
long millisUTC = new Date().getTime(); | |
String imagePath = "/out/" + millisUTC + " " + fonts.get(currentFontIndex).getName() + ".png"; | |
println("saved " + imagePath); | |
save(imagePath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment