I came across this piece of C code (I think) that's supposed to be a neat way to check if a point is within a concave
or convex
polygon, and I would like to convert it to a JS equivalent function to use in my JS program:
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
nvert: Number of vertices in the polygon. Whether to repeat the first vertex at the end.
vertx, verty: Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy: X- and y-coordinate of the test point.
Code above taken from this Stack Overflow question.
How would this translate into JS? I've already found out how I can start the for-loop in JS
j = nvert-1
for (i = 0; i < nvert; i++) {
//The whole if thing
j = i
}
And I guess that the "float *"s in the first row can just be omitted in JS. But I'm not quite sure what the "int i, j, c = 0;" does or what "!c" means when "c = 0". What's the opposite of 0?
Thanks!