Point in Polygon algorithm - Why does it work? I'm using the following algorithm (in C) to find if a point lays within a given polygon
typedef struct {
   int h,v;
} Point;

int InsidePolygon(Point *polygon,int n,Point p)
{
   int i;
   double angle=0;
   Point p1,p2;

   for (i=0;i<n;i++) {
      p1.h = polygon[i].h - p.h;
      p1.v = polygon[i].v - p.v;
      p2.h = polygon[(i+1)%n].h - p.h;
      p2.v = polygon[(i+1)%n].v - p.v;
      angle += Angle2D(p1.h,p1.v,p2.h,p2.v);
   }

   if (ABS(angle) < PI)
      return(FALSE);
   else
      return(TRUE);
}

/*
   Return the angle between two vectors on a plane
   The angle is from vector 1 to vector 2, positive anticlockwise
   The result is between -pi -> pi
*/
double Angle2D(double x1, double y1, double x2, double y2)
{
   double dtheta,theta1,theta2;

   theta1 = atan2(y1,x1);
   theta2 = atan2(y2,x2);
   dtheta = theta2 - theta1;
   while (dtheta > PI)
      dtheta -= TWOPI;
   while (dtheta < -PI)
      dtheta += TWOPI;

   return(dtheta);
}

I found the algorithm while searching online for such an algorithm, Point in Polygon algorithm, Solution 2 (2D).


*

*The explanation in the link says it checks if the sum of angles is $2\pi$ but the algorithm (which works) checks it with $\pi$.

*How are the angles even calculated?


The angle is computed with the atan2 function (see
https://en.wikipedia.org/wiki/Arctan2). But the code is not computationally
robust. The mathematical idea may be OK, but since $\pi$ and $2\pi$ are not exactly
representable as floating point numbers, there will be uncertainties produced by using floating point arithmetic near the interesting sum $2\pi$. Testing equality of two floating point variables is a questionable concept in many situations (since the angle is in the interval  $[-\pi, +\pi],\,$ the test $|\, . |<\pi\,$ is actually an equality test in disguise).
And there is a bug, if the sum happens to be $\pm\pi,$ the code should handle this separately.
