将一个正方形或长方形分解为大量随机大小的正方形或长方形

发布于 2024-12-09 18:58:20 字数 294 浏览 1 评论 0原文

我试图将一个正方形或矩形分解为大量随机大小的正方形或矩形,以便没有一个重叠。

好吧,当然其他人也问过这个问题,我发现最好的线程是 如何用较小的正方形/矩形填充正方形?

解决方案似乎是通过装箱或某种树形图。

但我正在寻找的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风尘浪孓 2024-12-16 18:58:20

解决方案是尝试“分而治之”技术。在迭代 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!

北凤男飞 2024-12-16 18:58:20

提供的代码创建一个 kd 树。您可以使用它在矩形上绘制线条,将其分成更小的矩形。获得树后,您可以按如下方式使用它来将区域划分为这些矩形:

  1. 选择树根处的节点。
  2. 通过该点画一条垂直线。
  3. 选择它的左子项,在您刚刚通过其父项绘制的线的左侧通过该点绘制一条水平线(这条线在您刚刚绘制的线处停止)。
  4. 选择它的右子项,在您刚刚通过其父级绘制的线的右侧通过该点绘制一条水平线(这条线也停在您通过父级绘制的线处)。
  5. 递归地执行此操作,在树的每一层的垂直线和水平线之间切换。

代码:

int MAX_HEIGHT = 100;
int MAX_WIDTH = 100;
int NUM_POINTS = 6;


// Generate random list of points
List<Point> pointList = new List<Point>();

Random rand = new Random();

for(int i = 0; i < NUM_POINTS ; i++)
{
    pointList.add(new Point(rand.nextInt(MAX_HEIGHT), rand.nextInt(MAX_WIDTH));
}

BinaryTree tree = CreateKDTree(pointList, 0);


// Recursive function for creating a K-D Tree from a list of points
// This tree can be used to draw lines that divide the space up
// into rectangles.
public BinaryTree CreateKDTree(List<Point> pointList, int depth)
{
    // Have to create the PointComparator class that just selects the
    // specified coordinate and sorts based on that
    Coordinate coord= depth % 2 == 0 ? X_COORDINATE : Y_COORDINATE
    Collections.sort(pointList, new PointComparator(coord));

    int median = pointList.size() / 2;

     // unfortunately Java doesn't have a BinaryTree structure so
     // you have to create this too
    BinaryTree node = new BinaryTree(pointList[median]);

    if(pointList.size() == 1) return node;

    if(median > 0)
        node.left(CreateKDTree(pointList.subList(0, median), depth + 1);

    if(median + 1 < subList.size())
        node.right(CreateKDTree(pointList.subList(median + 1, subList.size()), depth + 1);

    return node; 
}

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:

  1. Pick the node at the root of the tree.
  2. Draw a vertical line through this point.
  3. Choose it's left child, draw a horizontal line through this point on the left side of the line you just drew through it's parent (this line stops at the line you just drew).
  4. Choose it's right child, draw a horizontal line through this point on the right side of the line you just drew through it's parent (this line also stops at the line you drew through the parent).
  5. Do this recursively, switching between vertical and horizontal lines at each level of the tree.

Code:

int MAX_HEIGHT = 100;
int MAX_WIDTH = 100;
int NUM_POINTS = 6;


// Generate random list of points
List<Point> pointList = new List<Point>();

Random rand = new Random();

for(int i = 0; i < NUM_POINTS ; i++)
{
    pointList.add(new Point(rand.nextInt(MAX_HEIGHT), rand.nextInt(MAX_WIDTH));
}

BinaryTree tree = CreateKDTree(pointList, 0);


// Recursive function for creating a K-D Tree from a list of points
// This tree can be used to draw lines that divide the space up
// into rectangles.
public BinaryTree CreateKDTree(List<Point> pointList, int depth)
{
    // Have to create the PointComparator class that just selects the
    // specified coordinate and sorts based on that
    Coordinate coord= depth % 2 == 0 ? X_COORDINATE : Y_COORDINATE
    Collections.sort(pointList, new PointComparator(coord));

    int median = pointList.size() / 2;

     // unfortunately Java doesn't have a BinaryTree structure so
     // you have to create this too
    BinaryTree node = new BinaryTree(pointList[median]);

    if(pointList.size() == 1) return node;

    if(median > 0)
        node.left(CreateKDTree(pointList.subList(0, median), depth + 1);

    if(median + 1 < subList.size())
        node.right(CreateKDTree(pointList.subList(median + 1, subList.size()), depth + 1);

    return node; 
}
生活了然无味 2024-12-16 18:58:20

将长度随机分为 x 部分

现在,将每个较小的矩形分别随机分为 y 部分

这是一些 ActionScript 代码(用记事本编写,您必须检查错误)。它获取输入矩形的宽度和高度,并返回一个包含分割矩形顶点的数组

private function divRect(w:Number, h:Number):Array {
    var rw:Number=0, rh:Number=0;
    var wa:Array=[0], rv:Array=[];
    while(rw < w) {
        var r:Number=Math.random() * (w-rw);
        wa.push(r+rw);
        rw+=r;
    }

    for(var i:int=1; i<wa.length; i++) {
        while(rh < h) {
            var o:Object={x: wa[i-1], x2: wa[i]};
            var s:Number=Math.random() * (h-rh);
            o.y=rh;
            rh+=s;
            o.y2=rh;
            rv.push(o);
        }

    }

}

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

private function divRect(w:Number, h:Number):Array {
    var rw:Number=0, rh:Number=0;
    var wa:Array=[0], rv:Array=[];
    while(rw < w) {
        var r:Number=Math.random() * (w-rw);
        wa.push(r+rw);
        rw+=r;
    }

    for(var i:int=1; i<wa.length; i++) {
        while(rh < h) {
            var o:Object={x: wa[i-1], x2: wa[i]};
            var s:Number=Math.random() * (h-rh);
            o.y=rh;
            rh+=s;
            o.y2=rh;
            rv.push(o);
        }

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文