Created
May 8, 2019 22:21
-
-
Save KrabCode/6fe0048fb471b099563dac857b53aa32 to your computer and use it in GitHub Desktop.
Noise directed pixel sort
This file contains hidden or 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.UUID; | |
PImage src; | |
PGraphics temp; | |
PVector[] directions; | |
public void settings() { | |
// fullScreen(P2D, 2); | |
size(800, 800, P2D); | |
} | |
public void setup() { | |
directions = new PVector[8]; | |
directions[0] = new PVector(1, 0); | |
directions[1] = new PVector(1, -1); | |
directions[2] = new PVector(0, -1); | |
directions[3] = new PVector(-1, -1); | |
directions[4] = new PVector(-1, 0); | |
directions[5] = new PVector(-1, 1); | |
directions[6] = new PVector(0, 1); | |
directions[7] = new PVector(1, 1); | |
temp = createGraphics(width, height, P2D); | |
reset(); | |
} | |
private void reset() { | |
src = loadImage("https://picsum.photos/800/800.jpg"); | |
image(src, 0, 0, src.width, src.height); | |
temp.beginDraw(); | |
temp.image(src, 0, 0, src.width, src.height); | |
temp.endDraw(); | |
} | |
public void draw() { | |
float t = radians(frameCount); | |
float scl = .002f; | |
loadPixels(); | |
temp.beginDraw(); | |
temp.loadPixels(); | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
if (nearEdge(x, y)) { | |
continue; | |
} | |
int here = get(x, y); | |
float angle = 2 * TWO_PI * noise(x * scl, y * scl, t); | |
angle = abs(angle % TWO_PI); | |
int i = floor(map(angle, 0, TWO_PI, 0, 8)); | |
i = constrain(i, 0, 7); | |
PVector dir = directions[i]; | |
int thereX = x + (int) dir.x; | |
int thereY = y + (int) dir.y; | |
int there = get(thereX, thereY); | |
if (here > there) { | |
put(here, thereX, thereY); | |
put(there, x, y); | |
} else { | |
put(here, x, y); | |
put(there, thereX, thereY); | |
} | |
} | |
} | |
temp.updatePixels(); | |
temp.endDraw(); | |
image(temp, 0, 0); | |
} | |
private boolean nearEdge(int x, int y) { | |
return (x - 1 <= 0 || x + 1 >= width || y - 1 <= 0 || y + 1 >= height); | |
} | |
public void keyPressed() { | |
if (key == 'k') { | |
save("/capture/" + UUID.randomUUID().toString() + ".jpg"); | |
} | |
if (key == 'r') { | |
reset(); | |
} | |
} | |
void put(int c, int x, int y) { | |
temp.pixels[y * width + x] = c; | |
} | |
public float getRed(int c) { | |
return c >> 16 & 0xFF; | |
} | |
public float getGreen(int c) { | |
return c >> 8 & 0xFF; | |
} | |
public float getBlue(int c) { | |
return c & 0xFF; | |
} | |
public int get(int x, int y) { | |
return pixels[y * width + x]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment