具有给定纵横比的树形图
我想创建一个 TreeMap
,使用图片来填写树状图
矩形。我可以假设所有图片都具有相同的宽度和高度(即长宽比)。因此,我需要一个树形映射算法来创建具有给定比例的矩形,我是否能够将图片放在那里(如果需要,也许可以缩放图片)。
你能推荐一个吗?
I would like to create a TreeMap
, using pictures to fill out the treemap
rectangles. I can assume that all the pictures have the same width and height (i.e. the aspect ratio). Thus, I need a treemapping
algorithm to create the rectangles with a given ratio, do I will be able to put the pictures there (and perhaps scale the pictures if I need).
Could you recommend one ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,解决方案是不可能的 - Albin Sunnanbo 提供了反例证明。假设您的边界矩形与目标矩形具有相同的纵横比,您可以通过将边界矩形缩放为正方形并运行 方形树状图[pdf]算法对其进行处理,然后缩放回矩形。由于方形树状图不能保证完美的正方形,因此您的最终矩形不会具有完美的纵横比,但实际上它可能不会使您的图片扭曲太多。
一个很好的改进是对图片进行接缝雕刻,使它们正确地适应稍微偏离的纵横比矩形;例如,如果您使用它来组装拼贴画,您应该从中获得视觉上令人愉悦的结果。
In general, a solution is not possible - Albin Sunnanbo has provided a proof by counterexample. Assuming your bounding rectangle has the same aspect ratio as your target rectangles, you might be able to get a good-enough solution by scaling the bounding rectangle to be a square, running the squarified treemap[pdf] algorithm over it, and then scaling back to a rectangle. Since the squarified treemap doesn't guarantee perfect squares, your final rectangles will not have a perfect aspect ratio, but in practice it might not distort your pictures too much.
A nice refinement would be to run a seam carving pass over your pictures to fit them properly into the slightly-off aspect-ratio rectangles; if you're using this to assemble a collage, for instance, you should get visually pleasing results out of this.