Hi, ive recently found this point in polygon code on a website that i can no longer find, and i was wondering if it was possible if someone could explain the code and if possible break it down for me so i can understand just how a single line is created between two point. i am using this for a geofenced rc car but i do not understand it therefore cannot troubleshoot or try to change anything without breaking my whole code.
here is the code and please just assume each variable in it is already declared.
If you want to know more you should Read about the Jordan curve theorem and the Ray casting algorithm.
The idea is based on counting the number of time a Ray from your test point is crossing the polygon boundaries. If you have a test point (Px,Py), a possible Ray is to test either along the X axis (Py constant, any x in one direction) or Y axis (Px constant, any y in one direction)
Typical algorithm use a comparison to flip a boolean to count the number of hits but You will find the optimization technique with XOR in this article (third code)
Usually one adds first à bounding box test, looking at the min and max of each vertex X and Y. If the point you test is not within this encompassing rectangle, then the point is outside the polygon
thanks that helped. now i have worked that part out im just having one other problem.
here is the full sample of code but within it when it print latitude and [j] and longitude and [j] all i get is 0.000000 for all of them. can anyone tell me what im doing wrong? ``` *void testInPoly() {
Did you read the links I pointed at in the first answer ?
A polygon is defined by a sequential list of vertexes (some say also vertices, in the same way the plural of index can be indices...)
So your polygon is defined in an array of (X,Y) coordinates that you have to pre-fill before calling the function. They will define the “border” of your hit area.
If you have only 4 vertexes (with four edges and four vertexes ) you have a quadrilateral - some of which have a specific name depending on the vertexes' geometric properties relative to each others (square, rhombus, parallelogram, trapezoid, rectangle,...)
If you have a plain rectangle with sides parallel to the axis of your coordinate system then checking if à point is within this rectangle does not need complex iteration. Find the min and max of X and Y of your 4 corners and check if your hit point is in between those min and max values