Hi All,
Im completely new here and to code tbh, I followed this tutorial on a gift wrapping algorithm, which of course was fairly easy to follow, problems arise when i want to do something different to the tutorial. The code is bellow.
const points = [];
const hull = [];
let leftMost;
let currentVertex;
let index = 0;
let nextIndex = -1;
let nextVertex;
function setup() {
createCanvas(500, 500);
let buffer = 50;
for (let i = 0; i < 7; i++) {
points.push(createVector(random(buffer, width - buffer), random(buffer, height - buffer)));
}
points.sort((a, b) => a.x - b.x);
leftMost = points[0];
currentVertex = leftMost;
hull.push(currentVertex);
nextVertex = points[1];
index = 2;
}
function draw() {
background(0);
stroke(255);
strokeWeight(8);
for (let p of points) {
point(p.x, p.y);
}
stroke(3, 198, 252, 95);
strokeWeight(2);
fill(3, 198, 252, 60);
beginShape();
for (let p of hull) {
vertex(p.x, p.y);
}
endShape(CLOSE);
stroke(0, 255, 0);
point(leftMost.x, leftMost.y);
stroke(0, 0, 255);
strokeWeight(4);
point(currentVertex.x, currentVertex.y);
stroke(255);
strokeWeight(2);
line(currentVertex.x, currentVertex.y, nextVertex.x, nextVertex.y);
let checking = points[index];
stroke(0, 255, 0);
line(currentVertex.x, currentVertex.y, checking.x, checking.y);
const a = p5.Vector.sub(nextVertex, currentVertex);
const b = p5.Vector.sub(checking, currentVertex);
const cross = a.cross(b);
if (cross.z < 0) {
nextVertex = checking;
nextIndex = index;
}
index = index + 1;
if (index == points.length) {
if (nextVertex == leftMost) {
console.log('done');
noLoop();
} else {
hull.push(nextVertex);
currentVertex = nextVertex;
index = 0;
//points.splice(nextIndex, 1);
nextVertex = leftMost;
}
}
}
What I am trying to do is to change the colour of the stroke line for the current line and checking lines, when the hull has completed, so there basically isn’t a green line and a white line once the program completes?
what I am thinking is change the stroke alpha on them line to 0 so they have no opacity? but i cant seem to figure out how change the values from when it is initially set.
My p5sketch link also - https://editor.p5js.org/FireMe/sketches/N7TUVV6t9
Kind regards