public Init ( GameObject startItem, int depth ) : void | ||
startItem | GameObject | |
depth | int | |
Résultat | void |
/// <summary> /// 案例1:添加N个圆形区域 /// </summary> void Case1() { _case = 1; _quadTree.Init(new Rect(0, 0, MapSize, MapSize)); for (var i = 0; i < ObjectCount; i++) { var radius = Random.Range(1, ObjectSize); var pos = new float2(Random.Range(-MapSize / 2, MapSize / 2), Random.Range(-MapSize / 2, MapSize / 2)); _quadTree.AddObject(new TestQuadTreeData(), new Circle(radius, pos)); if (Debug) { var obj = Instantiate(Sphere); obj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; obj.name = "Sphere:" + i; obj.transform.position = new Vector3(pos.x, 0, pos.y); obj.transform.localScale = new Vector3(radius, radius, radius); } } if (Debug) { _quadTree.Output(10); } _quadTree.FakeClear(); }
void CreatQuadTree() { m_QuadTree = new QuadTree <QTObject>(); m_QuadTree.Init(m_rect, m_QTGameObjectList); #if UNITY_EDITOR //绘制四叉树 var root = m_QuadTree.Root; DrawQuadTree(root, null); #endif }
/// <summary> /// 案例8:添加N个任意朝向的矩形区域 /// 删除随机区域内的矩形 /// 调用FindNearObject方法获取Start节点附近的对象 /// (拖动StartPos可以颜色发生变化的就是查询结果) /// </summary> void Case8() { _case = 8; _quadTree2.Init(new Rect(0, 0, MapSize, MapSize)); for (var i = 0; i < ObjectCount; i++) { var size = new float2(Random.Range(1, ObjectSize), Random.Range(1, ObjectSize)); var pos = new float2(Random.Range(-MapSize / 2, MapSize / 2), Random.Range(-MapSize / 2, MapSize / 2)); var forward = math.normalizesafe(new float2(Random.Range(-1f, 1f), Random.Range(-1f, 1f))); if (math.lengthsq(forward) < 0.1f) { forward = new float2(0, 1); } //if (Debug) { var obj = Instantiate(Cube); obj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; obj.name = "Cube:" + i; obj.transform.position = new Vector3(pos.x, 0, pos.y); obj.transform.localScale = new Vector3(size.x * 2, 1, size.y * 2); obj.transform.rotation = quaternion.LookRotation(new float3(forward.x, 0, forward.y), new float3(0, 1, 0)); _quadTree2.AddObject(new TestData2() { Obstacle = obj }, new AnyForwardRect(size, pos, forward)); } } for (var i = 0; i < ObjectCount; i++) { var size = new float2(Random.Range(1, ObjectSize), Random.Range(1, ObjectSize)); var pos = new float2(Random.Range(-MapSize / 2, MapSize / 2), Random.Range(-MapSize / 2, MapSize / 2)); _quadTree2.RemoveAllObjectsInRect(pos - size, pos + size); } _startPos = new GameObject("StartPos"); if (Debug) { _quadTree2.Output(1000); } }
public static void Test() { QuadTree quadTree = new QuadTree(); quadTree.Init(-90, 90, -180, 180); Dictionary<Vector2, int> posDic = new Dictionary<Vector2, int>(); System.Random random = new System.Random(0); for (int i = 0; i < 100000; i++) { Vector2 pos = new Vector2(); pos.x = (float)(random.Next() % 360 - 180 + (float)(random.Next() % 1000) / 1000); pos.y = (float)(random.Next() % 180 - 90 + (float)(random.Next() % 1000) / 1000); if (posDic.ContainsKey(pos)) { posDic[pos] += 1; } else { QuadTreeElement element = new QuadTreeElement(); element.lng = pos.x; element.lat = pos.y; //Debug.Log("尝试InsertElement " + i.ToString() + " " + element.lng + " " + element.lat); quadTree.InsertElement(element); posDic.Add(pos, 1); } } QuadTreeElement test = new QuadTreeElement(); test.lng = -24f; test.lat = -45.4f; List<QuadTreeElement> retList = quadTree.queryEle(test); Debug.Log("附近有 " + retList.Count + " 个点"); foreach(var iter in retList) { Debug.Log(iter.lng + " " + iter.lat); } }