void UpdateQtree(QTree qTree, IEntity e) { if (qTree.Check((int)e.Pos.x, (int)e.Pos.y)) { if (e.Depth > qTree.depth) { var childLen = qTree.Length / 2; var subLen = qTree.Length / 4; if (qTree.Child == null) { qTree.Child = new QTree[4]; qTree.Child[0] = new QTree(qTree.position + new Vector2(subLen, subLen), childLen, qTree.depth + 1); qTree.Child[1] = new QTree(qTree.position + new Vector2(-subLen, -subLen), childLen, qTree.depth + 1); qTree.Child[2] = new QTree(qTree.position + new Vector2(subLen, -subLen), childLen, qTree.depth + 1); qTree.Child[3] = new QTree(qTree.position + new Vector2(-subLen, subLen), childLen, qTree.depth + 1); } foreach (var q in qTree.Child) { UpdateQtree(q, e); } } else if (e.Depth == qTree.depth) { qTree.es.Add(e); } } }
public QTree(double value, Type type, QTree left, QTree right) { this.value = value; this.type = type; this.left = left; this.right = right; }
//leaf node public QTree(double value) { this.value = value; this.type = Type.Null; this.left = null; this.right = null; }
private void GizomoDrawQtree(QTree root) { if (!GeometryUtility.TestPlanesAABB(_planes, root.Bounds)) { return; } if (root.depth == depth || debugAlways) { if (root.depth == depth) { Gizmos.color = Color.red; if (root.es.Count > 0) { Gizmos.DrawCube(new Vector3(root.Bounds.center.x, 0.01f * root.depth, root.Bounds.center.z), root.Bounds.size - new Vector3(0.1f, 0, 0.1f)); } } else { Gizmos.color = debugColor[root.depth]; Gizmos.DrawCube(new Vector3(root.Bounds.center.x, 0.01f * root.depth, root.Bounds.center.z), root.Bounds.size - new Vector3(0.1f, 0, 0.1f)); } } if (root.Child == null) { return; } foreach (var qTree in root.Child) { GizomoDrawQtree(qTree); } }
public QTreeExt(QTree qt, bool isVisited) { this.value = qt.value; this.type = qt.type; this.left = qt.left; this.right = qt.right; this.isVisited = isVisited; }
//intermediate node public QTree(Type type, QTree left, QTree right) { Assert.IsTrue(type != Type.Null); this.value = double.NaN; this.type = type; this.left = left; this.right = right; }
public QNode(Bound bound, int depth, QNode father, QTree qTree, QTNodeType type) { this.Bound = bound; this.Depth = depth; this.Father = father; this.BelongedTree = qTree; this.Type = type; ObjList = new List <Obstacle>(); ChildList = new List <QNode>(); }
public static void FixedUpdateObs(QTree qTree) { /*foreach (Obstacle obs in Obss) * { * qTree.UpdateObj(obs); * }*/ foreach (Obstacle obs in Obss) { obs.HighlightSelf(); } }
public static QTree build(List <RandNode> operands, List <RandNode> operators) { //operands stack is always having 1 more element than operators Stack <QTree> opdStack = new Stack <QTree> (); Stack <QTree> oprStack = new Stack <QTree> (); // foreach (RandNode rn in operands) // Debug.Log (rn.number); // foreach (RandNode rn in operators) // Debug.Log (rn.type); // // Debug.Log ("==================="); // List <RandNode> .Enumerator opdItr = operands.GetEnumerator(); opdItr.MoveNext(); opdStack.Push(new QTree(opdItr.Current.number)); opdItr.MoveNext(); foreach (RandNode node in operators) { //keep poping if the next operator is not having higher precedence while (oprStack.Count > 0 && !QTree.higherPriority(node.type, oprStack.Peek().type)) { QTree opr = oprStack.Pop(); opr.right = opdStack.Pop(); opr.left = opdStack.Pop(); // Debug.Log (opr.type + " | " + opr.left.value + " | " + opr.right.value); opdStack.Push(opr); } //pushing a new operator in stack oprStack.Push(new QTree(node.type, null, null)); opdStack.Push(new QTree(opdItr.Current.number)); opdItr.MoveNext(); } //drain the oprStack while (oprStack.Count > 0) { QTree opr = oprStack.Pop(); opr.right = opdStack.Pop(); opr.left = opdStack.Pop(); // Debug.Log (opr.type + " | " + opr.left.value + " | " + opr.right.value); opdStack.Push(opr); } return(opdStack.Pop()); }
private static int getScore(QTree root) { if (root.type == null || root.type == QTree.Type.Null) { return((int)(root.value / 20) + 1); //every 20, the score is increase by 1 } else { int oprScore = getOperatorScore(root.type); int leftScore = getScore(root.left); int rightScore = getScore(root.right); return(oprScore * (leftScore + rightScore)); } }
public static Question generate() { List <RandNode> opdNodes = new List <RandNode> (); List <RandNode> oprNodes = new List <RandNode> (); string questionStr = ""; RandNode rn = getOpd(GlobalSettings.largestNum); opdNodes.Add(rn); questionStr += rn.number.ToString(); for (int i = 0; i < GlobalSettings.numberOfOperators - 1; i++) { RandNode opr = getOps(); /** Deal with divide, we only want integer for dividen **/ RandNode opd; if (opr.type == QTree.Type.Div) { opd = getValidDivisor(getPrevDiv(oprNodes, opdNodes, opdNodes.Count - 1)); } else { opd = getOpd(GlobalSettings.largestNum); } //Add them in opdNodes.Add(opd); oprNodes.Add(opr); questionStr += " " + QTree.getOperatorStr(opr.type) + " " + opd.number.ToString(); } //Debug.Log (questionStr); QTree root = QTreeBuilder.build(opdNodes, oprNodes); int rightAnswer = (int)root.evaluate(); int score = getScore(root); string explaination = getExplaination(root); Debug.Log("Explaination " + explaination + ", " + score); Question question = new Question(questionStr, rightAnswer, score, explaination); //Debug.Log ("The correct answer is " + question.answer.ToString ()); return(question); }
/* * 5 + 6 * 3 / 2 - 3 * =5 + 18 / 2 - 3 * =5 + 9 -3 * =14 - 3 * =11 * BFS to a stack, then pop out * */ private static string getExplaination(QTree root) { Stack <string> exp = new Stack <string> (); //bfs Stack <QTreeExt> nodes = new Stack <QTreeExt>(); nodes.Push(new QTreeExt(root, false)); while (nodes.Count > 0) { Stack <QTreeExt> nextLevel = new Stack <QTreeExt> (); string levelRes = ""; while (nodes.Count > 0) { QTreeExt qt = nodes.Pop(); if (qt.isVisited) { levelRes += QTree.getOperatorStr(qt.type); } else { levelRes += qt.evaluate(); if (qt.left != null && qt.right != null) { nextLevel.Push(new QTreeExt(qt.left, false)); nextLevel.Push(new QTreeExt(qt, true)); //make sure we don't visit this again nextLevel.Push(new QTreeExt(qt.right, false)); } } } nodes = nextLevel; exp.Push(levelRes); } //pop the result stack string result = " "; while (exp.Count > 0) { result += exp.Pop(); result += "\n="; } return(result); }
private void DrawQtree(QTree qTree) { if (GeometryUtility.TestPlanesAABB(_planes, qTree.Bounds)) { testCount++; foreach (var e in qTree.es) { DrawEntity(e); } if (qTree.Child != null) { foreach (var tree in qTree.Child) { DrawQtree(tree); } } } }
void Start() { int mapSize = minSize * (int)Mathf.Pow(2, depth); Debug.Log($"map size = {mapSize}"); root = new QTree(Vector2.zero, mapSize, 0); IEntity[] es = new IEntity[count]; for (int i = 0; i < count; i++) { es[i] = new TestEntity() { Pos = new Vector2(Random.Range(-mapSize / 2, mapSize / 2), Random.Range(-mapSize / 2, mapSize / 2)), Type = Random.Range(0, 100), Depth = depth, }; } _camera = GetComponent <Camera>(); root.BuildQTree(es); }
public static void BulletsUpdate(QTree qTree) { foreach (Bullet b in Bullets) { b.Move(); b.destroySelfOT -= Time.deltaTime; } LinkedListNode <Bullet> _node = Bullets.First; while (_node != null) { Bullet _b = _node.Value; _node = _node.Next; if (_b.destroySelfOT <= 0) { _b.DestroySelf(); } else { qTree.SearchNode(_b); } } }
public QNode(List <Obstacle> intersectionObjs, Bound bound, int depth, QNode father, QTree qTree, QTNodeType type) { this.Bound = bound; this.Depth = depth; this.Father = father; this.BelongedTree = qTree; this.Type = type; ObjList = new List <Obstacle>(); ChildList = new List <QNode>(); List <Obstacle> _LTlist = new List <Obstacle>(); List <Obstacle> _RTlist = new List <Obstacle>(); List <Obstacle> _RBlist = new List <Obstacle>(); List <Obstacle> _LBlist = new List <Obstacle>(); List <Obstacle> _Rootlist = new List <Obstacle>(); List <List <Obstacle> > _AllLists = new List <List <Obstacle> > { _LTlist, _RTlist, _RBlist, _LBlist, _Rootlist }; foreach (Obstacle obj in intersectionObjs) { if (obj.isLoaded == false) { #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; } #endregion #region 该物体穿过多个子节点 if (_intersectionTimes >= 2) { _Rootlist.Add(obj); } #endregion #region 完全在某个子节点内 else if (_intersectionTimes == 1) { for (int i = 0; i < 4; i++) { if (_blist[i]) { _AllLists[i].Add(obj); break; } } } #endregion else { throw new Exception("正在检测父节点外的物体"); } } } int _totalCnt = 0; foreach (List <Obstacle> list in _AllLists) { _totalCnt += list.Count(); } if (_totalCnt <= BelongedTree.MaxObjCnt || Depth >= BelongedTree.MaxDepth) { #region 直接全部放入父节点 foreach (List <Obstacle> list in _AllLists) { foreach (Obstacle obj in list) { AddObj(obj); } } #endregion } else { #region 父节点物体放入根节点,子节点物体继续向下递归 foreach (Obstacle obj in _Rootlist) { AddObj(obj); } #region 递归创建四个节点 for (int i = 0; i < 4; i++) { if (_AllLists[i].Count() != 0) { QTNodeType _type = (QTNodeType)i; ChildList.Add(new QNode(_AllLists[i], GenBound(_type), depth + 1, this, BelongedTree, _type)); } } #endregion #endregion } }
public GeneratedGrid(IGridGenerator <T> generator) { tree = new(); this.generator = generator; }
public void Start() { Player = GeneratePlayer(); qTree = new QTree(FixedInput ? LoadBoxes() : GenerateBoxs(), new Bound(0, 0, XSize, YSize), MaxObjCntPerNode, MaxDepth); //qTree.InsertObj(Player); }