I Want To Create Quadtree

Anonymous
timer Asked: Jul 14th, 2013
account_balance_wallet $5

Question Description

I create Quad tree code . I want to get 10 point from database and put them in Quad tree's leaves but it doesn't work correctly.for example random point is (449.6745,411.1505) and range detected is 88 < x < 89 and 411 < y < 412 but it isn't correct . range should be 449 < x < 450 and 411 < y < 412 . Thanks Here is my code:

public class QuadTree
{
    public TreeNode MainRoot;             
    bool Switch = false;                         
 
    public QuadTree(Rectangle R)          
    {
        MainRoot = new TreeNode();
        MainRoot.Rectangle = R;
        MainRoot.LeftChild = null;
        MainRoot.RightChild = null;
    }
 
    public void CreateTree(TreeNode Root, Rectangle R, bool Switch)
    {
        if (Root.Rectangle.Width <=1 && Root.Rectangle.Height <=1 )
        {
            return;
        }
        Root.LeftChild = new TreeNode();
        Root.RightChild = new TreeNode();
        if (!Switch)
        {
            Root.LeftChild.Rectangle = new Rectangle(R.X, R.Y, R.Width / 2, R.Height);
            Root.RightChild.Rectangle = new Rectangle(R.X + R.Width / 2, R.Y, R.Width / 2, R.Height);
        }
 
        if (Switch)
        {
            Root.LeftChild.Rectangle = new Rectangle(R.X, R.Y, R.Width, R.Height / 2);
            Root.RightChild.Rectangle = new Rectangle(R.X, R.Y - R.Height / 2, R.Width, R.Height / 2);
        }
 
        CreateTree(Root.LeftChild, Root.LeftChild.Rectangle, !Switch);
        CreateTree(Root.RightChild, Root.RightChild.Rectangle, !Switch);
    }
 

    //*****************************************************************************************

 
    double x, y;
    public void SearchPoint(TreeNode Root)
    {
        bool sw = false;
 
        SqlConnection scon = new SqlConnection("Data Source=GATEWAY-PC;Initial Catalog=QuadTree;Integrated Security=True");
        scon.Open();
        SqlCommand scom = new SqlCommand("delete from PointArea", scon);
        scom.ExecuteNonQuery();
 
        for (int i = 1; i <= 10; i++)
        {
            TreeNode Current = new TreeNode();
            TreeNode T = new TreeNode();
            T = Root;
            Current = Root;
            SqlDataReader s = new SqlCommand("select X,Y from Coordinate where ID='" + i + "'", scon).ExecuteReader();
            while (s.Read())
            {
                x = Convert.ToDouble(s["X"]);
                y = Convert.ToDouble(s["Y"]);
            }
            s.Close();
 
            while (Current != null /*&& (Current.Rectangle.Width > 1 || Root.Rectangle.Height > 1)*/)
            {
                if (!sw)
                {
                    if (x >= Current.Rectangle.X && x <= (Current.Rectangle.X + Current.Rectangle.Width / 2))
                    {
                        T = Current;
                        Current = Current.LeftChild;
 
                    }
                    else if (x >= (Current.Rectangle.X + Current.Rectangle.Width / 2) && x <= Current.Rectangle.X + Current.Rectangle.Width)
                    {
                        T = Current;
                        Current = Current.RightChild;
                    }
                }
                if (sw)
                {
 
                    if (y <= Current.Rectangle.Y && y >= (Current.Rectangle.Y - Current.Rectangle.Height / 2))
                    {
                        T = Current;
                        Current = Current.LeftChild;
                    }
                    else if (y <= (Current.Rectangle.Y - Current.Rectangle.Height / 2) && y >= Current.Rectangle.Y - Current.Rectangle.Height)
                    {
                        T = Current;
                        Current = Current.RightChild;
                    }
                }
                sw = !sw;
            }
            SqlCommand sq = new SqlCommand("insert into PointArea values('" + i + "','" + T.Rectangle.X + "','" + (T.Rectangle.X + T.Rectangle.Width) + "','" + (T.Rectangle.Y - T.Rectangle.Height) + "' ,'" + T.Rectangle.Y + "')", scon);
            sq.ExecuteNonQuery();
            T.PointX = x;
            T.PointY = y;
        }
        scon.Close();
 
    }

Tutor Answer

fitting k
School: Rice University

Thank you for the opportunity to help you with your question!

In most cases, the discipline is self-governed by the entities which require the programming, and sometimes very strict environments are defined

Please let me know if you need any clarification. I'm always happy to answer your questions.

flag Report DMCA
Review

Anonymous
Goes above and beyond expectations !

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors