Dividing Polygons
by Algirdas Rascius.
Mouse-click to restart.
/
static final int ANIMATE_STEPS = 50;
static final float MIN_SIDE = 30;
LinkedList activePolygons;
LinkedList splittablePolygons;
LinkedList donePolygons;
void setup() {
size(500, 500);
background(0);
colorMode(HSB, TWO_PI, 1, 1, 1);
smooth();
stroke(0);
strokeWeight(2);
initialize();
frameRate(25);
}
void draw() {
// Animate
filter(ERODE);
filter(BLUR);
drawPolygons(activePolygons);
drawPolygons(splittablePolygons);
drawPolygons(donePolygons);
// Check for polygons that ended to animate
for (Iterator i=activePolygons.iterator(); i.hasNext();) {
Polygon p = (Polygon)i.next();
if (p.isAnimationDone()) {
i.remove();
if (p.canSplit()) {
splittablePolygons.add(p);
} else {
donePolygons.add(p);
}
}
}
// Randomly split polygons
float splitProbability = 1.0/(splittablePolygons.size()+10);
for (Iterator i=splittablePolygons.iterator(); i.hasNext();) {
Polygon p = (Polygon)i.next();
if (random(1)= ANIMATE_STEPS;
}
/**
Returns true, if polygon can be split into two smaller polygons.
Polygon can be, split if at least two sides are splitable.
/
boolean canSplit() {
int splitableSides = 0;
int n = x.length;
for (int i=0; i= 2;
}
/**
Returns true, if specified side is splitable.
Side can be split, if it is longer than two miminum side lengths.
/
boolean isSplitableSide(int i) {
int n = x.length;
return dist(x[i], y[i], x[(i+1)%n], y[(i+1)%n]) >= 2 * MIN_SIDE;
}
/**
Splits polygon into two. Append resulting polygons to specified list.
/
void splitPolygon(LinkedList targetList) {
int n = x.length;
// Randomly get first side to be split
int side1;
do {
side1 = (int)random(n);
} while (!isSplitableSide(side1));
// Randomly get second side to be split
int side2;
do {
side2 = (int)random(n);
} while (side1 == side2 || !isSplitableSide(side2));
// Get randon split points
float c1 = getRandomSplitPoint(side1);
float c2 = getRandomSplitPoint(side2);
// Do split
targetList.add(createNew(side1, side2, c1, c2, 1));
targetList.add(createNew(side2, side1, c2, c1, -1));
}
/**
Create new polygon that is splitted half of this one
/
Polygon createNew(int side1, int side2, float c1, float c2, float hueMultiplier) {
int n = x.length;
float xSplit1 = x[side1]*c1 + x[(side1+1)%n]*(1-c1);
float ySplit1 = y[side1]*c1 + y[(side1+1)%n]*(1-c1);
float xSplit2 = x[side2]*c2 + x[(side2+1)%n]*(1-c2);
float ySplit2 = y[side2]*c2 + y[(side2+1)%n]*(1-c2);
int n1 = (side2-side1+n) % n + 2;
float[] x1 = new float[n1];
float[] y1 = new float[n1];
x1[0] = xSplit2;
y1[0] = ySplit2;
x1[1] = xSplit1;
y1[1] = ySplit1;
for (int i=2; i
Source code: DividingPolygons
Built with Processing