Discussion:
Locating a position on a canvas
TJ Hanson
2016-07-29 18:22:33 UTC
Permalink
I’ve drawn a polygon on a canvas. For simplicity assume the x axis is longitude and the y axis is latitude. The polygon can have any shape, with 3 to many sides.

Now I have a point to plot. The point is designated with x,y coordinates (or in this case, lat/long coordinates).

I want to plot this point only if it falls within the polygon.

How can I tell if I am inside the polygon?

This may sound like an easy problem, but I’ve spent the last 24 hours looking at it, and it keeps getting more complicated. Especially since the polygon can take on any shape. For example:

|
| /———|
| / | Given three points I can see that only one falls inside the polygon. But
| • / | ——| I need to be able to figure that out programatically.
| / |
| / / —-\ • |
| / / • \ |
| /——/ \——|
|—————————————-

Any ideas would be greatly appreciated.

TJ





_______________________________________________
Unsubscribe by sending a message to:
<nug-lea
Markus Winter
2016-07-29 18:32:01 UTC
Permalink
Of the top of my head:

Draw the polygon filled in red on a second picture.

Check wether the colour of the pixel is red at that position.

MfG

Markus

Sent from my iPad

_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Lars Jensen
2016-07-31 00:38:46 UTC
Permalink
I'd start here:

https://en.wikipedia.org/wiki/Point_in_polygon

On Fri, Jul 29, 2016 at 2:32 PM, Markus Winter <
Post by Markus Winter
Draw the polygon filled in red on a second picture.
Check wether the colour of the pixel is red at that position.
MfG
Markus
Sent from my iPad
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Markus Winter
2016-07-31 06:14:38 UTC
Permalink
Post by Lars Jensen
https://en.wikipedia.org/wiki/Point_in_polygon
Mine is easier ;-)

MfG

Markus

Sent from my iPad
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Markus Winter
2016-07-31 06:19:12 UTC
Permalink
Or you could simply google for a solution and you quickly find (took me under 20 sec)

http://nug.xojo.narkive.com/J1tl3weA/implementing-a-polygon-contains-method

MfG

Markus

Sent from my iPad
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Giulio Mastrosanti
2016-07-31 10:08:32 UTC
Permalink
Can’t understand why sometimes we (me too…)
are looking for more complicated solutions in the name of… what?

I think that Markus idea is wonderfully simple, and it should work just fine…

there’s some counterpart problem in this approach, or is just too much simple? :)

Giulio



<http://www.bitbazar.com/>
Post by Markus Winter
Post by Lars Jensen
https://en.wikipedia.org/wiki/Point_in_polygon
Mine is easier ;-)
MfG
Markus
Sent from my iPad
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <listhe
Markus Winter
2016-07-31 10:20:49 UTC
Permalink
Post by Giulio Mastrosanti
Can’t understand why sometimes we (me too…)
are looking for more complicated solutions in the name of… what?
Elegance ;-)

But sometimes it is more efficient to be less elegant.

Take care

MfG

Markus

Sent from my iPad
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.co
Trochoid
2016-08-01 00:13:37 UTC
Permalink
My preference would be the math because it's less expensive than drawing a
bunch of pixels (untested), can be tested from the top polygon down while
drawing has to draw all polys from bottom up, and it's more direct to call
IsPointInPoly(pnt, poly) than make a picture, draw, test.

That being said there are 2 ways to fill a polygon, using the non-zero
winding rule or the even-odd rule. Xojo uses non-zero winding but the only
example algorithms I can find are for even-odd. Both rules give the same
result unless the polygon is self intersecting in which case it can vary
depending how it intersects.

Here's the even-odd algorithm translated to Xojo using Realbasic.Points.
I'd like to find an example of the non-zero winding rule if anyone comes
across it :)

Function pointInPoly(p As REALbasic.Point, poly() As REALbasic.Point) As
boolean
//from https://en.wikipedia.org/wiki/Even-odd_rule

dim last As integer = poly.Ubound
dim i As integer
dim j As integer = last
dim c As boolean = false

for i = 0 to last

if ( (poly(i).Y > p.Y) <> (poly(j).Y > p.Y) ) then
if p.X < (poly(j).X-poly(i).X) * (p.Y-poly(i).Y) /
(poly(j).Y-poly(i).Y) + poly(i).X then
c = not c
end
end

j = i
next

return c

End Function


On Sun, Jul 31, 2016 at 3:20 AM, Markus Winter <
Post by Markus Winter
Post by Giulio Mastrosanti
Can’t understand why sometimes we (me too…)
are looking for more complicated solutions in the name of… what?
Elegance ;-)
But sometimes it is more efficient to be less elegant.
Take care
MfG
Markus
Sent from my iPad
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List H
Trochoid
2016-08-01 00:16:55 UTC
Permalink
Oh, I think I found a non-zero winding source example at the end of this
page
http://geomalgorithms.com/a03-_inclusion.html
Post by Trochoid
My preference would be the math because it's less expensive than drawing a
bunch of pixels (untested), can be tested from the top polygon down while
drawing has to draw all polys from bottom up, and it's more direct to call
IsPointInPoly(pnt, poly) than make a picture, draw, test.
That being said there are 2 ways to fill a polygon, using the non-zero
winding rule or the even-odd rule. Xojo uses non-zero winding but the only
example algorithms I can find are for even-odd. Both rules give the same
result unless the polygon is self intersecting in which case it can vary
depending how it intersects.
Here's the even-odd algorithm translated to Xojo using Realbasic.Points.
I'd like to find an example of the non-zero winding rule if anyone comes
across it :)
Function pointInPoly(p As REALbasic.Point, poly() As REALbasic.Point) As
boolean
//from https://en.wikipedia.org/wiki/Even-odd_rule
dim last As integer = poly.Ubound
dim i As integer
dim j As integer = last
dim c As boolean = false
for i = 0 to last
if ( (poly(i).Y > p.Y) <> (poly(j).Y > p.Y) ) then
if p.X < (poly(j).X-poly(i).X) * (p.Y-poly(i).Y) /
(poly(j).Y-poly(i).Y) + poly(i).X then
c = not c
end
end
j = i
next
return c
End Function
On Sun, Jul 31, 2016 at 3:20 AM, Markus Winter <
Post by Markus Winter
Post by Giulio Mastrosanti
Can’t understand why sometimes we (me too…)
are looking for more complicated solutions in the name of… what?
Elegance ;-)
But sometimes it is more efficient to be less elegant.
Take care
MfG
Markus
Sent from my iPad
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.

Continue reading on narkive:
Loading...