Folks,

I need to plot random points within a polygon. Does anyone knows about an implementation which can do that ?

Also if anyone knows the algorithm how to do that I can look at it as well.

Thanks

AC

Folks,

I need to plot random points within a polygon. Does anyone knows about an implementation which can do that ?

Also if anyone knows the algorithm how to do that I can look at it as well.

Thanks

AC

Hi AC,

Cesium doesn’t have direct support for this (although perhaps we should! It’s a classic graphics problem). However, I don’t think the implementation is too tricky. I would use a sample-reject approach, in which you try to place a random point somewhere in the axis-aligned bounding rectangle around your (presumably 2D) polygon, then check if it’s inside. If not, then give up on that point and start again. Repeat for as many points as you need inside your polygon.

The easiest (but not most robust) solution for checking whether a point is inside a 2D polygon is to ray cast from the point and check how many times you intersect any edges. Wikipedia has a nice description: https://en.wikipedia.org/wiki/Point_in_polygon

You may find some of our IntersectionTest methods useful:

http://cesiumjs.org/Cesium/Build/Documentation/IntersectionTests.html

Hope that helps! Let me know if you need more help – if you give me more details about your use case I may also be able to make more specific suggestions.

- Rachel

Rachel,

Thanks for your suggestion.I will try to implement the sample-reject approach which you mentioned.

Here is my use case. My usecase is similar to this tutorial example of custom styling.

Instead of using entity.polygon.extrudedHeight = entity.properties.Population / 50.0;

I would like to just put random points in the polygon. Number of points in a particular polygon/state would be directly propotional to the population of that state.

If you know of any example code which does the similar thing that would be very appreciated.

Best Regards -Ajit

Chour, I would recommend you check out turf.js: http://turfjs.org/

In fact, this link looks to be exactly what you need: https://gis.stackexchange.com/questions/163044/mapbox-how-to-generate-a-random-coordinate-inside-a-polygon