【发布时间】:2011-08-24 00:23:13
【问题描述】:
我的班级有以下成员:
- X
- 是
- 宽度
- 身高
可以使用这些参数创建一个矩形。
现在我的问题是我有一个此类的列表,List<MyClass>。
我需要将列表中的每个对象与所有剩余对象进行比较,如果currentObject.Location(X, Y) 落在另一个对象的rectangle(X, Y, Width, Height) 中,我需要从列表中删除另一个对象。
我用 for 循环实现了它。
但主要问题是:性能。 我的最小列表数是 300000。
是否有任何程序可以使用任何 .Net 版本(包括 LINQ)来提高此任务的性能?
`公共类 RectBase { 私人 int _rectId; 私人PointF _rectLocation; 私有SizeF _rectSize;
public RectBase()
{
_rectId = -911;
_rectLocation = new PointF(0, 0);
_rectSize = new SizeF(0, 0);
}
public RectBase(int id, PointF loc, SizeF size)
{
_rectId = id;
_rectLocation = loc;
_rectSize = size;
}
public bool IsIntersected(RectBase otherRectObject)
{
RectangleF currentRect = new RectangleF(_rectLocation, _rectSize);
if (currentRect.Contains(otherRectObject.RectLocation))
return true;
else
return false;
}
public int RectId
{
get { return _rectId; }
set { _rectId = value; }
}
public PointF RectLocation
{
get { return _rectLocation; }
set { _rectLocation = value; }
}
public SizeF RectSize
{
get { return _rectSize; }
set { _rectSize = value; }
}
}
public class RectProcessor
{
List<RectBase> _rectList;
int maxCount = 300000;
public RectProcessor()
{
_rectList = new List<RectBase>();
FillList();
}
private void FillList()
{
// Adding the items to the list with dummy values
for (int i = 0; i < maxCount; i++)
{
int id = i+1;
PointF loc = new PointF(id, id);
SizeF sz = new SizeF(id, id);
RectBase obj = new RectBase(id, loc, sz);
_rectList.Add(obj);
}
}
private void RemoveIntersectedObjects()
{
List<RectBase> filteredList = new List<RectBase>();
bool isIntersected = false;
for (int i = 0; i < maxCount; i++)
{
for (int j = 0; j < maxCount; j++)
{
if (_rectList[i].IsIntersected(_rectList[j]))
{
isIntersected = true;
break;
}
}
if (!isIntersected)
{
filteredList.Add(_rectList[i]);
}
isIntersected = false;
}
}
}
`
【问题讨论】:
-
感觉四叉树可以帮到你...
-
请发布代码示例,以便我们了解您是如何解决问题的。
-
Linq 不会更快。您需要的是针对该问题的优化算法。
-
听起来矩形的顺序很重要 - “对于所有 R[i = 0..n] 和所有 R[j = i+1..n] 如果 R[i]重叠 R[j] 然后删除 R[i]。”如果是这样的话,那么我认为你被卡住了。任何形式的问题划分都会影响矩形的顺序,因此会删除错误的。您可以获得的最佳性能是对
n矩形的n*(n-1)/2操作。 -
这是对问题的正确陈述吗:“给定一个矩形列表,从列表中删除完全包含在列表中任何其他矩形边界内的任何矩形。”?