Beispiel #1
0
        private void createButton_Click(object sender, EventArgs e)
        {
            try
            {
                int   numPoints = int.Parse(numPointsTextBox.Text);
                float xmin      = Radius;
                float ymin      = Radius;
                float xmax      = (pointsPictureBox.ClientSize.Width - Radius) / 3;
                float ymax      = (pointsPictureBox.ClientSize.Height - Radius) / 3;
                for (int i = 0; i < numPoints; i++)
                {
                    float x = xmin + (float)(
                        (rand.NextDouble() * xmax - xmin) +
                        (rand.NextDouble() * xmax - xmin) +
                        (rand.NextDouble() * xmax - xmin));
                    float y = ymin + (float)(
                        (rand.NextDouble() * ymax - ymin) +
                        (rand.NextDouble() * ymax - ymin) +
                        (rand.NextDouble() * ymax - ymin));
                    Root.AddPoint(new PointF(x, y));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            // Redraw.
            pointsPictureBox.Refresh();
        }
Beispiel #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);
                }
            }
        }