且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拆分一个矩形分成N个相同大小的矩形

更新时间:2023-01-04 10:52:36

(我假设,也许是错误的,你的矩形是无限可分的,而不是由独立的像素。)

(I'm assuming, perhaps wrongly, that your rectangles are infinitely divisible rather than being made up of discrete pixels.)

您可以随时获得完全正确的纵横比,在一定的成本浪费矩形,通过让M = CEIL(开方(N)),并使用m个每边。

You can always get exactly the correct aspect ratios, at some cost in wasted rectangles, by letting m = ceil(sqrt(n)) and using m pieces on each side.

否则,您正在寻找P,Q接近的sqrt(N),使得PQ> = n和p,q是彼此接近。当然,P的***选择,Q将取决于你怎么舍得对不准确的权衡浪费。这似乎并不可能,你会永远想利用P,Q从开方(N)很远,因为这样做会给你一个很大的错误的形状。所以我想你想是这样的:

Otherwise, you're looking for p,q close to sqrt(n) such that pq >= n and p,q are close to one another. The best choice of p,q will of course depend on how willing you are to trade off waste against inaccuracy. It doesn't seem likely that you'll ever want to take p,q very far from sqrt(n), because doing so would give you a large error in shape. So I think you want something like this:

p = ceiling(sqrt(n))
best_merit_yet = merit_function(p,p,0)
best_configuration_yet = (p,p)
for p from floor(sqrt(n)) downward:
  # we need pq >= n and q as near to p as possible, which means (since p is too small) as small as possible
  q = ceiling(n/p)
  if max(merit_function(n/p,n/q,0), merit_function(n/q,n/p,0)) < best_merit_yet:
    break
  n_wasted = p*q-n
  merit1 = merit_function(n/p,n/q,n_wasted)
  merit2 = merit_function(n/q,n/p,n_wasted)
  if max(merit1,merit2) > best_merit_yet:
    if merit1 > merit2:
      best_configuration_yet = (p,q)
      best_merit_yet = merit1
    else:
      best_configuration_yet = (q,p)
      best_merit_yet = merit2

和希望是非常错误的形状是非常糟糕的事实将意味着你从来没有真正不得不采取循环的迭代。

and hopefully the fact that very wrong shapes are very bad will mean that you never actually have to take many iterations of the loop.

下面, merit_function 应该体现你的preferences交易性降形状反对浪费。

Here, merit_function is supposed to embody your preferences for trading off shape against waste.