-
-
Save anonymous/021f08fa6b02aea0b140 to your computer and use it in GitHub Desktop.
regex fractal
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
// Labelling sectors | |
String[][] labels; | |
String labelMode = "qtree"; | |
void labelPixels() | |
{ | |
labels = new String[w][h]; | |
int x, y; | |
String label = ""; | |
for(int i = 0; i < w; i++) | |
{ | |
for(int j = 0; j < h; j++) | |
{ | |
if(labelMode.equals("qtree")) | |
label = quadTreeLabel(i, j); | |
labels[i][j] = label; | |
} | |
} | |
} | |
// Divide a square/rectangle into four pieces labelled by quadrant 1/2/3/4 | |
String quadTreeLabel(float x, float y) | |
{ | |
return quadTreeLabelRec(new PVector(x, y), new PVector(0, 0), new PVector(w, h)); | |
} | |
String quadTreeLabelRec(PVector point, PVector topleft, PVector bottomright) | |
{ | |
if(abs(topleft.x - bottomright.x) < 1 || abs(topleft.y - bottomright.y) < 1) | |
return ""; | |
PVector centre = new PVector(topleft.x, topleft.y); | |
centre.add(bottomright); | |
centre.mult(0.5); | |
if(point.x < centre.x) | |
{ | |
if(point.y < centre.y) | |
return "4" + quadTreeLabelRec(point, topleft, centre); | |
else | |
return "3" + quadTreeLabelRec(point, new PVector(topleft.x, centre.y), new PVector(centre.x, bottomright.y)); | |
} | |
else | |
{ | |
if(point.y < centre.y) | |
return "1" + quadTreeLabelRec(point, new PVector(centre.x, topleft.y), new PVector(bottomright.x, centre.y)); | |
else | |
return "2" + quadTreeLabelRec(point, centre, bottomright); | |
} | |
} |
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
// regex fractal (specifically /[13]2/) | |
// amiagoodperson.tumblr.com | |
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
int w, h; | |
int count = 0; | |
int fps = 5; | |
int bgFreq = 1; //every n frames, 0 for never | |
color bgColor = color(0, 20); //rgba | |
String filename = "rf[13]2"; | |
int saveFreq = 1; | |
int interval = 9; // ceil log2 of w/h | |
int saveStart = interval; | |
boolean iterate = true; | |
boolean rollingShutter = false; // looks better with ofn enabled | |
boolean onlyFillNew = true; // only recolor matches that didn't match last iteration | |
int iterInterval = 1; | |
Pattern regex = Pattern.compile("[13]2"); | |
color matchColor; | |
void setup() | |
{ | |
size(500, 500); | |
w = width; | |
h = height; | |
colorMode(HSB); | |
ellipseMode(RADIUS); | |
frameRate(fps); | |
background(0); | |
labelPixels(); | |
} | |
void draw() | |
{ | |
if(bgFreq != 0 && count % bgFreq == 0) | |
{ | |
float pad = 15; | |
fill(bgColor); | |
rect(-pad, -pad, w+pad*2, h+pad*2); | |
} | |
count++; | |
int hue = (80 + 15 * (count % interval)) % 256; | |
matchColor = color(hue, 255, 255, 170); | |
PImage tmpImg = createImage(w, h, RGB); | |
tmpImg.loadPixels(); | |
loadPixels(); | |
for(int i = 0; i < w; i++) | |
{ | |
for(int j = 0; j < h; j++) | |
{ | |
String label = labels[i][j]; | |
if(iterate) | |
{ | |
int numChars = ((count % interval) / iterInterval); | |
if(rollingShutter) | |
{ | |
numChars += j/50; | |
} | |
numChars = 1 + (numChars % label.length()); | |
label = label.substring(0, numChars); | |
} | |
Matcher m = regex.matcher(label); | |
boolean found = m.find(); | |
boolean shouldFill = found; | |
// check difference between previous iter and current | |
if(onlyFillNew && label.length() > 1) | |
{ | |
Matcher oldm = regex.matcher(label.substring(0, label.length() - 1)); | |
shouldFill ^= oldm.find(); | |
} | |
if(shouldFill) | |
{ | |
tmpImg.pixels[j*w+i] = matchColor; | |
} | |
else if(!found) | |
{ | |
tmpImg.pixels[j*w+i] = color(0); | |
} | |
else // leave untouched | |
tmpImg.pixels[j*w+i] = pixels[j*w+i]; | |
} | |
} | |
image(tmpImg, 0, 0); | |
if(filename.length() > 0 && count % saveFreq == 0 && count > saveStart && (count - saveStart) <= interval) | |
save(filename + filename((count-saveStart)/saveFreq) + ".png"); | |
} | |
String filename(int i) | |
{ | |
if(i > 26) | |
return 'z' + filename(i - 26); | |
return ""+(char)('a'+i-1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment