Example #1
0
 // Initialize the quadtree.
 private void Form1_Load(object sender, EventArgs e)
 {
     Root = new QuadtreeNode(new RectangleF(0, 0,
                                            pointsPictureBox.ClientSize.Width - 1,
                                            pointsPictureBox.ClientSize.Height - 1));
 }
Example #2
0
        // Add a point to this subtree.
        public void AddPoint(PointF newPoint)
        {
            // See if this quadtree node us full.
            if ((Points != null) && (Points.Count + 1 > MaxPoints))
            {
                // Divide this quadtree node.
                float wid = (Area.Right - Area.Left) / 2f;
                float hgt = (Area.Bottom - Area.Top) / 2f;
                NWchild = new QuadtreeNode(new RectangleF(Area.Left, Area.Top, wid, hgt));
                NEchild = new QuadtreeNode(new RectangleF(Area.Left + wid, Area.Top, wid, hgt));
                SEchild = new QuadtreeNode(new RectangleF(Area.Left + wid, Area.Top + hgt, wid, hgt));
                SWchild = new QuadtreeNode(new RectangleF(Area.Left, Area.Top + hgt, wid, hgt));

                // Move the points into the appropriate subtrees.
                foreach (PointF pt in Points)
                {
                    if (pt.Y < Ymid)
                    {
                        if (pt.X < Xmid)
                        {
                            NWchild.AddPoint(pt);
                        }
                        else
                        {
                            NEchild.AddPoint(pt);
                        }
                    }
                    else
                    {
                        if (pt.X < Xmid)
                        {
                            SWchild.AddPoint(pt);
                        }
                        else
                        {
                            SEchild.AddPoint(pt);
                        }
                    }
                }

                // Remove this node's Points list.
                Points = null;
            }

            // Add the point to the appropriate subtree.
            if (Points != null)
            {
                Points.Add(newPoint);
            }
            else if (newPoint.Y < Ymid)
            {
                if (newPoint.X < Xmid)
                {
                    NWchild.AddPoint(newPoint);
                }
                else
                {
                    NEchild.AddPoint(newPoint);
                }
            }
            else
            {
                if (newPoint.X < Xmid)
                {
                    SWchild.AddPoint(newPoint);
                }
                else
                {
                    SEchild.AddPoint(newPoint);
                }
            }
        }