# 二叉树算法应用案例

public AtlasNode Insert(Texture2D image, int index) {
if (image == null) // Obviously an error!
return null;

if (child != null) {// If this node is not a leaf, try inserting into first child.
AtlasNode newNode = child[0].Insert(image, index);
if (newNode != null)
return newNode;

// No more room in first child, insert into second child!
return child[1].Insert(image, index);
}
else {
// If there is already a lightmap in this node, early out
if (hasImage)
return null;

// If this node is too small for the image, return
if (!ImageFits(image, rc))
return null;

// If the image is perfect, accept!
if (PerfectFit(image, rc)) {
hasImage = true;
imageRef = image;
name = imageRef.name;
sortIndex = index;
return this;
}

// If we made it this far, this node must be split.
child = new AtlasNode[2];
child[0] = new AtlasNode();
child[1] = new AtlasNode();

// Decide which way to split image
float deltaW = rc.width - image.width;
float deltaH = rc.height - image.height;

if (deltaW > deltaH) {
child[0].rc = new Rect(rc.xMin, rc.yMin, image.width, rc.height);
child[1].rc = new Rect(rc.xMin + image.width + TEXTURE_PADDING, rc.yMin, rc.width - (image.width + TEXTURE_PADDING), rc.height);
}
else {
child[0].rc = new Rect(rc.xMin, rc.yMin, rc.width, image.height);
child[1].rc = new Rect(rc.xMin, rc.yMin + image.height + TEXTURE_PADDING, rc.width, rc.height - (image.height + TEXTURE_PADDING));
}

// Lets try inserting into first child, eh?
return child[0].Insert(image, index);
}
}

public static Atlas[] CreateAtlas(string name, Texture2D[] textures, Atlas startWith = null) {
List<Texture2D> toProcess = new List<Texture2D>();
int index = toProcess.Count - 1;
toProcess.Reverse(); // Because we index backwards

List<Atlas> result = new List<Atlas>();

int insertIndex = 0;
if (startWith != null) {
insertIndex = startWith.root.sortIndex;
}

while(index >= 0) {
Atlas _atlas = startWith;
if (_atlas == null) {
_atlas = new Atlas();
_atlas.texture = new Texture2D(AtlasSize, AtlasSize, TextureFormat.RGBA32, false);
_atlas.root = new AtlasNode();
_atlas.root.rc = new Rect(0, 0, AtlasSize, AtlasSize);
}
startWith = null;

while (index >= 0 && (_atlas.root.Contains(toProcess[index].name) || _atlas.root.Insert(toProcess[index], insertIndex++) != null)) {
index -= 1;
}
_atlas.root.sortIndex = insertIndex;
insertIndex = 0;
_atlas = null;
}

foreach(Atlas atlas in result) {
atlas.root.Build(atlas.texture);
List<AtlasNode> nodes = new List<AtlasNode>();
atlas.root.GetBounds(ref nodes);
nodes.Sort(delegate (AtlasNode x, AtlasNode y) {
if (x.sortIndex == y.sortIndex) return 0;
if (y.sortIndex > x.sortIndex) return -1;
return 1;
});

List<Rect> rects = new List<Rect>();
foreach(AtlasNode node in nodes) {
Rect normalized = new Rect(node.rc.xMin / atlas.root.rc.width, node.rc.yMin / atlas.root.rc.height, node.rc.width / atlas.root.rc.width, node.rc.height / atlas.root.rc.height);
// bunp everything over by half a pixel to avoid floating errors
normalized.x += 0.5f / atlas.root.rc.width;
normalized.width -= 1.0f / atlas.root.rc.width;
normalized.y += 0.5f / atlas.root.rc.height;
normalized.height -= 1.0f / atlas.root.rc.height;
}

atlas.uvRects = new AtlasDescriptor[rects.Count];
for (int i = 0; i < rects.Count; i++) {
atlas.uvRects[i] = new AtlasDescriptor();
atlas.uvRects[i].width = (int)nodes[i].rc.width;
atlas.uvRects[i].height = (int)nodes[i].rc.height;
atlas.uvRects[i].name = nodes[i].name;
atlas.uvRects[i].uvRect = rects[i];
}

atlas.root.Clear();
#if DEBUG_ATLASES
atlas.texture.Apply(false, false);
SaveAtlas(atlas, name);
#else
if (atlas != result[result.Count - 1])
atlas.texture.Apply(false, true);
else
atlas.texture.Apply(false, false);
#endif
}

return result.ToArray();
}

if(d<100) state=嘲笑，单挑;
else if(d<200) state=单挑;
else if(d<300) state=嗜血魔法;
else if(d<400) state=呼唤同伴;
else state=逃跑;


　if(d>=200)&&(d<300) state=嗜血魔法;
else if(d>=300)&&(d<500) state=呼唤同伴;
else if(d>=100)&&(d<200) state=单挑;
else if(d<100) state=嘲笑，单挑;
else state=逃跑;


## 总结

jxw167

• 擅长领域：
• 3D引擎架构
• 服务器架构
• GPU渲染
• 客户端架构
• 引擎优化

#### 关于二叉树结构的一个应用小实例

2013年04月25日 199KB 下载

#### 二叉树的好处（应用）

2012-11-30 17:13:03

2011年11月10日 下载

#### 一个二叉排序树的实际例子

2016-05-08 11:20:04

#### 二叉树的经典技巧及算法 I

2015-09-10 22:29:11

#### 二叉树的应用详解 - 数据结构

2012-06-25 11:45:05

#### 构建二叉树实例

2014-03-17 15:12:18

#### 用递归思想解决二叉树的实际应用

2016-04-28 13:46:07

#### 二叉树及其应用--二叉树的应用

2016-06-28 19:34:06

#### 【每日算法】二叉树的遍历

2016-02-25 00:17:25