I was wondering if any of you could help me by explaining, how to keep the displacement & rotation of the smileys, but make the top of every 2. row hit the middle of the upper row.
See example on my sketch (the blue one)
And this is my code
import processing.pdf.*;
PShape smiley;
float step;
float antal = 10;
int k = 1;
float [] startAngles = new float[100];
boolean startRecord = false;
boolean slutRecord = false;
void setup() {
size(1000, 1000);
frameRate(30);
smooth();
for (int i = 0; i < 100; i++) {
startAngles[i] = random(10);
}
background(251, 240, 255);
step = width/antal;
smiley = loadShape("smiley-03.svg");
}
void draw() {
background(251, 240, 255);
if (startRecord) {
beginRecord(PDF, "smiley###.pdf");
startRecord = false;
slutRecord = true;
}
shapeMode(CENTER);
smiley.disableStyle();
fill(255, 0, 0);
noStroke();
for (int j = 0; j < antal; j ++ ) {
for (int i = 0; i < antal; i ++) {
if (j%2 == 0) {
pushMatrix();
translate(i*step, j*step);
rotate(startAngles[i*j]+k*PI/15);
shape(smiley, 0, 0, 100, 100);
popMatrix();
}
if (j%2 == 1) {
pushMatrix();
translate(i*step+step/2, j*step);
rotate(startAngles[i*j]+k*PI/15);
shape(smiley, 0, 0, 100, 100);
popMatrix();
}
}
}
k++;
if (slutRecord) {
endRecord();
}
}
void keyPressed() {
if (key == 's') {
startRecord =true;
}
}
You’re close. Notice that the pattern creates tiles of isosceles triangles. Then the question becomes what is the width and height offset from one point of the isosceles triangle to another:
Each angle in the isosceles triangle is 60 degrees. You got the horizontal offset correct. Using trigonometry:
Now I’m struggling how to get every second row of smileys, to have 11 smileys, so it goes all the way to the width of the canvas.
I tried to change antalX to 11, but that just makes 11 smiley in the row, instead of the previous 10.
In addition to changing antalX, you need to stride past the edge of the width. Without refactoring your loops, that requires your strides to add up to one more than the width of the screen – on every row. So update this line as well
stepX = width/(antalX-1);
Note that you could also use this approach to stepY.
A drawback in general is that you are storing your rotations in an array. This makes your code brittle, because any changes that increases the number of smileys could break the sketch because you the rotations will no longer match. You should size that array dynamically
startAngles = new float[antalX*antalY];
…or not use it at all, and compute the values on the fly?