Created
September 10, 2017 20:42
-
-
Save FlorianCassayre/cd1c9fc5a7e6194b18dccb800de53341 to your computer and use it in GitHub Desktop.
Reproduction of "Self-Description" by xkcd (https://xkcd.com/688).
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
// Reproduction of "Self-Description" by xkcd (https://xkcd.com/688). | |
// @author Florian Cassayre | |
final int PANEL_WIDTH = 500, PANEL_HEIGHT = 400, PADDING = 50, WEIGHT = 4; | |
PFont font; | |
void setup() | |
{ | |
surface.setSize(PANEL_WIDTH * 3 + PADDING * 2, PANEL_HEIGHT); | |
font = createFont("Arial Bold", 20); | |
frameRate(1); // Change this to see the convergence | |
} | |
void draw() | |
{ | |
loadPixels(); // Save previous state | |
background(255); | |
fill(255); | |
strokeWeight(WEIGHT); | |
stroke(0); | |
textAlign(LEFT); | |
textFont(font); | |
for(int i = 0; i < 3; i++) | |
rect(i * (PANEL_WIDTH + PADDING), 0, PANEL_WIDTH - (WEIGHT >> 1), PANEL_HEIGHT - (WEIGHT >> 1)); | |
// Panel 1 | |
ellipse(300, 200, 300, 300); | |
line(143, 51, 190, 51); | |
line(190, 51, 230, 92); | |
line(143, 342, 165, 342); | |
line(165, 342, 192, 315); | |
noStroke(); | |
fill(0); | |
final float start = 2.02; | |
arc(300, 200, 300, 300, start, start + (blackPercentage(0) + blackPercentage(1) + blackPercentage(2)) * TWO_PI / 3); | |
text("FRACTION OF THIS IMAGE WHICH IS WHITE", 15, 12, 170, 150); | |
text("FRACTION OF THIS IMAGE WHICH IS BLACK", 15, 304, 170, 150); | |
// Panel 2 | |
stroke(0); | |
line(610, 106, 610, 340); | |
line(610, 340, 978, 340); | |
text("AMOUNT OF BLACK INK PER PANEL:", 567, 12, 150, 150); | |
noStroke(); | |
fill(0); | |
textAlign(CENTER); | |
for(int i = 0; i < 3; i++) | |
{ | |
text(i + 1 + "", 670 + i * 100, 350, 50, 50); | |
rect(670 + i * 100, 340, 40, -blackPercentage(i) * 1600); | |
} | |
textAlign(LEFT); | |
// Panel 3 | |
text("LOCATION OF BLACK INK IN THIS IMAGE:", 1118, 12, 150, 150); | |
stroke(0); | |
line(1150, 155, 1150, 284); | |
line(1150, 284, 1577, 284); | |
line(1150, 155, 1145, 165); | |
line(1150, 155, 1155, 165); | |
line(1577, 284, 1567, 279); | |
line(1577, 284, 1567, 289); | |
final PGraphics g = createGraphics(width, height); | |
g.beginDraw(); | |
g.loadPixels(); | |
for(int i = 0; i < pixels.length; i++) | |
g.pixels[i] = pixels[i]; | |
g.updatePixels(); | |
g.endDraw(); | |
image(g, 1160, 176, width / 4, height / 4); | |
} | |
void keyPressed() | |
{ | |
if((key + "").toLowerCase().equals("s")) // Press 'S' to save the image | |
saveFrame(); | |
} | |
float blackPercentage(int i) | |
{ | |
int count = 0; | |
float sum = 0.0; | |
for(int y = 0; y < PANEL_HEIGHT - 1; y++) | |
{ | |
for(int x = i * (PANEL_WIDTH + PADDING); x < (i + 1) * (PANEL_WIDTH + PADDING) - 1; x++) | |
{ | |
sum += red(pixels[x + y * width]); | |
count++; | |
} | |
} | |
return 1 - sum / (count * 255); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment