xxxxxxxxxx
136
//all points (one point per client)
var points = [];
//points on the hull
var hullPoints = [];
var leftMost;
//current vertex on hull
var currentHullPoint;
//current point we're checking
var index;
var nextIndex = -1;
var nextPoint;
var checking;
var clients = {};
var cursors = [];
var id;
//var socket = io();
var lastEmit, now;
var Lines = [];
var cursor;
var inactivityThreshold;
var drawing;
//=================================================================
function setup() {
createCanvas(windowWidth, windowHeight);
console.log("setup");
prev = {};
inactivityThreshold = 60000;
lastEmit = 0;
drawing = 1.0;
//console.log(clients);
//create vector list for client mouse positions
for (let i = 0; i < 5; i++) {
points.push(createVector(random(windowWidth), random(windowHeight)));
//console.log(points);
}
leftMost = findLeftMost(points);
//console.log(leftMost);
currentHullPoint = leftMost;
nextPoint = points[1];
index = 2;
}
//=================================================================
function draw() {
background(255);
now = millis();
var myPoint = createVector(mouseX, mouseY);
points.push(myPoint);
//calculateHull
for (point in points) {
var hullPoints = calculateHull(leftMost, points);
}
//draw hull
drawHull(hullPoints);
for (point in points) {
ellipse(points[point].x, points[point].y, 5, 5);
}
//draw the points
for (var c in clients) {
var cx = clients[c].x;
var cy = clients[c].y;
fill(255);
ellipse(cx, cy, 10, 10);
fill(0);
ellipse(mouseX, mouseY, 10, 10);
}
}
//=================================================================
function findLeftMost(points) {
//find leftmost point by comparing each client's x-val
points.sort((a, b) => a.x - b.x);
leftMost = points[0];
hullPoints.push(leftMost);
//console.log("leftmost:", leftMost);
return leftMost;
}
//=================================================================
function calculateHull(leftMost, points) {
//calculates hull and returns a list
let checking = points[index];
const a = p5.Vector.sub(nextPoint, currentHullPoint);
const b = p5.Vector.sub(checking, currentHullPoint);
const crossProd = a.cross(b);
if (crossProd.z < 0) {
nextPoint = checking;
nextIndex = index;
}
index += 1;
if (index == points.length) {
hullPoints.push(nextPoint);
currentHullPoint = nextPoint;
index = 0;
nextPoint = leftMost;
}
//console.log(hullPoints);
return hullPoints;
}
//=================================================================
function drawHull(hullPoints) {
//for(var j = 0; j< hullPoints.length; j++) {
//line(hullPoints[j].x, hullPoints[j].y, hullPoints[j+1].x,hullPoints[j+1].y);
//}
stroke(0, 0, 255);
fill(0, 0, 255, 50);
beginShape();
for (let p of hullPoints) {
vertex(p.x, p.y);
}
endShape();
}
function keyPressed(){
points = [];
}