矩形近似算法
我有一个略低于 32 个绝对矩形尺寸的枚举,我需要给定尺寸并在我的枚举中找到最佳近似值。
有没有比我用大量嵌套的 if
和 else
编写的意大利面条代码更好(即更具可读性和可维护性)的方法?
目前我只有:
enum imgOptsScale {
//Some relative scales
w005h005 = 0x8,
w010h010 = 0x9,
w020h020 = 0xA,
w040h040 = 0xB,
w070h070 = 0xC,
w100h100 = 0xD,
w150h150 = 0xE,
w200h200 = 0xF,
w320h320 = 0x10,
w450h450 = 0x11,
w200h010 = 0x12,
w200h020 = 0x13,
w200h070 = 0x14,
w010h200 = 0x15,
w020h200 = 0x16,
w070h200 = 0x17
};
imgOptsScale getClosestSizeTo(int width, int height);
并且我想在进一步编码之前我应该寻求帮助。我应该强调远离过于复杂的库的偏见,尽管我对算法比容器更感兴趣,容器应该在资源受限的系统上运行。
I have an enumeration of just under 32 absolute rectangle sizes and I need to given dimensions and find the best approximation among my enumeration.
Is there any better (ie more readable and maintainable) way than the spaghetti code I am formulating out of lots of nested if
's and else
's?
At the moment I have just:
enum imgOptsScale {
//Some relative scales
w005h005 = 0x8,
w010h010 = 0x9,
w020h020 = 0xA,
w040h040 = 0xB,
w070h070 = 0xC,
w100h100 = 0xD,
w150h150 = 0xE,
w200h200 = 0xF,
w320h320 = 0x10,
w450h450 = 0x11,
w200h010 = 0x12,
w200h020 = 0x13,
w200h070 = 0x14,
w010h200 = 0x15,
w020h200 = 0x16,
w070h200 = 0x17
};
imgOptsScale getClosestSizeTo(int width, int height);
and I thought I'd ask for help before I got too much further into coding up. I should emphasise a bias away from too elaborate libraries though I am more interested in algorithms than containers this is supposed to run on a resource constrained system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我会用一些结构数组来解决这个问题,一个用于水平测量,一个用于垂直测量。
读取数组以找到下一个更大的大小,并返回相应的键。从两个键构建最终的盒子尺寸。 (由于 32 只允许 5 位,这可能不是很理想 - 您可能需要 2.5 位用于水平,2.5 位用于垂直,但我这里的简单方法需要 6 位 - 3 位用于水平,3 位用于垂直如果您对自由度较低的维度之一感到满意,则可以从其中一个列表中删除一半元素(也可以调整
<<3
)。想要两个维度更好地表示,这可能需要足够的返工,这种方法可能不合适。)未经测试的伪代码:
I think I'd approach this with a few arrays of structs, one for horizontal measures and one for vertical measures.
Read through the arrays to find the next larger size, and return the corresponding key. Build the final box measure from the two keys. (Since 32 only allows 5 bits, this is probably not very ideal -- you'd probably want 2.5 bits for the horizontal and 2.5 bits for the vertical, but my simple approach here requires 6 bits -- 3 for horizontal and 3 for vertical. You can remove half the elements from one of the lists (and maybe adjust the
<< 3
as well) if you're fine with one of the dimensions having fewer degrees of freedom. If you want both dimensions to be better represented, this will probably require enough re-working that this approach might not be suitable.)Untested pseudo-code:
是的...将 32 种不同的尺寸放入预先构建的二叉搜索树中,然后在树中递归搜索“最佳”尺寸。基本上,如果当前节点矩形的左子预构建矩形小于输入矩形,并且当前节点矩形大于输入矩形,您将停止搜索。然后,您将返回与两者之间的输入矩形“最接近”的预定义矩形。
递归搜索创建的干净代码的一个很好的补充是它的搜索时间也是对数的而不是线性的。
顺便说一句,您将需要随机化将初始预定义矩形值插入二叉搜索树的顺序,否则您最终会得到一个看起来像链表的退化树,并且您将不会获得对数搜索时间因为树的高度将是节点的数量,而不是节点数量的对数。
因此,例如,如果您已按矩形的面积对树进行排序(假设没有两个矩形具有相同的面积),那么您可以执行如下操作:
顺便说一句,如果树太复杂,您可以也只需做一个矩形实例的数组或
std::vector
,使用std::sort
使用某种类型的标准对它们进行排序,然后在大批。Yes ... place your 32 different sizes in a pre-built binary search tree, and then recursively search through the tree for the "best" size. Basically you would stop your search if the left child pre-built rectangle of the current node's rectangle is smaller than your input rectangle, and the current node's rectangle is larger than the input rectangle. You would then return the pre-defined rectangle that is "closest" to your input rectangle between the two.
One nice addition to the clean code the recursive search creates is that it would also be logarithmic rather than linear in search time.
BTW, you will want to randomize the order that you insert the initial pre-defined rectangle values into the binary search tree, otherwise you will end up with a degenerate tree that looks like a linked list, and you won't get logarithmic search time since the height of the tree will be the number of nodes, rather than logarithmic to the number of nodes.
So for instance, if you've sorted the tree by the area of your rectangles (provided there are no two rectangles with the same area), then you could do something like the following:
BTW, if a tree is too complex, you could also simply do an array or
std::vector
of instances of your rectangles, sort them using some type of criteria usingstd::sort
, and then do binary searched on the array.这是我提出的解决方案,
Here is my proposed solution,