Skip to content

Instantly share code, notes, and snippets.

@Damercy
Created November 9, 2020 19:36
Show Gist options
  • Save Damercy/923ccf5d9bb1e649b8954795cedcc3e9 to your computer and use it in GitHub Desktop.
Save Damercy/923ccf5d9bb1e649b8954795cedcc3e9 to your computer and use it in GitHub Desktop.
let nodes = [];
let nodesBegin = [];
let nodesEnd = [];
let nodesDist = [];
let paths = [];
let filtered = [];
let exponent = 4; // Determines the curve
let x = 0.0; // Current x-coordinate
let y = 0.0; // Current y-coordinate
let step = 0.1; // Size of each step along the path
let pct = 0.0;
function setup() {
createCanvas(600, 400);
frameRate(20);
noStroke();
const numNodes = 7;
for (let i = 0; i < numNodes; i++)
nodes.push(new Node());
}
function calculateDist() {
for (let i = 0; i < nodesBegin.length; i++)
nodesDist.push({
x: nodesEnd[i].x - nodesBegin[i].x,
y: nodesEnd[i].y - nodesBegin[i].y
});
}
function draw() {
fill(0, 10);
rect(0, 0, width, height);
nodes.forEach((node, idx) => {
node.draw();
node.distBetweenNode(nodes.slice(idx), idx);
});
calculateDist();
for (let i = 0; i < nodes.length; i++) {
pct += step;
console.log(pct);
if (pct < 1.0) {
x = nodesBegin[i].x + pct * nodesDist[i].x;
y = nodesBegin[i].y + pow(pct, exponent) * nodesDist[i].y;
} else
pct = 0.0;
fill(255);
ellipse(x, y, 5, 5);
}
}
class Node {
constructor() {
this.pos = createVector(random(300), random(200));
this.size = 40;
}
draw() {
fill("orange");
noStroke();
circle(this.pos.x, this.pos.y, this.size);
}
distBetweenNode(nodes, idx) {
let distance;
nodes.forEach(node => {
if (nodesBegin.length <= nodes.length)
nodesBegin.push({
x: node.pos.x,
y: node.pos.y
});
distance = dist(this.pos.x, this.pos.y, node.pos.x, node.pos.y);
if (distance != 0 && distance > this.size) {
if (nodesEnd.length != nodes.length - 1) {
nodesEnd.push({
x: node.pos.x,
y: node.pos.y
});
}
}
});
}
}
/*
let beginX = 20.0; // Initial x-coordinate
let beginY = 10.0; // Initial y-coordinate
let endX = 570.0; // Final x-coordinate
let endY = 320.0; // Final y-coordinate
let distX; // X-axis distance to move
let distY; // Y-axis distance to move
let exponent = 4; // Determines the curve
let x = 0.0; // Current x-coordinate
let y = 0.0; // Current y-coordinate
let step = 0.01; // Size of each step along the path
let pct = 0.0; // Percentage traveled (0.0 to 1.0)
function setup() {
createCanvas(720, 400);
noStroke();
distX = endX - beginX;
distY = endY - beginY;
}
function draw() {
fill(0, 2);
rect(0, 0, width, height);
}
function mousePressed() {
pct = 0.0;
beginX = x;
beginY = y;
endX = mouseX;
endY = mouseY;
distX = endX - beginX;
distY = endY - beginY;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment