• 大小: 1.51MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-31
  • 语言: C#
  • 标签: c#  4叉树  

资源简介

C#实现的4叉树算法,亲测可用。适合初学者

资源截图

代码片段和文件信息

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public delegate void UQtCellChanged(UQtLeaf left UQtLeaf entered);
public delegate void UQtCellSwapIn(UQtLeaf leaf);
public delegate void UQtCellSwapOut(UQtLeaf leaf);

public static class UQtConfig
{
    // this value determines the smallest cell size
    // the space-partition process would stop dividing if cell size is smaller than this value
    public static float CellSizeThreshold = 50.0f;

    // swap-in distance of cells
    public static float CellSwapInDist = 100.0f;
    // swap-out distance of cells 
    //  (would be larger than swap-in to prevent poping)
    public static float CellSwapOutDist = 150.0f;

    // time interval to update the focus point
    // so that a new swap would potentially triggered (in seconds)
    public static float SwapTriggerInterval = 0.5f;
    // time interval to update the in/out swapping queues (in seconds)
    public static float SwapProcessInterval = 0.2f;
}

// user data stored in quadtree leaves
public interface IQtUserData
{
    Vector3 GetCenter();
    Vector3 GetExtends();

    void SwapIn();
    void SwapOut();

    bool IsSwapInCompleted();
    bool IsSwapOutCompleted();
}

public class UQtNode
{
    public UQtNode(Rect bound)
    {
        _bound = bound;
    }

    public Rect Bound { get { return _bound; } }
    protected Rect _bound;

    public virtual void SetSubNodes(UQtNode[] subNodes)
    {
        _subNodes = subNodes;
    }

    public virtual void Receive(IQtUserData userData)
    {
        if (!UQtAlgo.Intersects(Bound userData))
        {
            return;
        }

        foreach (var sub in SubNodes)
        {
            sub.Receive(userData);
        }
    }

    public UQtNode[] SubNodes { get { return _subNodes; } }
    public const int SubCount = 4;
    protected UQtNode[] _subNodes = null;
}

public class UQtLeaf : UQtNode
{
    public UQtLeaf(Rect bound) : base(bound)
    {
    }
    public override void SetSubNodes(UQtNode[] subNodes)
    {
        UCore.Assert(false);
    }

    public override void Receive(IQtUserData userData)
    {
        if (!UQtAlgo.Intersects(Bound userData))
            return;

        if (Bound.Contains(new Vector2(userData.GetCenter().x userData.GetCenter().z)))
        {
            _ownedobjects.Add(userData);            
        }
        else
        {
            _affectedobjects.Add(userData);
        }
    }

    public void SwapIn()
    {
        foreach (var obj in _ownedobjects)
            obj.SwapIn();
        foreach (var obj in _affectedobjects)
            obj.SwapIn();
    }

    public void SwapOut()
    {
        foreach (var obj in _ownedobjects)
            obj.SwapOut();
        foreach (var obj in _affectedobjects)
            obj.SwapOut();
    }

    public bool IsSwapInCompleted()
    {
        foreach (var obj in _ownedobjects)
        {
            if (!obj.IsSwapInCompleted())
                return false;
        }
        fore

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-07-19 06:56  uquadtree-master\
     文件         499  2015-07-19 06:56  uquadtree-master\.gitignore
     文件        1084  2015-07-19 06:56  uquadtree-master\LICENSE
     文件         128  2015-07-19 06:56  uquadtree-master\README.md
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree\
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree\Unity-5.0.1f1\
     文件     3470336  2015-07-19 06:56  uquadtree-master\uquadtree\Unity-5.0.1f1\UnityEditor.dll
     文件      794112  2015-07-19 06:56  uquadtree-master\uquadtree\Unity-5.0.1f1\UnityEngine.dll
     文件         974  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree.sln
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree\
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree\Properties\
     文件        1394  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree\Properties\AssemblyInfo.cs
     文件        7628  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree\UQuadtree.cs
     文件        4971  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree\UQuadtree_Internal.cs
     文件        2604  2015-07-19 06:56  uquadtree-master\uquadtree\uquadtree\uquadtree.csproj
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree_test\
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\
     文件         191  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree.meta
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\
     文件         191  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Materials.meta
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Materials\
     文件        4268  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Materials\Ground.mat
     文件         179  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Materials\Ground.mat.meta
     文件         191  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Scenes.meta
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Scenes\
     文件       34178  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Scenes\test_scene.unity
     文件         174  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\Scenes\test_scene.unity.meta
     文件         191  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\scripts.meta
     目录           0  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\scripts\
     文件        1900  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\scripts\UTestBootstrap.cs
     文件         262  2015-07-19 06:56  uquadtree-master\uquadtree_test\Assets\UQuadtree\scripts\UTestBootstrap.cs.meta
............此处省略19个文件信息

评论

共有 条评论