Ejemplo n.º 1
0
 public void InsertObj(Obstacle obj)
 {
     // 遇到最大深度的叶子,直接添加,不再递归
     if (Depth == BelongedTree.MaxDepth || ObjList.Count() < BelongedTree.MaxObjCnt)
     {
         AddObj(obj);
         return;
     }
     #region 检测与节点的几个子节点相交
     List <bool> _bList = new List <bool>
     {
         CheckIntersection(obj.Bound, QTNodeType.LT),
         CheckIntersection(obj.Bound, QTNodeType.RT),
         CheckIntersection(obj.Bound, QTNodeType.RB),
         CheckIntersection(obj.Bound, QTNodeType.LB)
     };
     int _intersectionTimes = 0;
     foreach (bool b in _bList)
     {
         _intersectionTimes += b ? 1 : 0;
     }
     if (_intersectionTimes >= 2)  //要添加的物体与多个子节点相交
     {
         //  直接加入父节点
         AddObj(obj);
         return;
     }
     else if (_intersectionTimes == 1)  // 在子节点的位置内
     {
         QNode _node;
         for (int i = 0; i < 4; i++)
         {
             if (_bList[i])
             {
                 QTNodeType _type = (QTNodeType)i;
                 _node = GetNode((QTNodeType)i);
                 if (_node == null)
                 {
                     _node = new QNode(GenBound(_type), Depth + 1, this, BelongedTree, _type);
                     ChildList.Add(_node);
                     _node.AddObj(obj);
                 }
                 else
                 {
                     _node.InsertObj(obj);
                 }
             }
         }
     }
     else
     {
         throw new Exception("该物体不在树的范围内");
     }
     #endregion
 }