-1

I'm trying to build Quad-tree which receive array of points and split them into 4 cells. those 4 cells(cell1...cell4) are suppose to be created recursively but it doesn't works. What I'm doing wrong? wasted all day trying fix it. I erased some basic things not to overload the script here.

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
}  
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dima B
  • 39
  • 7
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 04 '14 at 17:36
  • 2
    "it doesn't work", in what way does it not work? Do you get a exception, values are null, unexpected results? – Scott Chamberlain Jan 04 '14 at 17:37
  • FYI on the second constructor you should just call into the first constructor i.e. `public QuadTreeNode(Point2D[] pointArray, Point2D leftMin, Point2D rightMax, int maxPPC) : this(leftMin, rightMax, maxPPC) { ... }` this keeps your initialisation in one place. – James Jan 04 '14 at 17:39
  • it doesn't work=Its sort the points into cellsList[] after first run of Split but in the next Split its doesn't create new cells-of-cells even thought it pass there – Dima B Jan 04 '14 at 17:43
  • Side note: `Array` - naming parameter with upper case is unusual (and does not match the rest of your code), but naming it to match common CLR type should only be done if you really want to confuse readers. – Alexei Levenkov Jan 04 '14 at 17:59

1 Answers1

1

The problem may be with this part:

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

Here, you're copying the list at location c but you're clearing each list at every iteration anyway, so you're likely losing some of your points.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
Matt
  • 2,682
  • 1
  • 17
  • 24