Skip to content

Instantly share code, notes, and snippets.

@mathisonian mathisonian/poygon.pde
Last active Aug 29, 2015

Embed
What would you like to do?
polygon painting
// setup params
int rows = 4;
int columns = 4;
int squareSize = 40;
int spreadSize = squareSize;
// whitespace
int margin = 200;
// inverse speed
// 1 = fastest,
// bigger value = slower
int steps = 10;
ArrayList<Polygon> polygons;
ArrayList<Polygon> updatingPolygons;
Polygon updatingPolygon = null;
void setup() {
frameRate(60);
size(2 * margin + columns * squareSize,
2 * margin + rows * squareSize);
// blendMode(DIFFERENCE);
polygons = new ArrayList<Polygon>();
updatingPolygons = new ArrayList<Polygon>();
updatePolys();
}
void updatePolys() {
polygons.clear();
updatingPolygons.clear();
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
Polygon p1 = new Polygon(i, j);
Polygon p2 = new Polygon(i, j);
polygons.add(p1);
polygons.add(p2);
updatingPolygons.add(p1);
updatingPolygons.add(p2);
}
}
}
void draw() {
clear();
background(255);
drawLattice();
}
void drawLattice() {
int circSize = 2;
noStroke();
fill(0);
for (int i=0; i<columns; i++) {
for (int j=0; j<rows; j++) {
// ellipse(margin + i * squareSize, margin + j * squareSize, circSize, circSize);
// ellipse(margin + (i+1) * squareSize, margin + j * squareSize, circSize, circSize);
// ellipse(margin + i * squareSize, margin + (j+1) * squareSize, circSize, circSize);
// ellipse(margin + (i+1) * squareSize, margin + (j+1) * squareSize, circSize, circSize);
}
}
if (updatingPolygon == null || updatingPolygon.update() && updatingPolygons.size() > 0) {
int rx = (int) random(0, updatingPolygons.size());
updatingPolygon = updatingPolygons.get(rx);
}
// updatingPolygon = polygons.get(0);
// updatingPolygon.update();
boolean done = true;
for (Polygon p : polygons) {
p.draw();
if (!p.done) {
done = false;
}
else {
updatingPolygons.remove(p);
}
}
if (done || updatingPolygons.size() == 0) {
delay(2000);
updatePolys();
}
}
class Polygon {
int row, column;
PVector[] points, startingPoints;
int updateCount;
int updateStep;
boolean done;
color c;
Polygon(int row, int column) {
this.row = row;
this.column = column;
this.updateStep = 0;
this.updateCount = 0;
this.done = false;
PVector center = getCenter(row, column);
this.points = new PVector[4];
this.startingPoints = new PVector[4];
PVector p1 = new PVector(center.x + random(-spreadSize, spreadSize), center.y + random(-spreadSize, spreadSize));
PVector p2 = new PVector(center.x + random(-spreadSize, spreadSize), center.y + random(-spreadSize, spreadSize));
PVector p3 = new PVector(center.x + random(-spreadSize, spreadSize), center.y + random(-spreadSize, spreadSize));
PVector p4 = new PVector(center.x + random(-spreadSize, spreadSize), center.y + random(-spreadSize, spreadSize));
this.points[0] = p1;
this.startingPoints[0] = p1;
this.points[1] = p2;
this.startingPoints[1] = p2;
this.points[2] = p3;
this.startingPoints[2] = p3;
this.points[3] = p4;
this.startingPoints[3] = p4;
if ((row + column) % 2 == 0) {
this.c = color(41, 38, 77, random(100, 100));
}
else {
this.c = color(41, 38, 77, random(50, 50));
}
}
boolean update() {
boolean finished = false;
if (this.updateStep >= steps) {
this.updateStep = 0;
finished = true;
}
if (this.updateStep == 0) {
this.updateCount++;
}
if (this.updateCount > 4) {
this.updateStep = 1;
this.done = true;
return true;
}
this.updateStep++;
PVector source = this.startingPoints[this.updateCount - 1];
PVector target = getPoint(this.row, this.column, this.updateCount-1);
PVector point = new PVector(int(map(this.updateStep, 1, steps, source.x, target.x)), int(map(this.updateStep, 1, steps, source.y, target.y)));
if (this.updateStep == steps) {
point.x = target.x;
point.y = target.y;
}
this.points[this.updateCount-1] = point;
return finished;
}
void draw() {
noStroke();
fill(c);
quad(this.points[0].x, this.points[0].y,
this.points[1].x, this.points[1].y,
this.points[2].x, this.points[2].y,
this.points[3].x, this.points[3].y);
}
}
PVector getCenter(int row, int column) {
return new PVector(margin + column * squareSize + squareSize / 2., margin + row * squareSize + squareSize / 2.);
}
PVector getPoint(int row, int column, int index) {
if (index == 0) {
return new PVector(margin + column * squareSize, margin + row * squareSize);
}
else if (index == 1) {
return new PVector(margin + (column+1) * squareSize, margin + row * squareSize);
}
else if (index == 2) {
return new PVector(margin + (column+1) * squareSize, margin + (row+1) * squareSize);
}
return new PVector(margin + (column) * squareSize, margin + (row+1) * squareSize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.