Last active
October 2, 2022 19:27
-
-
Save companje/afb4968de439709a65152da3d03cbbcc to your computer and use it in GitHub Desktop.
Understanding 'City in a bottle' in Processing
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
//Original by Frank Force (KilledByAPixel) - https://twitter.com/KilledByAPixel/status/1517294627996545024. | |
//Observable by Daniel Darabos - https://observablehq.com/@darabos/decoding-a-city-in-a-bottle | |
//Processing version by Rick Companje as intermediate step to 8086 assembly language. | |
//this version currently lacks textures on the buildings. To be continued. | |
int w=72; | |
int h=50; | |
int t=0; | |
void setup() { | |
size(576, 400); | |
noStroke(); | |
} | |
void draw() { | |
scale(8); | |
for (int i=w*h; i>0; i--) { | |
int x=t*w; | |
int y=0; | |
int z=0; | |
int u = i%w; //0..72 | |
int v = i/w; //0..50 | |
int a=u*2-w; // -72..72 | |
int b=100-v*3; // -50..100 | |
do { | |
if (hitBuilding(x, y, z)) | |
break; | |
x+=a; | |
y-=b; | |
z++; | |
} while (z<w); | |
fill(z*3); | |
rect(u, v, 1, 1); | |
} | |
t++; | |
} | |
boolean hitBuilding(int x, int y, int z) { | |
x /= w; //in this version x,y and z are 72x bigger than the original to be able to work with just integers and no floats | |
y /= w; | |
int a = x % w; //AVENUE_PERIOD | |
int b = a > 27 ? 1 : 0; //AVENUE_WIDTH | |
int c = x / 9; //BUILDING_WIDTH | |
int d = z / 9; //BUILDING_DEPTH | |
int e = c ^ d; | |
int f = z > 31 ? 1 : 0; //CITY_DISTANCE | |
int g = f & b; | |
int h = g!=0 ? e : 0; | |
int j = h * 8; | |
int l = j % 60; //BUILDING_HEIGHT + 1 | |
int m = 10 - l; //GROUND_PLANE | |
return y >= m; | |
} |
Author
companje
commented
Oct 2, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment