xxxxxxxxxx
188
const pts = []
var size = 0.6;
let rVal = 0;
let alpha = 255;
let left = 0;
let right = 0;
function preload () {
img0 = loadImage("mex1.png")
}
function setup() {
createCanvas(1200, 600);
textSize(18);
for (var i = 0; i < country.length; i++) {
country[i].polygons = convertPathToPolygons(country[i].vertexPoint);
console.log(country[i].name);
console.log(country[i].polygons);
}
setUpSerial();
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial(SELECT_PORT);
}
}
function draw() {
background(map(rVal, 0, 1023, 0, 255), 255, 255);
console.log(rVal)
fill(100);
stroke(255);
strokeWeight(1);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
for (var i = 0; i < country.length; i++) {
if (country[i].name == "China") {
fill('red');
}
else if (country[i].name == "Mexico") {
fill('red');
}
else if (country[i].name == "Sri Lanka") {
fill('red');
}
else if (country[i].name == "France") {
fill('red');
}
else if (country[i].name == "Palestine") {
fill('red');
}
else if (country[i].name == "Ethiopia") {
fill('red');
}
else if (country[i].name == "Palestine 1") {
fill('red');
}
else {
fill('gray');
}
var coord_point = [0, 0];
//For loop to calculate the vertex
for (var k = 0; k < country[i].vertexPoint.length; k++) {
if (country[i].vertexPoint[k][0] == "m") {
coord_point[0] += country[i].vertexPoint[k][1] * size;
coord_point[1] += country[i].vertexPoint[k][2] * size;
beginShape();
continue;
}
if (country[i].vertexPoint[k][0] == "M") {
coord_point[0] = country[i].vertexPoint[k][1] * size;
coord_point[1] = country[i].vertexPoint[k][2] * size;
beginShape();
continue;
}
if (country[i].vertexPoint[k] == "z") {
vertex(coord_point[0], coord_point[1]);
endShape();
continue;
}
vertex(coord_point[0], coord_point[1]);
coord_point[0] += country[i].vertexPoint[k][0] * size;
coord_point[1] += country[i].vertexPoint[k][1] * size;
}
}
// if (rVal > 200 ) {
// image(img0, 0, 0, width, height);
// }
}
// This is very similar to your existing drawing code, but instead of actually drawing with beginShape/vertex/endShape this pushes vertices into an array, and then starts a new array each time it sees the "z" command.
function convertPathToPolygons(path) {
let coord_point = [0, 0];
let polygons = [];
let currentPolygon = [];
//For loop para calcular os pontos do vertex
for (const node of path) {
if (node[0] == "m") {
coord_point[0] += node[1] * size;
coord_point[1] += node[2] * size;
currentPolygon = [];
} else if (node[0] == "M") {
coord_point[0] = node[1] * size;
coord_point[1] = node[2] * size;
currentPolygon = [];
} else if (node == "z") {
currentPolygon.push([coord_point]);
polygons.push(currentPolygon);
} else {
currentPolygon.push([coord_point]);
coord_point[0] += node[0] * size;
coord_point[1] += node[1] * size;
}
}
return polygons;
}
function pointInPoly(verts, pt) {
let c = false;
// for each edge of the polygon
for (let i = 0, j = verts.length - 1; i < verts.length; j = i++) {
// Compute the slope of the edge
let slope = (verts[j][1] - verts[i][1]) / (verts[j][0] - verts[i][0]);
// If the mouse is positioned within the vertical bounds of the edge
if (((verts[i][1] > pt.y) != (verts[j][1] > pt.y)) &&
// And it is far enough to the right that a horizontal line from the
// left edge of the screen to the mouse would cross the edge
(pt.x > (pt.y - verts[i][1]) / slope + verts[i][0])) {
// Flip the flag
c = !c;
}
}
return c;
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 1) {
// only store values here
// do everything with those values in the main draw loop
rVal = fromArduino[0];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "\n";
writeSerial(sendToArduino);
}
}