Break a rectangle into randomly shaped polygons

↘锁芯ラ 提交于 2019-12-20 10:44:53

问题


Just like the title says, I need to break a rectangle into randomly shaped polygons.

Example, with 7 polygons:

+--------+--------+
|\       |   2    |
| \  1   |''--..__|
|  ------|  5     |
| 3  /   \________|
|   /    /\   6   |
|__/ 4  /  \______|
|      /  7       |
+-----+-----------+

I don't know if there's an algorithm already out there for this, but I can't seem to get my head around this.

I don't particularly care what language you answer in, but I'll be implementing in Java/Swing.


回答1:


You may drop a bunch of random points on the rectangle, and calculate the Voronoi Diagram.

Here is a Java/Swing implementation.

I did some samples (but using Mathematica, not the above implementation)

HTH!




回答2:


I'd draw a bunch of random lines across the full rectangle and then "split" the lines at each line intersection, so that you basically have some kind of net of lines. Then remove as many random line segments as you like until you reach the desired number of polygons.

Edit: So for your sample it would have been like this after adding the lines:

+----+---+----+---+
|\'--.\_/|   /    |
| \    X |''/-..__|
|--\--+-\+-/------|
|___\/___\/_______|
|   /\   /\       |
|__/__\_/|_\______|
| /    X |  \     |
++----+-++---+----+


来源:https://stackoverflow.com/questions/4330524/break-a-rectangle-into-randomly-shaped-polygons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!