xxxxxxxxxx
113
const pts = []
var size = 0.6;
function setup() {
createCanvas(1200, 600);
}
function draw() {
fill(100);
stroke(255);
strokeWeight(1);
for (var i = 0; i < country.length; i++) {
var coord_point = [0, 0];
convertPathToPolygons(country[i].vertexPoint);
//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;
}
}
}
// 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;
}
}
//console.log(polygons);
return polygons;
}
function pointInPoly(verts, pt) {
intersections = [];
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])) {
// To help visualize the algorithm.
//intersections.push({ x: (pt.y - verts[i].y) / slope + verts[i].x, y: mouseY });
// Flip the flag
c = !c;
}
}
return c;
}