xxxxxxxxxx
158
const r = 8;
const curveRes = 50;
let c, f;
let clicked = null;
let moving = null;
const mindist = 8;
let {Vector} = p5;
let back;
function preload()
{
back = loadImage('road-1.png');
}
function setup() {
createCanvas(900, 900).parent('content');
doHTML();
f = new follower();
}
function draw() {
background(back);
c.show();
if (show.checked()) {
fill(0);
road(c, 5).show(c.evenlySpaced(1), c.isclosed);
}
f.follow(c, 10);
}
//function to generate quadratic bezier curve
function quadcurve(a, b, c, t) {
let p0 = p5.Vector.lerp(a, b, t);
let p1 = p5.Vector.lerp(b, c, t);
return p5.Vector.lerp(p0, p1, t);
}
//function to generate cubic bezier curve from two quadratic curves
function cubicCurve(a, b, c, d, t) {
let p0 = quadcurve(a, b, c, t);
let p1 = quadcurve(b, c, d, t);
return p5.Vector.lerp(p0, p1, t);
}
function mousePressed() {
mouse = createVector(mouseX, mouseY);
// this is for adding points
if (keyIsDown(SHIFT)) {
// get the closest point on the curve to the mouse
let close = null;
for (let i = 0; i < c.path.length; i++) {
let seg = c.path[i];
for (let j = 0; j < seg.length; j++) {
let p = seg[j];
if (p.dist(mouse) < mindist) {
close = [p, i];
}
}
}
// if there is a closest then add a point on the seg
if (close != null) {
c.Split(close[0], close[1]);
// if not add a regular point
} else {
c.addSeg(createVector(mouseX, mouseY));
}
}
// this is for removing points
else if (mouseButton === RIGHT || keyIsDown(82)) {
for (let i = 0; i < c.length; i++) {
let node = c[i];
if (node.dist(createVector(mouseX, mouseY)) < r) {
c.remove(i);
}
}
}
}
function mouseDragged() {
let mouse = createVector(mouseX, mouseY);
if(c.centerdist < r)
{
moving = null;
}
if((mouse.dist(c.center) < r*2 || moving != null) && c.centerdist > r && clicked == null)
{
moving = true;
let dif = Vector.sub(c.center, mouse);
for(let i = 0; i < c.length; i++)
{
c[i].sub(dif);
}
}
else if (clicked == null) {
for (let i = 0; i < c.length; i++) {
let node = c[i];
if (node.dist(mouse) < r) {
clicked = i;
}
}
}
if (clicked != null) {
c.movePoint(clicked, mouse);
}
}
function mouseReleased() {
clicked = null;
moving = null;
}
function road(path, spacing, w = 50)
{
// let w = 100;
let points = path.evenlySpaced(spacing);
let verts = new Array(points.length*2);
let numTris = 2*(points.length-1) + ((path.isclosed)?2:0);
let tris = new Array(numTris*3);
let vertindex = 0;
let triIndex = 0;
for(let i = 0; i < points.length; i++)
{
let forward = createVector();
if(i < points.length-1 || path.isclosed)
{
forward.add(points[(i+1)%points.length].copy().sub(points[i].copy()));
}
else if(i > 0 || path.isclosed)
{
forward.add(points[i].copy().sub(points[(i-1+points.length)%points.length].copy()));
}
forward.normalize();
let left = createVector(-forward.y, forward.x);
verts[vertindex] = points[i].copy().add(left.copy().mult(w/2));
verts[vertindex+1] = points[i].copy().sub(left.copy().mult(w/2));
if(i < points.length-1 || path.isclosed)
{
tris[triIndex] = vertindex;
tris[triIndex+1] = (vertindex+2)%verts.length;
tris[triIndex+2] = vertindex+1;
tris[triIndex+3] = vertindex+1;
tris[triIndex+4] = (vertindex+2)%verts.length;
tris[triIndex+5] = (vertindex+3)%verts.length;
}
vertindex += 2;
triIndex+=6;
}
return new mesh(verts, tris, points);
}