I have an array containing 6 sets of random x, y coordinates. I'm able to draw lines connecting the points, like this:
But, I'm having trouble figuring out how to draw repeating squares (to achieve a dotted line effect) where the lines are. Right now, I'm able to get this, but I want to only draw squares along the line only:
I understand what I'm doing wrong theoretically, but really struggling with the math here. How do I go about only including the squares that appear along the lines? TIA.
This is my code right now:
let w = 500;
let h = 500;
let riverArr = [];
let distanceArr = [];
function setup() {
createCanvas(w, h);
}
function draw() {
background(240);
generatePoints();
for (i = 1; i < riverArr.length; i++) {
let x1 = parseInt(riverArr[i][0]);
let y1 = parseInt(riverArr[i][1]);
let x2 = parseInt(riverArr[i - 1][0]);
let y2 = parseInt(riverArr[i - 1][1]);
line(x1, y1, x2, y2);
let d = int(dist(x1, y1, x2, y2));
ellipse(x1, y1, 5);
for (j = 0; j <= x1; j++) {
for (k = 0; k <= y1; k++){
rect(j, k, 1);
}
}
distanceArr.push(d);
}
noLoop();
}
function generatePoints() {
let finished;
riverArr.push([0, random(h)]);
for (i = 0; i < 4; i++) {
finished == false;
riverArr.push([random(w), random(h)]);
if (i > 0 && riverArr[i][0] <= riverArr[i - 1][0]) {
console.log(riverArr[i][0], riverArr[i - 1][0]);
console.log('Bad path');
}
finished == true;
}
riverArr.push([w, random(h)]);
}