Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/9890035c2a51f1c31408 to your computer and use it in GitHub Desktop.
Save jkwok91/9890035c2a51f1c31408 to your computer and use it in GitHub Desktop.
FUCK YEAH
/*
hilbert curve
create a series of points and then draw that
*/
int iter = 0;
// a queue to store quadrants to percolate through.
// for each quadrant i put in subquadrants into the end of the queue
ArrayList<Quadrant> quadrants = new ArrayList<Quadrant>();
ArrayList<Tuple> tuples = new ArrayList<Tuple>();
void setup() {
size(256,256);
background(255);
stroke(0);
//noLoop();
}
void mouseClicked() {
reset();
iter++;
int side = (int)(width/pow(2,iter+1));
if (side == 0) {
iter = 0;
}
drawCurve();
}
void draw() {
}
void reset() {
background(255);
quadrants.clear();
tuples.clear();
}
void drawCurve() {
createTuples(iter);
// translate by (the smallest quadrant size)/2 for height and width
int side = (int)(width/pow(2,iter+1));
pushMatrix();
translate(side,side);
connect();
popMatrix();
}
// this function gets tuples. it will go through quadrants and plop them into the arraylist
// when the side of the quadrant is (pow(4,iteration))/(pow(2,iteration)),
// it will get ordered pair representing location of that quadrant
void createTuples(int iteration) {
int desiredSide = (int)(width/pow(2,iteration));
Quadrant whole = new Quadrant(0,0,width,0);
quadrants.add(whole);
while (quadrants.size() != 0) {
Quadrant qu = quadrants.remove(0);
if (qu.s != desiredSide) {
// split quadrant further into quadrants
int smallsize = (int)(qu.s/2); // size of future subquadrant
Quadrant[] subquadrants = makeSubQs(qu.x,qu.y,smallsize,qu.r);
for (Quadrant q : subquadrants) {
quadrants.add(q);
}
}
else {
tuples.add(new Tuple(qu.x,qu.y));
}
}
}
Quadrant[] makeSubQs(int qx, int qy, int subqWidth, float rotation) {
Quadrant q0, q1, q2, q3;
Quadrant[] quads = new Quadrant[4];
if (rotation == PI/2) {
q0 = new Quadrant(qx, qy+subqWidth, subqWidth, 0);
q1 = new Quadrant(qx+subqWidth, qy+subqWidth, subqWidth, PI/2);
q2 = new Quadrant(qx+subqWidth, qy, subqWidth, PI/2);
q3 = new Quadrant(qx, qy, subqWidth, PI);
} else if (rotation == -PI/2) {
q0 = new Quadrant(qx+subqWidth, qy, subqWidth, PI);
q1 = new Quadrant(qx, qy, subqWidth, -PI/2);
q2 = new Quadrant(qx, qy+subqWidth, subqWidth, -PI/2);
q3 = new Quadrant(qx+subqWidth, qy+subqWidth, subqWidth, 0);
} else if (rotation == PI) {
q0 = new Quadrant(qx+subqWidth, qy, subqWidth, -PI/2);
q1 = new Quadrant(qx+subqWidth, qy+subqWidth, subqWidth, PI);
q2 = new Quadrant(qx, qy+subqWidth, subqWidth, PI);
q3 = new Quadrant(qx, qy, subqWidth, PI/2);
} else { // no rotation
q0 = new Quadrant(qx, qy+subqWidth, subqWidth, PI/2);
q1 = new Quadrant(qx, qy, subqWidth, 0);
q2 = new Quadrant(qx+subqWidth, qy, subqWidth, 0);
q3 = new Quadrant(qx+subqWidth, qy+subqWidth, subqWidth, -PI/2);
}
quads[0] = q0;
quads[1] = q1;
quads[2] = q2;
quads[3] = q3;
return quads;
}
void connect(){
// connects the dots
for (int i = 0; i < tuples.size()-1; i++) {
Tuple thiz = tuples.get(i);
Tuple that = tuples.get(i+1);
line(thiz.x,thiz.y,that.x,that.y);
}
}
class Quadrant {
int x;
int y;
int s;
int q;
float r;
Quadrant(int xcor, int ycor, int side, float totalRotation) {
x = xcor;
y = ycor;
s = side;
r = totalRotation;
}
String toString(){
return "I am a quadrant starting at ("+x+","+y+") with sidelength"+s+".";
}
}
class Tuple {
int x;
int y;
Tuple(int xcor, int ycor) {
x = xcor;
y = ycor;
}
}
@jkwok91
Copy link
Author

jkwok91 commented Dec 30, 2014

badness lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment