简体   繁体   English

获得最好的方形排列

[英]Get the best square like arrangement

The Situation情况

I have a certain amount of squares.我有一定数量的正方形。 I want these square to always from a square like grid but it can't have any empty spaces.我希望这些正方形总是来自像网格这样的正方形,但它不能有任何空白空间。 I'd use JavaScript to do this.我会使用 JavaScript 来做到这一点。 The grid would be stored like so网格将像这样存储

var gridLayout = [[1, 2], [3, 4]];

This would result in this grid这将导致此网格

2 x 2 网格

The Problem问题

If I have 16 squares it would result with如果我有 16 个正方形,它会导致

var gridLayout = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]];

And the visual而且视觉效果

4乘4格

Now all that's easy to do, but what if there's 3 squares?现在这一切都很容易做到,但是如果有 3 个正方形呢? I'd like it to do something like this, favoring the horizontal over the vertical layout .我希望它做这样的事情,支持水平布局而不是垂直布局

var gridLayout = [[1, 2, 3]];

with the visual随着视觉

1乘3格

If I had 8 squares it couldn't make the perfect square so I'd like to be如果我有 8 个正方形,它就不能成为完美的正方形,所以我想成为

var gridLayout = [[1, 2, 3, 4], [5, 6, 7, 8]];

And the visual而且视觉效果

2 x 4 格子

What I've tried我试过的

I've looked at this but it seems to be working with rectangles instead of squares.我看过这个,但它似乎使用矩形而不是正方形。 I also have a very hard time following without the visuals of what they want.如果没有他们想要的视觉效果,我也很难跟随。

I have also looked at this which is kinda similar but seem to be a bit irrelevant to my situation.我也看过这个有点相似但似乎与我的情况无关。

Conclusion结论

How would I create the gridLayout in such a way as what I want?我将如何以我想要的方式创建gridLayout Any help is appreciated.任何帮助表示赞赏。

You can use a solution from Split array into chunks , to split an array of elements.您可以使用Split array into chunks 中的解决方案来拆分元素数组。 Now we just need to determine the correct chunk size.现在我们只需要确定正确的块大小。

It looks like the criteria is:看起来标准是:

  • Each chunk must be the same size.每个块的大小必须相同。 This can be achieved by requiring that the number of elements is evenly divisible by the chunk size (ie #elements % chunk_size === 0 ).这可以通过要求元素数量被块大小整除(即#elements % chunk_size === 0 )来实现。
  • There should be at most as many rows as columns.最多应该有与列一样多的行。 In other words, the chunk size must be larger than the number of chunks.换句话说,块大小必须大于块的数量。 We can fulfill that by starting with a chunk size that is at least Math.sqrt(#elements) .我们可以通过从至少为Math.sqrt(#elements)的块大小开始来实现这一点。

 // Implementation function chunk (arr, len) { var chunks = [], i = 0, n = arr.length; while (i < n) { chunks.push(arr.slice(i, i += len)); } return chunks; } function gridify(arr) { let size = Math.ceil(Math.sqrt(arr.length)); while (arr.length % size !== 0) { size += 1; } return chunk(arr, size); } // Helper functions for example function range(n) { return Array.from({length: n}, (_,i) => i+1); } function run(size) { console.log(JSON.stringify(gridify(range(size)))); } // Create examples run(4); run(16); run(3); run(8);

There is hopefully a better way to determine the chunk size than just iterating and increasing size, but that's what I came with in this short time.希望有一种更好的方法来确定块大小,而不仅仅是迭代和增加大小,但这就是我在短时间内想到的。

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

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