 # I Want To Create Quadtree

User Generated

Ubzn Fu

Programming

### 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;

{
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();
{
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. In most cases, the discipline is self-governed by the entities which require the programming, and sometimes very strict environments are defined svggvat x (56)
Rice University Anonymous
Just the thing I needed, saved me a lot of time. Studypool 4.7 Trustpilot 4.5 Sitejabber 4.4