The question is: how do you determine if a point is within a polygon?
This question has been asked and answered many times. There are multiple methods for determining whether a point is within a polygon.
I've grokked the Winding Number algorithm, ported a solid answer from another SO thread into C# and written xUnit tests around it to ensure that I could refactor ruthlessly. The goal was to take an answer, all of which seem to use a procedural programming approach and variable names that are similar to those you'd find in a mathematical formula, and refactor it into a reasonably sound set of OOP classes and methods.
So, to rephrase this question specifically to the answer that I will go on to provide:
How do I determine if a location / point (latitude and longitude) is within a polygon in OOP C#?
The answer that I used as my starting point was provided by Manuel Castro in the following SO thread Geo Fencing - point inside/outside polygon :
I proceeded to write xUnit tests around an implementation that started out using the exact logic expressed in the above code. The following are a bit overkill, but I preferred more tests to ensure that refactoring would not create issues. Using inline data in xUnit theories is so easy, eh, why not. With all the tests green, I was then able to refactor to my heart's content:
This allowed me to refactor the original code to the following:
The original code, of course, is far less verbose. However, it:
The refactored code:
And:
Now, all that said, I'm not saying that there are no additional refactors that might be suggested, or that the above refactor approaches perfection. However, I think that in terms of implementing the Winding Number algorithm for determining whether a point is in a polygon and really understanding the algorithm that this is helpful.
I hope that this helps some who, like me, had some difficulty wrapping their heads around it.
Cheers