Programming
I Want To Create Quadtree

Question Description

I need support with this Programming question so I can learn better.

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();
 
    }

Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

Final Answer

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.

fitting k (56)
Rice University

Anonymous
Top quality work from this tutor! I’ll be back!

Anonymous
Just what I needed… fantastic!

Anonymous
Use Studypool every time I am stuck with an assignment I need guidance.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

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