简体   繁体   中英

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)

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 . 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.

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. 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 .

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:

在此处输入图像描述

Example Result 6x6:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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