Skip to content

Instantly share code, notes, and snippets.

@FreedomGrenade
Created September 7, 2015 07:36
Show Gist options
  • Save FreedomGrenade/57e729c8b59a454d8f42 to your computer and use it in GitHub Desktop.
Save FreedomGrenade/57e729c8b59a454d8f42 to your computer and use it in GitHub Desktop.
import java.util.*;
PImage img;
int num = 15000;
ArrayList<PVector> pRank;
color silver = color(246, 246, 250);
color dblue = color(112, 83, 145);
color dblue2 = color(185, 181, 252);
void setup() {
img = loadImage("a.png"); //choose image here
size(img.width, img.height);
noStroke();
background(silver);
pRank = new ArrayList<PVector>();
img.loadPixels();
for (int loc = 0; loc < img.pixels.length - 1; loc++) {
float total1 = red(img.pixels[loc]) + green(img.pixels[loc]) + blue(img.pixels[loc]);
float total2 = red(img.pixels[loc+1]) + green(img.pixels[loc+1]) + blue(img.pixels[loc+1]);
int diff = (int)(total1-total2);
// the PVector.z is used to sort so set that to diff
// loc % width = x, loc / width = y (this isn't faster or anything)
pRank.add(new PVector(loc % width, loc / width, diff));
}
// Sort pRank with the Comp comparator below
Collections.sort(pRank, new Comp());
}
public class Comp implements Comparator<PVector> {
public int compare(PVector a, PVector b) {
return (a.z == b.z) ? 0 : ((a.z < b.z) ? 1 : -1);
}
}
void draw() {
noLoop();
lines2();
save("fistLine.png");
}
void lines2() {
stroke(0);
float closest;
ArrayList<PVector> vectors = new ArrayList<PVector>();
// in case num was set higher than the number of pixels in the image
int max = min(num, pRank.size());
// there's probably a way to just remove then end of the pRank arraylist and use that instead of copying them.
for (int i = 0; i < max; i++) {
vectors.add(pRank.get(i));
}
// this is now the slowest part of the sketch, I don't know how to improve finding closests pairs
int a=0;
int b=0;
float curDist=width;
while (vectors.size()>34) {
closest=width;
float px = vectors.get(a).x;
float py = vectors.get(a).y;
vectors.remove(a);
for (int p=0; p<vectors.size(); p++) {
curDist=dist(px, py, vectors.get(p).x, vectors.get(p).y);
if (curDist<closest) {
closest=curDist;
b=p;
}
}
float cRatio = py/height;
color cor = lerpColor(dblue, dblue2, cRatio);
stroke(cor);
line(px, py, vectors.get(b).x, vectors.get(b).y);
a=b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment