void Compare(SortNode current, SortNode target) { Stack <SortNode> checks = new Stack <SortNode>(); int side; current.checkDeep = 0; checks.Push(current); while (checks.Count > 0) { current = checks.Pop(); side = CaculateSide(current.rect, target.rect); if (side > 0) { //current遮挡target //继续比较和子结点的关系 for (int i = current.children.Count - 1; i >= 0l; --i) { current.children[i].checkDeep = current.checkDeep + 1; checks.Push(current.children[i]); } if (target.parent == null) { current.AddChild(target); target.checkDeep = current.checkDeep + 1; } else if (current.checkDeep >= target.checkDeep) { current.AddChild(target); target.checkDeep = current.checkDeep + 1; } } else if (side < 0) { //target遮挡current,把current做为target的潜在子对象 target.virtualChildren.Add(current); } else { //暂时没有遮挡关系,继续比较子结点。 for (int i = current.children.Count - 1; i >= 0; --i) { current.children[i].checkDeep = current.checkDeep + 1; checks.Push(current.children[i]); } } } //处理比target小的没有移过来的结点 ParseVirtualChildren(target); }
void Compare2(SortNode current, SortNode target, int deep) { int side = CaculateSide(current.rect, target.rect); if (side > 0) { //current遮挡target //继续比较和子结点的关系 CompareChildren(current, target, deep); if (target.parent == null) { current.AddChild(target); } } else if (side < 0) { //target遮挡current,把current做为target的潜在子对象 target.virtualChildren.Add(current); } else { //暂时没有遮挡关系,继续比较子结点。 CompareChildren(current, target, deep); } }
void ParseVirtualChildren(SortNode node) { SortNode virtualChild = null; Stack <SortNode> checks = new Stack <SortNode>(); checks.Push(node); while (checks.Count > 0) { node = checks.Pop(); //首先检查需子类,看看有没有要改变深度的。 //大多数时候在被检查结点插入的地方的子结点会被新加到这个结点下。 for (int i = node.virtualChildren.Count - 1; i >= 0; --i) { virtualChild = node.virtualChildren[i]; if (node.checkDeep >= virtualChild.checkDeep) { node.AddChild(virtualChild); node.virtualChildren.RemoveAt(i); virtualChild.checkDeep = node.checkDeep + 1; checks.Push(virtualChild); } } for (int i = node.children.Count - 1; i >= 0; --i) { checks.Push(node.children[i]); } } }