本文介绍了最大的空矩形在一个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在数学真的很好,所以我真的有困难的时候转换公式为code,我找不到任何现成的谷歌搜索周围。我有一个包含很多小的矩形组成的大长方形...和所有我需要做的是计算最大的空矩形。 Anyonne能帮帮我吗?

下面是我想出了...没什么可说的,这是一个很大的失败。

 矩形结果=新的矩形();

对于(双L = 0; L< bigRect.Width ++升)
{
    对于(双T = 0; T< bigRect.Height ++ T)
    {
        双H =​​ 0;
        双瓦特= 0;

        而((H&其中; = bigRect.Width)及及(瓦特&其中; = bigRect.Height))
        {
            矩形largestEmpty =新的Rect(L,T,W,H);

            如果(smallRects.TrueForAll(smallRect =>!smallRect.IntersectsWith(largestEmpty))及及((largestEmpty.Height * largestEmpty.Width)GT(result.Height * result.Width)))
                结果= largestEmpty;
            其他
                打破;

            + + H;
            ++瓦;
        }
    }
}
 

解决方案

从您的该说,有在大矩形点的一组(姑且称之为ASD),你将有找不包含分集ASD的最大矩形。看你的code,看来你不(直接)将这些点。我想从较小Rects ANS创建设置ASD提取点。由于您在double类型的工作,你应该有访问点,否则算法将有显著更高的运行时间,因为你需要检查所有可能的双打在特定范围内(整个大矩形)。使用点,我会努力找到最短距离的形式分彼此(的sqrt(DX ^ 2 + DY ^ 2))(最短不应包含任何点),然后进入下一个最短,看看是否有任何点包含的,等等。换句话说,创建所有的组合(不置换,(A,B)到(C,D)应==(C,D)为(A,B))的距离有序的顺序列表在它们之间。可能不是最佳的,但能够完成任务。

编辑:所有的订单对除了较小Rects对角线应该是顺序列表中,因为较小Rects不应conatined,您也可以排除对具有相同的x或y值

I'm not really good in maths, so I'm having really hard times converting formulas into code, and I can't find anything ready-made googling around. I have a big rectangle containing a lot of small rectangles... and all what I need to do is to calculate the largest empty rectangle. Anyonne can help me?

Here is what I came up with... nothing to say, it's a big fail.

Rect result = new Rect();

for (Double l = 0; l < bigRect.Width; ++l)
{
    for (Double t = 0; t < bigRect.Height; ++t)
    {
        Double h = 0;
        Double w = 0;

        while ((h <= bigRect.Width) && (w <= bigRect.Height))
        {
            Rect largestEmpty = new Rect(l, t, w, h);

            if (smallRects.TrueForAll(smallRect => !smallRect.IntersectsWith(largestEmpty)) && ((largestEmpty.Height * largestEmpty.Width) > (result.Height * result.Width)))
                result = largestEmpty;
            else
                break;

            ++h;
            ++w;
        }
    }
}
解决方案

From your Perdue Docs Link it says there is a set (let's call it ASD) of points in the Big Rect and you would to have find the largest Rect containing no points of the set ASD. Looking at your code, it seems you didn't (directly) incorporate these points. I would extract the points from the smaller Rects ans create set ASD. Since your working in type double, you should have access to the points, otherwise the algorithm would have a significantly higher run time since you need to check all possible doubles in a specific range (the entire Big Rect). Using the points, I would trying find the points with the shortest distance form each other (sqrt(dx^2+ dy^2)) (shortest shouldn't contain any points) then go to the next shortest and see if any points are contained and etc. In other words, create a order list of all combinations (not permutations, (a,b) to (c, d) should be == (c, d) to (a,b)) ordered by the distance in between them. Might not be optimal, but gets the job done.

EDIT: All order pair besides the diagonals of the smaller Rects should be in the order list, since the smaller Rects should not be conatined, You can also exclude pairs with the same x or y value.

这篇关于最大的空矩形在一个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:57