我需要在网站页面上使用 JavaScript 优化布局图像,以便最大限度地减少空白量。
优化问题基本上是最小化以下内容:
(rightmost x-coordinate of an image - leftmost x-coordinate of an image) +
(bottommost y-coordinate of an image - topmost y-coordinate of an image)
但是,没有图像可以重叠,因此对于每个图像,约束是:
for i in images
for j in each other image
(topmost coordinate of i > bottommost coordinate of j) ||
(bottommost coordinate of i < topmost coordinate of j) ||
(leftmost coordinate of i > rightmost coordinate of j) ||
(rightmost coordinate of i < leftmost coordinate of j)
此外,还有一个约束,任何图像的最右边坐标不能大于页面的宽度,并且任何图像的最左边坐标必须> 0。
首先我想把它表述为一个线性规划问题,但是我看到的所有 JavaScript 的线性规划库都不允许这样复杂的约束,所以我认为这可能不是一个线性问题。
然后我开始认为这是一个动态编程问题,但如果不尝试每种布局组合,我不确定如何解决它,这会非常慢。
有谁知道如何有效地解决这类问题?