Search code examples
coperatorspolygonconvex-polygon

What is the purpose of "!" in this code from "Point in Polygon Test"?


Point in Polygon Test Code

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;
}

What is the purpose of ! in c = !c; ?


Solution

  • Any non-zero number or non-null pointer is considered to have a logical value of "true", whereas a number with value 0 or a null pointer is considered to have a logical value of "false".

    The ! operator is the logical NOT operator (as opposed to a bit-wise NOT operator). It inverts the logical value of its operand, producing the integer value 0 if the operand has a logical value of "true", and the value 1 if the operand has a logical value of "false".