<p>While my first answer should do the trick for this simple problem, I can't help but mention that there exist general techniques for dealing with these kinds of special cases.</p>

<p><a href="http://arxiv.org/pdf/math/9410209.pdf" rel="nofollow">This article</a> describes a technique for dealing with these kinds of issues in general. And one of the first examples they provide happens to be the algorithm you ask about!</p>

<p>The idea is to apply <a href="http://en.wikipedia.org/wiki/Automatic_differentiation" rel="nofollow">Automatic differentiation</a> aka <a href="http://en.wikipedia.org/wiki/Dual_number" rel="nofollow">Dual numbers</a> to compute symbolic perturbations.</p>

<p>By the way the same technique can also be used to avoid handling 0/0 as a special case in programs!</p>

<p><a href="http://blog.sigfpe.com/2008/05/desingularisation-and-its-applications.html" rel="nofollow">Here</a> is the blog post I originally learned this from, it gives some great background to the technique, and the author blogs a lot about automatic differentiation (AD).</p>

<p>Despite appearances AD is a very practical technique especially in languages with good support for operator overloading (eg: C++, Haskell, Python ...) and I have used it in "real life" (industrial applications in C++).</p>
"ray through vertex" special case when detecting point in polygon
<p>To detect if a point is in a polygon, you project a line from the point, to infinity, and see how many of polygon's vertices it intersects with... simple enough.  My problem is that if the ray intersects the polygon on one of the points, then it is counted as intersecting two segments, and considered outside the polygon.  I altered my function to make it only count one of the segments when the ray intersects a point of the polygon, but there are cases where a line could intersect the point while still being outside as well.  Take this image as an example:</p>

<p><img src="https://i.stack.imgur.com/CQlsY.png" alt="two examples of a ray crossing a polygon vertex"></p>

<p>If you assume the point in the top left is "infinity", and cast a ray to either of the other points, both intersect at a point of the polygon, and would count as intersecting the same number of vertices even though one is inside, and one is outside.  </p>

<p>Is there a way to compensate for that, or do I just have to assume that those fringe cases won't pop up?</p>