Hm I tried looking for an example but I can't seem to find one.
I do not completely understand how this thing is working, I mean I understand the principle but not how I can use it in my case. For me they are for example not very clear about where I need to fill in the point it checks and where the points are filled in that make up the polygon.
As a first stab, could you do a Sutherland-Cohen style clipping operation, to trivially exclude points outside the minimum bounding box, then start to get clever with triangles?
I guess one question you have to ask is whether or not you can treat the area as a plane, or if spherical geometry needs to be considered.
From the link from guix, consider a polygon that has co-ordinates like below ( 0,0 top left, clockwise vertices. )
__|
0,0
5,0
5,5
2,5
You would setup that function like:
// int polySides = how many corners the polygon has
// float polyX[] = horizontal coordinates of corners
// float polyY[] = vertical coordinates of corners
// float x, y = point to be tested
int polySides = 4;
float polyX[] = { 0, 5, 5, 2 };
float polyY[] = { 0, 0, 5, 5 };
float x, y; //What ever point GPS provides.
Y
^
|
| A B
| /------------|
| / |
| /______________|
| D C
+--------------------------------------> X
X ; Y
A { 3.0 ; 4.0 }
B { 7.0 ; 4.0 }
C { 7.0 ; 2.0 }
D { 2.0 ; 2.0 }
I'm very relieved that guix responded to lcj's "programming in arduino" question with a link to a site which presents a technique and not just a pile of code.
All too often over the last 30+ years I've seen programmers diving into code without thinking about what it is they're actually trying to do.
lcj's post is not a programming question (less yet a programming in Arduino question)- it's a topology question, one solution of which which happens to be implemented with some code.
^
|
| A B
| /------------|
| / |
| /______________|
| D C
+--------------------------------------> X
X ; Y
A { 3.0 ; 4.0 }
B { 7.0 ; 4.0 }
C { 7.0 ; 2.0 }
D { 2.0 ; 2.0 }
Thanks a lot for this wonderfull pice of code. It's working as expected with a gps receiver on arduino. I just have to figure it out how make a notification when entered in the polygon and when exit.
BTW, the code fails when segment AD is nearly vertical. There's got to be a better behaved snippet somewhere... This problem is called the Point in Polygon test. Links from that wiki page lead to a C++ implementation with the same problem.
As is clearly obvious to the most casual observer, it is a small matter of programming to replace the problematic code with the correct line segment test, and is best left as an exercise for the reader.
I tried to make one for myself and it worked perfectly, now the problem I encountered is when I want two polygons and test whether the point is in polygon 1 or polygon two...
what alteration/addition in the code should i make?