xxxxxxxxxx
// Set the size of the triangles
const triangleSize = 30;
// Define the space between the triangles
const spacing = 0; // Set spacing to zero for no gaps between triangles
function setup() {
createCanvas(400, 400);
colorMode(HSB); // Use HSB color mode for smoother color transitions
noStroke(); // Remove the stroke for a cleaner look
initializeRotationDirections(); // Initialize the rotation directions
}
function initializeRotationDirections() {
// Calculate the number of triangles fitting in the canvas, adjusted to cover edges
let cols = ceil(width / triangleSize);
let rows = ceil(height / (triangleSize * sqrt(3)/2));
rotationDirections = [];
for (let i = 0; i < cols; i++) {
rotationDirections[i] = [];
for (let j = 0; j < rows; j++) {
rotationDirections[i][j] = random() < 0.5;
}
}
}
function draw() {
background(10); // Clear the background
drawPattern(); // Draw the pattern
}
function drawPattern() {
let offset = triangleSize * sqrt(3) / 2; // Vertical offset for the triangles
for (let i = 0; i < rotationDirections.length; i++) {
for (let j = 0; j < rotationDirections[i].length; j++) {
let x = i * triangleSize;
let y = j * offset;
// Alternate the starting position for each column to interlock the triangles
if (i % 2 == 0) {
y += offset / 2;
}
// Calculate the angle from the triangle center to the mouse position
let angle = atan2(mouseY - y, mouseX - x);
if (rotationDirections[i][j]) {
angle += PI; // Reverse the angle if the direction is true
}
drawRotatedTriangle(x, y, angle);
}
}
}
function drawRotatedTriangle(x, y, angle) {
push();
translate(x, y);
rotate(angle);
let hueValue = map(angle, -PI, PI, 0, 360);
fill(hueValue, 100, 100);
triangle(0, -triangleSize / 2, triangleSize / 2, triangleSize / 2, -triangleSize / 2, triangleSize / 2);
pop();
}
function mousePressed() {
initializeRotationDirections(); // Re-randomize rotation directions on mouse press
}