将一个正方形或长方形分解为大量随机大小的正方形或长方形
我试图将一个正方形或矩形分解为大量随机大小的正方形或矩形,以便没有一个重叠。
好吧,当然其他人也问过这个问题,我发现最好的线程是 如何用较小的正方形/矩形填充正方形?
解决方案似乎是通过装箱或某种树形图。
但我正在寻找的是 Java、Javacript、actionscript 甚至 C 中的实际算法。
I'm trying to break up a square or rectangle into a large number of randomly sized squares or rectangles so that none are overlapping.
Ok of course others have asked this the best thread I found is
How to fill a square with smaller squares/rectangles?
The solution seems to be either through bin packing or some sort of tree map.
But what I'm looking for is the actual algorithm in Java, Javacript , actionscript or even C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是尝试“分而治之”技术。在迭代 1 中,您有一个矩形。将矩形分成两个较小的矩形。一种方法如下。假设矩形是 100x50 。选择一个 0-100 之间的随机数(矩形的长度)。假设随机数是 20 。然后你可以将矩形分成两个较小的,尺寸分别为 20x50 和 80x50 。对于这 2 个新矩形,递归地应用相同的过程。(因此在迭代 2 中,您将有 4 个矩形)。这样做 n 次,你将得到 2^n 个矩形。
此外,在每次迭代中,您可以随机选择是否应根据每个矩形的长度(垂直)或宽度(水平)来完成吐痰。
希望有帮助!
A solution would be to try the "Divide and Conquer" technique. In iteration 1 you have a rectangle. Divide the rectangle in two smaller ones. A way to do that is the following . Lets say the rectangle is 100x50 .Choose a random number between 0-100 (the length of the rectangle).Lets say the random number is 20. Then you can spit your rectangle in two smaller ones with sizes 20x50 and 80x50. For these 2 new rectangles apply recursively the same procedure.(hence in iteration 2 you will have 4 rectangles). Do that n -times and you will have 2^n rectangles.
Also in each iteration you could randomly choose if the spitting should be done by the length (vertical) or width (horizontal) of each rectangle.
hope it helps!
提供的代码创建一个 kd 树。您可以使用它在矩形上绘制线条,将其分成更小的矩形。获得树后,您可以按如下方式使用它来将区域划分为这些矩形:
代码:
The provided code creates a k-d tree. You can use this to draw lines on your rectangle that will divide it into smaller rectangles. After you've got your tree you can use it as follows to divide your region up into these rectangles:
Code:
将长度随机分为 x 部分
现在,将每个较小的矩形分别随机分为 y 部分
这是一些 ActionScript 代码(用记事本编写,您必须检查错误)。它获取输入矩形的宽度和高度,并返回一个包含分割矩形顶点的数组
Randomly divide the length into x parts
Now, randomly divide each smaller rectangle individually into y parts
Here's some ActionScript code (written in notepad, you'll have to check for errors). It takes the width and height of the input rectangle and returns an array with the vertices of the divided rectangles