/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.LORVqlCfQFU/rev.729
*
* authors:
* Fawaz Ahmad
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
float i = 0;
TriCent[][] grid;
// Constant values used in program
// calculated by trig for equilateral triangles
float sideLen = 50;
float triHeight = 43.30127;
float centerOffset1 = 14.43;
float centerOffset2 = 28.87127;
void setup() { // this is run once.
// set the background color
background(233, 255, 232);
// canvas size (Variable aren't evaluated. Integers only, please.)
size(800, 600);
int wide = 800;
int lengt = 600;
// smooth edges
smooth();
// set the width of the line.
stroke(240);
strokeWeight(1);
// vertical lines in background
while (i < wide) {
line(i, 0, i, lengt);
i = i + triHeight;
}
i = 0;
// diagonal lines going topleft to botright
while (i < lengt) {
line(0, i, lengt*sqrt(3), lengt + i);
i = i + sideLen;
}
i = 0;
while (i < wide) {
line(i, 0, i + lengt*sqrt(3), lengt);
i = i + 2*triHeight;
}
// diagonal lines going from botleft topright
i = 0;
while (i < lengt) {
line(0, i, lengt*sqrt(3), i - lengt);
i = i + sideLen;
}
i = 0;
while (i < wide) {
line(i, lengt, i + lengt*sqrt(3), 0);
i = i + 2*triHeight;
}
// create array of triangular positions, row major
int rows = (int)(2*lengt/sideLen) + 1;
int cols = (int)(wide/triHeight) + 1;
grid = new TriCent[rows][cols];
for (int col = 0; col < grid[0].length; col++) {
for (int row = 0; row < grid.length; row++) {
// deals with case if triangle is pointing left
if ((col % 2 == 0 && row % 2 == 0) || (col % 2 == 1 && row % 2 == 1))
grid[row][col] = new TriCent(col*triHeight + centerOffset2, row*sideLen/2,
col*triHeight, row*sideLen/2,
col*triHeight + triHeight, row*sideLen/2 - sideLen/2,
col*triHeight + triHeight, row*sideLen/2 + sideLen/2);
// deals with case if triangle is pointing right
else if ((col % 2 == 0 && row % 2 == 1) || (col % 2 == 1 && row % 2 == 0))
grid[row][col] = new TriCent(col*triHeight + centerOffset1, row*sideLen/2,
col*triHeight, row*sideLen/2 - sideLen/2,
col*triHeight, row*sideLen/2 + sideLen/2,
col*triHeight + triHeight, row*sideLen/2);
}
}
}
class TriCent {
float centerX;
float centerY;
float voneX;
float voneY;
float vtwoX;
float vtwoY;
float vthreeX;
float vthreeY;
color dark = color(6, 0, 77);
color light = color(254, 254, 255);
color tone = color(252, 226, 207);
color back = color(233, 255, 232);
TriCent (float cX, float cY, float v1X, float v1Y, float v2X, float v2Y, float v3X, float v3Y) {
centerX = cX;
centerY = cY;
voneX = v1X;
voneY = v1Y;
vtwoX = v2X;
vtwoY = v2Y;
vthreeX = v3X;
vthreeY = v3Y;
}
float distTo (int x, int y) {
float dist = sqrt(sq(x-centerX)+sq(y-centerY));
return dist;
}
void drawTri (int x, int y) {
strokeJoin(ROUND);
color current = get(x, y);
if (current == back) {
stroke(dark);
strokeWeight(1);
fill(dark);
triangle(voneX, voneY, vtwoX, vtwoY, vthreeX, vthreeY);
}
else if (current == dark) {
stroke(light);
strokeWeight(1);
fill(light);
triangle(voneX, voneY, vtwoX, vtwoY, vthreeX, vthreeY);
}
else if (current == light) {
stroke(tone);
strokeWeight(1);
fill(tone);
triangle(voneX, voneY, vtwoX, vtwoY, vthreeX, vthreeY);
}
else {
stroke(240);
strokeWeight(1);
fill(back);
triangle(voneX, voneY, vtwoX, vtwoY, vthreeX, vthreeY);
}
}
}
boolean running = false;
void draw() { // this is run repeatedly.
if (mousePressed) {
running = true;
TriCent minDistTri = new TriCent(0, 0, 0, 0, 0, 0, 0, 0);
float lowestDist = grid[0][0].distTo(mouseX, mouseY);
for (int col = 0; col < grid[0].length; col++) {
for (int row = 0; row < grid.length - 1; row++) {
// finds closest triangle to change color
float currDist = grid[row][col].distTo(mouseX, mouseY);
if (currDist < lowestDist) {
lowestDist = currDist;
minDistTri = grid[row][col];
}
}
}
minDistTri.drawTri(mouseX, mouseY);
}
// delay(50);
}