예제 #1
0
 private void InsertRecursive(T elem, QuadTreeBound bound)
 {
     if (m_children == null) //没有子节点,尝试进行划分
     {
         if (m_items.Count < m_capacity || m_bound.size / 2 < m_minSize)
         {
             m_items.Add(new QuadTreeItem(elem, bound));
         }
         else
         {
             Split();
             Redistribute();
             InsertRecursive(elem, bound);
         }
     }
     else
     {
         int bestFit = BestFitChild(bound.center);
         if (m_children[bestFit].Bound.Contains(bound))
         {
             m_children[bestFit].InsertRecursive(elem, bound);
         }
         else
         {
             m_items.Add(new QuadTreeItem(elem, bound));
         }
     }
 }
예제 #2
0
    public void Insert(T elem, QuadTreeBound bound)
    {
        if (!m_bound.Overlaps(bound))
        {
            return;
        }

        InsertRecursive(elem, bound);
    }
예제 #3
0
 public LooseQuadTree(QuadTreeBound bound, float baseSize, float loose, int capacity, float minSize)
 {
     m_bound    = bound;
     m_baseSize = baseSize;
     m_loose    = loose;
     m_capacity = capacity;
     m_minSize  = minSize;
     m_items    = new List <QuadTreeItem>(capacity);
 }
예제 #4
0
    public bool Overlaps(QuadTreeBound other)
    {
        if (other.xMax < xMin || other.xMin > xMax)
        {
            return(false);
        }
        if (other.yMax < yMin || other.yMin > yMax)
        {
            return(false);
        }

        return(true);
    }
예제 #5
0
 void Update()
 {
     if (m_isQuery == false && Input.GetMouseButtonUp(0))
     {
         Vector2 point = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         var     bound = new QuadTreeBound(point, m_pointSize);
         if (m_quadTree.Bound.Contains(bound))
         {
             m_Points.Add(point);
             m_quadTree.Insert(point, bound);
         }
         else
         {
             Debug.LogError("不能在区域外加点");
         }
     }
 }
예제 #6
0
    public void Query(QuadTreeBound bound, List <T> result)
    {
        if (!m_bound.Overlaps(bound))
        {
            return;
        }

        for (int i = 0; i < m_items.Count; i++)
        {
            if (bound.Overlaps(m_items[i].bound))
            {
                result.Add(m_items[i].elem);
            }
        }

        if (m_children != null)
        {
            for (int i = 0; i < m_children.Length; i++)
            {
                m_children[i].Query(bound, result);
            }
        }
    }
예제 #7
0
    public bool Remove(T elem, QuadTreeBound bound)
    {
        for (int i = 0; i < m_items.Count; i++)
        {
            if (m_items[i].elem.Equals(elem))
            {
                m_items.RemoveAt(i);
                return(true);
            }
        }

        if (m_children != null)
        {
            for (int i = 0; i < m_children.Length; i++)
            {
                if (m_children[i].Remove(elem, bound))
                {
                    return(true);
                }
            }
        }

        return(false);
    }
예제 #8
0
 public bool Contains(QuadTreeBound other)
 {
     return(xMin <= other.xMin && xMax >= other.xMax && yMin <= other.yMin && yMax >= other.yMax);
 }
예제 #9
0
    void OnGUI()
    {
        m_isQuery = GUILayout.Toggle(m_isQuery, "是否进行查询");
        if (m_isQuery)
        {
            m_showLevel = -1;
            m_showChild = -1;

            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("查询框宽度");
                int.TryParse(GUILayout.TextField(m_mouseRectWidth.ToString()), out m_mouseRectWidth);
            }

            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("查询框高度");
                int.TryParse(GUILayout.TextField(m_mouseRectHeight.ToString()), out m_mouseRectHeight);
            }
            GUILayout.EndHorizontal();

            DrawRect(m_quadTree, 0, 0);

            float         x     = Input.mousePosition.x;
            float         y     = Input.mousePosition.y;
            QuadTreeBound bound = new QuadTreeBound(x, y, m_mouseRectHeight);
            DrawRect(bound, Color.red);
            List <Vector2> queryResult = new List <Vector2>();
            m_quadTree.Query(bound, queryResult);
            for (int i = 0; i < queryResult.Count; i++)
            {
                DrawPoint(queryResult[i], Color.red);
            }
        }
        else
        {
            GUILayout.Label("左键点击产生点来查看四叉树的构建");

            if (m_showLevel >= 0)
            {
                float size = m_maxSize / Mathf.Pow(2, m_showLevel);
                GUILayout.Label("当前层大小=" + size + ", 松散大小=" + size * m_loose);
            }

            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("显示哪一层");
                int.TryParse(GUILayout.TextField(m_showLevel.ToString(), GUILayout.Width(20)), out m_showLevel);

                GUILayout.Space(5);

                GUILayout.Label("是否包含其以下层级");
                m_containsChildLevel = GUILayout.Toggle(m_containsChildLevel, string.Empty);
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("显示哪个子节点");
                int.TryParse(GUILayout.TextField(m_showChild.ToString()), out m_showChild);
            }
            GUILayout.EndHorizontal();

            DrawRect(m_quadTree, 0, 0);
        }
    }
예제 #10
0
 void DrawRect(QuadTreeBound bound, Color color)
 {
     m_mat.SetColor("_Color", color);
     GraphicsTool.DrawRect(new Vector2(bound.xMin, bound.yMin), new Vector2(bound.xMax, bound.yMax), false, m_mat);
 }
예제 #11
0
 public QuadTreeItem(T elem, QuadTreeBound bound)
 {
     this.elem  = elem;
     this.bound = bound;
 }