有问题创建递归构造函数

时间:2014-01-04 17:34:35

标签: c# recursion constructor quadtree

我正在尝试构建接收点阵列的Quad-tree,并将它们分成4个单元格。假设这4个单元格(cell1 ... cell4)是递归创建的,但它不起作用。 我做错了什么?整天都在浪费时间去修理它。 我删除了一些基本的东西,不要在这里重载脚本。

class QuadTreeNode
{
    private QuadTreeNode cell1;
    private QuadTreeNode cell2;
    private QuadTreeNode cell3;
    private QuadTreeNode cell4;
    private int maxppc;

    private double leftminX, leftminY, rightmaxX, rightmaxY;

    public QuadTreeNode(Point2D leftMin, Point2D rightMax, int maxPPC)
    {
        this.leftminX = leftMin.East;
        this.leftminY = leftMin.North;
        this.rightmaxX = rightMax.East;
        this.rightmaxY = rightMax.North;
        this.maxppc = maxPPC;
    }
    public QuadTreeNode(Point2D[] pointArray, Point2D leftMin, Point2D rightMax, int maxPPC)
    {
            for (int i=0; i <4; i++)
            {
                cellList[i] = new List<Point2D>();
            }

        allPointList = pointArray.ToList();
        this.leftminX = leftMin.East;
        this.leftminY = leftMin.North;
        this.rightmaxX = rightMax.East;
        this.rightmaxY = rightMax.North;
        this.maxppc = maxPPC;

        if (allPointList.Count > maxPPC)
        {
            Split(allPointList);
        }
    }

    public void Split(List<Point2D> Array)
    {

        if (Array.Count > maxppc)
        {
            double centerE = (this.leftminX + this.rightmaxX) / 2;
            double centerN = (this.leftminY + this.rightmaxY) / 2;
            double deltaE = (this.rightmaxX - this.leftminX) / 2;
            double deltaN = (this.rightmaxY - this.leftminY) / 2;

            Point2D Center = new Point2D(centerE, centerN);
            this.cell1 = new QuadTreeNode(cellList[0].ToArray(),new Point2D((Center.East - deltaE), (Center.North - deltaN)), Center, maxppc);
            this.cell2 = new QuadTreeNode(cellList[1].ToArray(), new Point2D(Center.East, Center.North - deltaN), new Point2D((Center.East + deltaE), Center.North), maxppc);
            this.cell3 = new QuadTreeNode(cellList[2].ToArray(), new Point2D((Center.East - deltaE), (Center.North)), new Point2D(Center.East, (Center.North + deltaN)), maxppc);
            this.cell4 = new QuadTreeNode(cellList[3].ToArray(), Center, new Point2D((Center.East + deltaE), (Center.North + deltaN)), maxppc);
            for (pntIndex = 0; pntIndex < Array.Count; pntIndex++)
            {
                CellIndex(Array[pntIndex]);
            }
            pntIndex = 0;
            Array.Clear();
            for (int c=0; c < 4; c++)
            {
                Array = cellList[c].ToList();
                cellList[0].Clear();
                cellList[1].Clear();
                cellList[2].Clear();
                cellList[3].Clear();
                Split(Array);
            }
            return;
        }
        else
        {
            return;
        }
    }
    public void CellIndex(Point2D point)
    {
//locates points up to East,North
}  

1 个答案:

答案 0 :(得分:1)

问题可能出在这个部分:

Array = cellList[c].ToList();
cellList[0].Clear();
cellList[1].Clear();
cellList[2].Clear();
cellList[3].Clear();
Split(Array);

在这里,您要复制位置c的列表,但无论如何您都会清除每个列表中的每个列表,因此您可能会失去一些积分。