将一个矩形划分为给定区域的近似正方形

发布于 2024-08-27 09:02:13 字数 295 浏览 11 评论 0原文

我有一组 N 个正数,以及一个尺寸为 XY 的矩形,我需要将其划分为 N em> 较小的矩形,使得:

  • 每个较小矩形的表面积与其在给定集合中的相应数字成正比
  • 大矩形的所有空间都被占据,并且较小矩形之间没有剩余空间
  • 每个小矩形的形状应接近正方形如果可行,
  • 执行时间应该相当短,

我需要这方面的指示。您知道网络上描述的这种算法吗?你有什么想法吗(伪代码就可以)?

谢谢。

I have a set of N positive numbers, and a rectangle of dimensions X and Y that I need to partition into N smaller rectangles such that:

  • the surface area of each smaller rectangle is proportional to its corresponding number in the given set
  • all space of big rectangle is occupied and there is no leftover space between smaller rectangles
  • each small rectangle should be shaped as close to square as feasible
  • the execution time should be reasonably small

I need directions on this. Do you know of such an algorithm described on the web? Do you have any ideas (pseudo-code is fine)?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你げ笑在眉眼 2024-09-03 09:02:13

您所描述的听起来像树形图

树形图将分层(树结构)数据显示为一组嵌套矩形。树的每个分支都有一个矩形,然后用代表子分支的较小矩形平铺。叶节点的矩形的面积与数据上的指定维度成比例。

该维基百科页面链接到 Ben Shneiderman 的页面,其中给出一个很好的概述和 Java 实现的链接:

然后,当我在教员休息室对此感到困惑时,我突然想到了啊哈!当你沿着关卡向下移动时,体验将屏幕分成水平和垂直方向交替的矩形。这种递归算法看起来很有吸引力,但我花了几天时间才说服自己它总是有效并编写了一个六行算法。

维基百科还有 Mark Bruls、Kees Huizing 和 Jarke J. van Wijk 的“方形树状图”< /a> (PDF) 提出了一种可能的算法:

我们怎样才能将一个矩形递归地细分为多个矩形,使得它们的长宽比(例如max(高/宽,宽/高))尽可能接近1?所有可能的镶嵌的数量非常大。该问题属于 NP 困难问题的范畴。然而,对于我们的应用程序来说,我们不需要最优解,一个好的解决方案
需要能够在短时间内计算出来。

您在问题中没有提到任何递归,因此您的情况可能只是树状图的一层;但由于算法一次只能在一个级别上工作,所以这应该没有问题。

What you describe sounds like a treemap:

Treemaps display hierarchical (tree-structured) data as a set of nested rectangles. Each branch of the tree is given a rectangle, which is then tiled with smaller rectangles representing sub-branches. A leaf node's rectangle has an area proportional to a specified dimension on the data.

That Wikipedia page links to a page by Ben Shneiderman, which gives a nice overview and links to Java implementations:

Then while puzzling about this in the faculty lounge, I had the Aha! experience of splitting the screen into rectangles in alternating horizontal and vertical directions as you traverse down the levels. This recursive algorithm seemed attractive, but it took me a few days to convince myself that it would always work and to write a six line algorithm.

Wikipedia also to "Squarified Treemaps" by Mark Bruls, Kees Huizing and Jarke J. van Wijk (PDF) that presents one possible algorithm:

How can we tesselate a rectangle recursively into rectangles, such that their aspect-ratios (e.g. max(height/width, width/height)) approach 1 as close as possible? The number of all possible tesselations is very large. This problem falls in the category of NP-hard problems. However, for our application we do not need the optimal solution, a good solution
that can be computed in short time is required.

You do not mention any recursion in the question, so your situation might be just one level of the treemap; but since the algorithms work on one level at a time, this should be no problem.

欢烬 2024-09-03 09:02:13

我一直在做类似的事情。我优先考虑简单性而不是获得尽可能相似的纵横比。这(理论上)应该有效。在纸上测试了 1 到 10 之间的某些 N 值。N

= 要创建的矩形总数,
Q = 最大(宽度,高度)/最小(宽度,高度),
R = N / Q

如果 Q > N/2,将矩形沿其最长边分成 N 部分。
如果 Q <= N/2,则沿矩形的最短边将矩形分割为 R(圆角整数)部分。
然后沿着其最短边将子矩形分割为 N/R(向下舍入整数)部分。
从下一个子矩形除法的结果中减去向下舍入的值。对所有子矩形重复此操作,或者直到创建所需数量的矩形。

I have been working on something similar. I'm prioritizing simplicity over getting as similar aspect ratios as possible. This should (in theory) work. Tested it on paper for some values of N between 1 and 10.

N = total number of rects to create,
Q = max(width, height) / min(width, height),
R = N / Q

If Q > N/2, split the rect in N parts along its longest side.
If Q <= N/2, split the rect in R (rounded int) parts along its shortest side.
Then split the subrects in N/R (rounded down int) parts along its shortest side.
Subtract the rounded down value from the result of the next subrects division. Repeat for all subrects or until the required number of rects are created.

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