Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Last active August 29, 2015 13:59
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/c9e55761f6a6bac69de6 to your computer and use it in GitHub Desktop.
Save jkwok91/c9e55761f6a6bac69de6 to your computer and use it in GitHub Desktop.
some parts of this are less than pretty
/*
rotating a 2D image of a potato
as suggested by Mother
april 11, 2014
*/
PImage potato;
float[][] rotationMat;
float theta;
float pw, ph;
void setup() {
size(300, 300);
//frameRate(6);
background(200);
potato = loadImage("potato.png");
pw = potato.width;
ph = potato.height;
theta = 0;
}
void draw() {
background(200);
changePixels();
theta = (theta+0.05)%TWO_PI;
}
void changePixels() {
rotationMat = getRMat(theta);
loadPixels();
potato.loadPixels();
for (int y = 0; y < ph; y++) {
for (int x = 0; x < pw; x++) {
color oldCoor = color(potato.pixels[y*potato.width+x]);
// this is here because apparently transparent images still have weirdly colored backgrounds?
// or maybe i'm doing something wrong?
if (oldCoor != color(6573620)) {
PVector coors = new PVector(x-pw/2, y-ph/2);
PVector newCoors = matMult(rotationMat, coors);
int newCoor = ((height/2)+(int)newCoors.y)*width+((width/2)+(int)newCoors.x);
if (newCoor < width*height && newCoor > 0) {
pixels[newCoor] = oldCoor;
}
}
}
}
updatePixels();
}
PVector matMult(float[][] mat, PVector p) {
float x = p.x*mat[0][0] + p.y*mat[0][1];
float y = p.x*mat[1][0] + p.y*mat[1][1];
return new PVector(x, y);
}
float[][] getRMat(float a) {
rotationMat = new float[2][2];
// column-major matrix
rotationMat[0][0] = cos(a);
rotationMat[0][1] = sin(a);
rotationMat[1][0] = -sin(a);
rotationMat[1][1] = cos(a);
return rotationMat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment