Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pattern generator for PCD Chicago 2019
import processing.pdf.*;
int paperWidth = 11;
int paperHeight = 17;
int DPI = 300;
int margin = 100;
color[] colorPalette = { color(179, 221, 242),
color(255),
color(255, 0, 0)
};
void setup() {
// 11x17 @ 300 DPI
// processing doesn't let you calculate at this at run time anymore.
size(3300, 5100, PDF, "filename.pdf");
ellipseMode (CENTER);
}
void draw() {
background (255);
drawGridOfTriangles (20, 4);
// Exit the program
println("Finished.");
exit();
}
void drawGridOfTriangles (float triangleSize, float triMargin) {
float superTriangleSize = triangleSize + triMargin;
float superTriangleHeight = sqrt(3)/2 * superTriangleSize;
float triRows = (height - 2 * margin - 0.5 * superTriangleHeight) / (superTriangleHeight);
float triCols = (width - 2 * margin - superTriangleSize) / (superTriangleSize);
noStroke();
// draw 2 per grid cell
for (int r = 0; r < triRows; r++) {
for (int c = 0; c < triCols; c++) {
float x1 = margin + c * superTriangleSize;
float y1 = margin + r * superTriangleHeight;
float x2 = x1 + 0.5 * superTriangleSize;
float y2 = y1 + superTriangleHeight;
float x3 = x1 + superTriangleSize;
float y3 = y1;
float m1x = (x1 + x2) / 2;
float m1y = (y1 + y2) / 2;
float m2x = (x2 + x3) / 2;
float m2y = (y2 + y3) / 2;
float m3x = (x3 + x1) / 2;
float m3y = (y3 + y1) / 2;
float cx = (m1x + m2x + m3x) / 3;
float cy = (m1y + m2y + m3y) / 3;
pushMatrix();
translate (cx, cy);
if (r % 2 == 1) {
translate (0, (y2 - cy) - (cy - y1));
rotate (radians(60));
}
//triangle (x1 - cx, y1 - cy, x2 - cx, y2 - cy, x3 - cx, y3 - cy);
scale (triangleSize / superTriangleSize);
int index = int(random(colorPalette.length));
fill(colorPalette[index]);
triangle (x1 - cx, y1 - cy, x2 - cx, y2 - cy, x3 - cx, y3 - cy);
popMatrix ();
pushMatrix ();
translate (cx + superTriangleSize * 0.5, cy);
if (r % 2 == 0) {
translate (0, (y2 - cy) - (cy - y1));
rotate (radians(-60));
}
//triangle (x1 - cx, y1 - cy, x2 - cx, y2 - cy, x3 - cx, y3 - cy);
scale (triangleSize / superTriangleSize);
index = int(random(colorPalette.length));
fill(colorPalette[index]);
triangle (x1 - cx, y1 - cy, x2 - cx, y2 - cy, x3 - cx, y3 - cy);
popMatrix();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment