I thought that circle 2 would be drawn where circle 4 is. Although then line 3 would be on top of that circle.
The problem is that both the lines and the circles are in the same loop. If I put them in separate loops then the problem would be fixed. But is there a way to fix the problem while still keeping them in the same loop?
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
background(50);
}
function draw() {
for (let i = 0; i < 3; i++) {
let x = 100 * cos(120 * i) + width / 2;
let y = 100 * sin(120 * i) + height / 2;
stroke(100 * i);
strokeWeight(5 * i + 1);
line(
x,
y,
100 * cos(120 * (i + 1)) + width / 2,
100 * sin(120 * (i + 1)) + height / 2
);
fill(50);
circle(x, y, 100);
}
}
Most of it
I guess by looking at it more I kind of understand, but just don’t see why write it like that. It’s more confusing for me.
I managed to just add the createGraphics() stuff to my previous code though:
let pg;
function setup() {
createCanvas(400, 400);
pg = createGraphics(400, 400);
angleMode(DEGREES);
background(50);
}
function draw() {
image(pg, 0, 0);
for (let i = 0; i < 3; i++) {
let x = 100 * cos(120 * i) + width / 2;
let y = 100 * sin(120 * i) + height / 2;
pg.stroke(100 * i);
pg.strokeWeight(5 * i + 1);
pg.line(
x,
y,
100 * cos(120 * (i + 1)) + width / 2,
100 * sin(120 * (i + 1)) + height / 2
);
stroke(100 * i);
strokeWeight(5 * i + 1);
fill(50);
circle(x, y, 100);
}
}
It works here, but am having a bit of issues when introducing it to the rest of the sketch I’m working on. Things not working as expected when running as animation. And also things were looking a bit pixelated. Maybe pixel density issue.
I’ll keep playing with it and see what I manage. Thanks!
Ohhh, I see. Will that make a considerable difference in performance?
Also, why you do the chaining with the dots? Like fill(50).stroke(200).strokeWeight(2)? Is there a benefit to doing it this way instead of just one below the other without chaining with dots?
And what about the >>? I know those are bitwise operators, but how do they actually work?
Would width >> 1 be the same as width / 2?
The only way is to calculate the intersection points between the lines and the circles and then draw the circles and truncated lines in one loop. Unless you have millions of circles and lines to draw then using an offscreen buffer with createGraphics or calculating the intersection points will swamp any benefit to be had from using a single loop.
So in this instance the most efficient way is simply to draw all lines in one loop and the circles in a second (later) loop.