Problem with polygon.DistanceToPoint

I am trying to check if my location is within an area.

Using Polygon.DistanceToPoint does not work as it tells me I am outside even though I am inside.


fig.3

Instead if I use Cirlcle.DistanceToPoint the program works.

fig.4

Figure 3 shows the blue trace of the polygon area obtained by loading several coordinates in PolygonFromString (fig2)

fig2
fig.2

Here is the code
fig1

When I use Polygon.DIstance... you can see that distance value is very big but the marker is inside

Why? Where is the problem?

This problem is solved using the ray casting algorithm.

https://www.google.com/search?q=how+to+tell+if+a+point+is+inside+a+polygon

Would you like the fun of coding it, or do you need help?

1 Like

Not seeing any issues in companion:

image

Try a polygon shaped like a gated courtyard, where only the walls are inside the polygon.

The centroid of such a polygon would be outside of the polygon.

Gated Courtyard
(MS Paint)

Ok I'll try to check the algoritm. But I thought the polygon block worked

What about the polygon coordinate you are using?

image

image

What about it ?

Did you drew to polygon by hand dragging the points? Because I have uploaded coordinates in PointsFromString instead

So it was there in the docs all along:

DistanceToPoint(latitude,longitude,centroid)
Computes the distance between the Polygon and the given latitude and longitude. If centroids is true, the distance is computed from the center of the Polygon to the given point. Otherwise, the distance is computed from the closest point on the Polygon to the given point. Further, this method will return 0 if centroids is false and the point is in the Polygon. If an error occurs, -1 will be returned.

https://ai2.appinventor.mit.edu/reference/components/maps.html#Polygon

@TimAI2 for the win.

OK, let us explore that. What method did you use to create your polygon with your uploaded coordinates?

@ABG I also tried to put a hole in a polygon but it didn't work

image

I copied alist of coordinates in the PointsFromString (see below)

fig2

Looks like you didn't swap the lat and long for the holes:

HolePoints
Specifies the points of any holes in the Polygon. The HolePoints property is a list of lists, with each sublist containing (latitude, longitude) points representing a hole.

HolePointsFromString
Specifies holes in a Polygonfrom a GeoJSON string. In contrast to HolePoints, the longitude of each point comes before the latitude as stated in the GeoJSON specification.

Yep, a hole is a list of lists..

Why is that going to be any different from drawing a polygon, it creates a list of coordinates in the PointsFromString.

humm, Do I need to specify the HolePoints? What Are they? I mean is it clear to me that a polygon is made by points; but what are holes?

Don't worry about holes, I was just testing out the theory of being in or out of a polygon.

no difference, just in the fact that if I draw them by hand it's app inventor that creates the list of points, that's all.

It seems that works if I set variables to locationSsensor instead of using get.latidude, get.longitude form the locationChanged block

Screenshot from 2024-10-10 18-59-09