简体   繁体   English

如何用随机大小的正方形和矩形填充正方形

[英]How can i fill a square with randomly sized squares and rectangles

So i need to know how i can go about filling an area with randomly sized rectangles and squares - like this for instance:所以我需要知道如何用随机大小的矩形和正方形填充一个区域——例如:

I already have a partially working demo however it has alot of instances in where it will not work and ontop of this it requires alot of manual checks which isn't the easiest thing to program nor is it efficient.我已经有一个部分工作的演示,但是它有很多无法工作的实例,除此之外,它需要大量的手动检查,这不是最容易编程的事情,也不是有效的。

Ontop of the challenge at hand i'd like to also avoid using methods that require checking collision such as using an attached RigidBody2D or a Ray cast as i'm working with ui and would like to simply generate a table of locations and sizes for easier access (however if this is unavoidable i understand and please do still share your answer if this is the case)除了手头的挑战之外,我还想避免使用需要检查碰撞的方法,例如在使用 ui 时使用附加的 RigidBody2D 或 Ray cast,并且想简单地生成位置和大小表以便更轻松访问(但是,如果这是不可避免的,我理解,如果是这种情况,请仍然分享您的答案)

I was hoping to simulate it in the sense of a table where you can merge cells together but i'm uncertain how this is achievable - if at all.我希望在表格的意义上模拟它,您可以将单元格合并在一起,但我不确定这是如何实现的 - 如果有的话。

Thankyou in advance!先感谢您! :) :)

Edit:编辑:

In relation to UnholySheep's comment i found this .关于 UnholySheep 的评论,我发现了这个 The Kd-Tree looks promising but (correct me if i'm wrong) i don't believe it can be immplemented in csharp without programming out of scope and i think it literally draws squares as opposed to implements gameobjects or Rect objects with a size and position. Kd-Tree 看起来很有希望,但是(如果我错了,请纠正我)我不相信它可以在 csharp 中实现而不编程超出范围,我认为它实际上绘制正方形而不是实现游戏对象或具有大小的 Rect 对象和位置。

Furthermore there's this thread but again it mentions using a Kd-Tree or methods which i want to avoid or as Brian said using a merge method which i don't think is achieavable in unity without programming an entire table module which is out of scope again.此外,还有这个线程,但它再次提到使用 Kd-Tree 或我想避免的方法,或者正如 Brian 所说的使用合并方法,我认为在不编程再次超出范围的整个表模块的情况下无法统一实现. Additionally someone mentioned using a spiral which albeit is an interesting approach it wouldn't result in the randomness i want to achieve.此外,有人提到使用螺旋虽然是一种有趣的方法,但它不会导致我想要实现的随机性。

To clarify i am looking for a fairly simple algorithm - i don't believe a Kd-tree fits this but for anyone else this may be an option as there are unity modules for this .为了澄清,我正在寻找一个相当简单的算法——我不相信 Kd-tree 适合这个,但对于其他任何人来说,这可能是一个选项,因为有统一模块

Your question is a challange, plus writing code that perfectly produces the image takes a lot of time and patience.您的问题是一个挑战,加上编写完美生成图像的代码需要大量时间和耐心。 But here I wrote code that puts a field of all kinds of letters in a 2D array.但在这里,我编写了将各种字母字段放入二维数组的代码。 As long as this array is empty it will allow itself to fill them with random rectangles.只要这个数组是空的,它就会允许自己用随机的矩形填充它们。

public enum squareType { none, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, Y, W, X, Z }

public squareType[,] cases = new squareType[4,4];
public void Start()
{
    var lengthX = cases.GetLength(0);
    var lengthY = cases.GetLength(1);
    
    var index = 0;
    
    for (var i = 0; i < lengthX; i++)
    {
        for (var j = 0; j < lengthY; j++)
        {
            if (cases[i,j] != squareType.none) continue;

            var randomX = Random.Range(i, Mathf.Min(i+3, lengthX)); 
            var randomY = Random.Range(j, Mathf.Min(j+3, lengthY));
            
            var color = (squareType) Enum.ToObject(typeof(squareType), ++index);
            
            for (var x = i; x <= randomX; x++)
            {
                for (var y = j; y <= randomY; y++) cases[x, y] = color;
            }
        }
    }
    
    // Debug table
    for (var i = 0; i < lengthX; i++)
    {
        var xField = "";
        for (var j = 0; j < lengthY; j++) xField += " | " + cases[i, j];
        Debug.Log(xField);
    }
}

Example Result 4x4:示例结果 4x4:

在此处输入图像描述

Example Result 6x6:示例结果 6x6:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM