Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created October 14, 2021 19:24
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/a9e5d4ec2a4f17bbe8026471fb237915 to your computer and use it in GitHub Desktop.
Save KrabCode/a9e5d4ec2a4f17bbe8026471fb237915 to your computer and use it in GitHub Desktop.
int sketchWidth = 600;
int sketchHeight = 600;
int rulerWidth = 400;
int rulerHeight = 375;
float waveHeight = 200;
float waveHeightHalf = waveHeight / 2;
int xLeft = sketchWidth / 2 - rulerWidth / 2;
int xRight = sketchWidth / 2 + rulerWidth / 2;
int yTop = sketchHeight / 2 - rulerHeight / 2;
int yBot = sketchHeight / 2 + rulerHeight / 2;
float yMid = lerp(yTop, yBot, 0.5);
float xMid = lerp(xLeft, xRight, 0.5);
float xMidLeft = lerp(xLeft, xRight, 0.25);
float xMidRight = lerp(xLeft, xRight, 0.75);
String fontName = "Times New Roman";
float textSize = 28;
float xWaveTextOffset = -20;
float yWaveTextOffset = -7;
float yRulerTextOffset = -30;
int backgroundColor = 12;
void settings() {
size(sketchWidth, sketchHeight, P2D);
smooth(16);
}
void setup() {
background(backgroundColor);
noFill();
stroke(150);
strokeCap(SQUARE);
strokeWeight(2);
textSize(textSize);
textFont(createFont(fontName, textSize));
drawRuler();
strokeWeight(14);
stroke(backgroundColor);
drawWaves(true);
strokeWeight(3);
stroke(255);
drawWaves(false);
save("out.png");
}
void noisyBackground() {
loadPixels();
for (int i = 0; i < pixels.length; i++) {
pixels[i] = color(random(24));
}
updatePixels();
}
void drawRuler() {
// horizontal
line(xLeft, yMid, xRight, yMid);
// vertical lines from left to right
line(xLeft, yTop, xLeft, yBot);
line(xMidLeft, yTop, xMidLeft, yBot);
line(xMid, yTop, xMid, yBot);
line(xMidRight, yTop, xMidRight, yBot);
line(xRight, yTop, xRight, yBot);
// text from left to right
textAlign(CENTER, CENTER);
text("0", xLeft, yTop + yRulerTextOffset);
text("π / 2", xMidLeft, yTop + yRulerTextOffset);
text("π", xMid, yTop + yRulerTextOffset);
text("3π / 4", xMidRight, yTop + yRulerTextOffset);
text("2π", xRight, yTop + yRulerTextOffset);
}
void drawWaves(boolean hideRuler) {
for (int i = 0; i < 2; i++) {
beginShape();
boolean sineWave = i == 0;
for (int x = 0; x < width; x++) {
if (hideRuler) {
if (x < xLeft + 50 || x > xRight - 50) {
continue;
}
}else{
if(x < xLeft || x > xRight){
continue;
}
}
float xNorm = norm(x, xLeft, xRight-1);
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);
}
endShape();
}
textAlign(RIGHT, CENTER);
text("sin", xLeft + xWaveTextOffset, yMid + yWaveTextOffset);
text("cos", xLeft + xWaveTextOffset, yMid - waveHeightHalf + yWaveTextOffset);
}
void draw() {
// can't close the sketch with ESC without it
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment