Need to plot random points within a ploygon

359 views
Skip to first unread message

chour...@gmail.com

unread,
May 23, 2017, 9:09:53 AM5/23/17
to cesium-dev
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

Rachel Hwang

unread,
May 24, 2017, 1:46:23 PM5/24/17
to cesium-dev
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: 

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

chour...@gmail.com

unread,
May 25, 2017, 2:36:47 PM5/25/17
to cesium-dev
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

Matthew Amato

unread,
May 25, 2017, 6:56:33 PM5/25/17
to cesiu...@googlegroups.com
Chour, I would recommend you check out turf.js: http://turfjs.org/ 


--
You received this message because you are subscribed to the Google Groups "cesium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cesium-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages