Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想创建四叉树_C#_Quadtree - Fatal编程技术网

C# 我想创建四叉树

C# 我想创建四叉树,c#,quadtree,C#,Quadtree,我想创建一个四叉树,但它不能正常工作。我得到一个矩形,然后将其分割,直到宽度和高度大于1。但它不能正确地划分。你能帮助我吗? 谢谢 这是我的密码 public class QuadTree { public TreeNode MainRoot; bool Switch=false; public QuadTree(Rectangle R) { MainR

我想创建一个四叉树,但它不能正常工作。我得到一个矩形,然后将其分割,直到宽度和高度大于1。但它不能正确地划分。你能帮助我吗? 谢谢 这是我的密码

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

        Switch = !Switch;
        CreateTree(Root.LeftChild, Root.LeftChild.Rectangle);
        CreateTree(Root.RightChild, Root.RightChild.Rectangle);
    }

}

你说的“分割为真”是什么意思?这是为了好玩还是你需要一起工作?代码丛中的项目正是这样做的…例如,我有一个10*10的矩形,我想把它分开,最后我想要100个1*1的矩形。。。但是我的代码不起作用。为什么是R.Y-R.Height/2而不是R.Y+R.Height/2?另外,您可能应该在递归调用中传递参数开关,而不是操纵字段。