C# 四叉树 算法

namespace GameEngine { using Microsoft.DirectX; using System; /// /// 四叉树 算法~ /// internal class QuadTree { private QuadTree BottomLeft; private QuadTree BottomRight; public Microsoft.DirectX.Vector2 CenterPosition; private int m_BottomBegin; private int m_BottomEnd; private int m_BottomMiddle; private int m_MiddleBegin; private int m_MiddleEnd; private int m_MiddleMiddle; private int m_TopBegin; private int m_TopEnd; private int m_TopMiddle; private int m_VerticesNumX = 1; private QuadTree TopLeft; private QuadTree TopRight; public float Width; public int BuildIndices(Microsoft.DirectX.Vector3 CameraPos, Array Indices, int n) { Microsoft.DirectX.Vector2 camera = new Microsoft.DirectX.Vector2(CameraPos.X, CameraPos.Z); if (this.IsVisible(this.CenterPosition, camera)) { this.BuildNextQuadTree(); n = this.DeepIn(CameraPos, Indices, n); return n; } if (((this.m_TopEnd - this.m_TopBegin) == 1) && ((this.m_BottomBegin - this.m_TopBegin) == this.m_VerticesNumX)) { n = this.EndMe(Indices, n); return n; } bool flag = false; Microsoft.DirectX.Vector2 center = new Microsoft.DirectX.Vector2(this.CenterPosition.X - this.Width, this.CenterPosition.Y); if (this.IsVisible(center, camera)) { flag = true; } else { flag = false; } bool flag2 = false; Microsoft.DirectX.Vector2 vector3 = new Microsoft.DirectX.Vector2(this.CenterPosition.X + this.Width, this.CenterPosition.Y); if (this.IsVisible(vector3, camera)) { flag2 = true; } else { flag2 = false; } bool flag3 = false; Microsoft.DirectX.Vector2 vector4 = new Microsoft.DirectX.Vector2(this.CenterPosition.X, this.CenterPosition.Y - this.Width); if (this.IsVisible(vector4, camera)) { flag3 = true; } else { flag3 = false; } bool flag4 = false; Microsoft.DirectX.Vector2 vector5 = new Microsoft.DirectX.Vector2(this.CenterPosition.X, this.CenterPosition.Y + this.Width); if (this.IsVisible(vector5, camera)) { flag4 = true; } else { flag4 = false; } if ((!flag && !flag2) && (!flag3 && !flag4)) { n = this.EndMe(Indices, n); return n; } if ((this.m_TopEnd - this.m_TopBegin) == 1) { if (flag4 || flag3) { n = this.EndMe(Indices, n); return n; } if (flag) { Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_MiddleBegin, n++); Indices.SetValue(this.m_MiddleBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_MiddleBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); return n; } if (flag2) { Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_MiddleBegin + 1, n++); Indices.SetValue(this.m_MiddleBegin + 1, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_MiddleBegin + 1, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); return n; } } if ((this.m_BottomBegin - this.m_TopBegin) == this.m_VerticesNumX) { if (flag || flag2) { n = this.EndMe(Indices, n); return n; } if (flag3) { Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_TopMiddle, n++); Indices.SetValue(this.m_TopMiddle, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_TopMiddle, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_TopEnd, n++); return n; } if (flag4) { Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomMiddle, n++); Indices.SetValue(this.m_BottomMiddle, n++); Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomMiddle, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_TopEnd, n++); return n; } } if (flag) { if ((this.m_MiddleBegin > this.m_TopBegin) && (this.m_BottomBegin > this.m_MiddleBegin)) { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_MiddleBegin, n++); Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_MiddleBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); } } else { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); } if (flag2) { if ((this.m_BottomEnd > this.m_MiddleEnd) && (this.m_MiddleEnd > this.m_TopEnd)) { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_MiddleEnd, n++); Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_MiddleEnd, n++); Indices.SetValue(this.m_TopEnd, n++); } } else { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_TopEnd, n++); } if (flag3) { if ((this.m_TopEnd > this.m_TopMiddle) && (this.m_TopMiddle > this.m_TopBegin)) { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_TopMiddle, n++); Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_TopMiddle, n++); Indices.SetValue(this.m_TopBegin, n++); } } else { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_TopBegin, n++); } if (flag4) { if ((this.m_BottomMiddle > this.m_BottomBegin) && (this.m_BottomEnd > this.m_BottomMiddle)) { Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomMiddle, n++); Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_BottomMiddle, n++); Indices.SetValue(this.m_BottomEnd, n++); } return n; } Indices.SetValue(this.m_MiddleMiddle, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); return n; } private void BuildNextQuadTree() { float num = this.Width / 4f; float num2 = this.Width / 2f; if (((this.TopLeft == null) && (this.m_TopMiddle > this.m_TopBegin)) && (this.m_MiddleBegin > this.m_TopBegin)) { this.TopLeft = new QuadTree(); this.TopLeft.Initialize(this.m_VerticesNumX, this.m_TopBegin, this.m_TopMiddle, this.m_MiddleBegin, this.m_MiddleMiddle); this.TopLeft.Width = num2; this.TopLeft.CenterPosition = new Microsoft.DirectX.Vector2(this.CenterPosition.X - num, this.CenterPosition.Y - num); } if (((this.TopRight == null) && (this.m_TopEnd > this.m_TopMiddle)) && (this.m_MiddleMiddle > this.m_TopMiddle)) { this.TopRight = new QuadTree(); this.TopRight.Initialize(this.m_VerticesNumX, this.m_TopMiddle, this.m_TopEnd, this.m_MiddleMiddle, this.m_MiddleEnd); this.TopRight.Width = num2; this.TopRight.CenterPosition = new Microsoft.DirectX.Vector2(this.CenterPosition.X + num, this.CenterPosition.Y - num); } if (((this.BottomLeft == null) && (this.m_MiddleMiddle > this.m_MiddleBegin)) && (this.m_BottomBegin > this.m_MiddleBegin)) { this.BottomLeft = new QuadTree(); this.BottomLeft.Initialize(this.m_VerticesNumX, this.m_MiddleBegin, this.m_MiddleMiddle, this.m_BottomBegin, this.m_BottomMiddle); this.BottomLeft.Width = num2; this.BottomLeft.CenterPosition = new Microsoft.DirectX.Vector2(this.CenterPosition.X - num, this.CenterPosition.Y + num); } if (((this.BottomRight == null) && (this.m_MiddleEnd > this.m_MiddleMiddle)) && (this.m_BottomMiddle > this.m_MiddleMiddle)) { this.BottomRight = new QuadTree(); this.BottomRight.Initialize(this.m_VerticesNumX, this.m_MiddleMiddle, this.m_MiddleEnd, this.m_BottomMiddle, this.m_BottomEnd); this.BottomRight.Width = num2; this.BottomRight.CenterPosition = new Microsoft.DirectX.Vector2(this.CenterPosition.X + num, this.CenterPosition.Y + num); } } private int DeepIn(Microsoft.DirectX.Vector3 CameraPos, Array Indices, int n) { if (this.TopLeft != null) { n = this.TopLeft.BuildIndices(CameraPos, Indices, n); } if (this.TopRight != null) { n = this.TopRight.BuildIndices(CameraPos, Indices, n); } if (this.BottomLeft != null) { n = this.BottomLeft.BuildIndices(CameraPos, Indices, n); } if (this.BottomRight != null) { n = this.BottomRight.BuildIndices(CameraPos, Indices, n); } return n; } private int EndMe(Array Indices, int n) { if (n < (Indices.Length - 6)) { Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); Indices.SetValue(this.m_TopEnd, n++); Indices.SetValue(this.m_TopBegin, n++); Indices.SetValue(this.m_BottomEnd, n++); } return n; } public void Initialize(int verticesNumX, int TopBegin, int TopEnd, int BottomBegin, int BottomEnd) { this.m_VerticesNumX = verticesNumX; int num = TopEnd - TopBegin; int num2 = num / 2; this.m_TopBegin = TopBegin; this.m_TopEnd = TopEnd; this.m_TopMiddle = this.m_TopBegin + num2; this.m_BottomBegin = BottomBegin; this.m_BottomEnd = BottomEnd; this.m_BottomMiddle = this.m_BottomBegin + num2; this.m_MiddleBegin = ((((this.m_BottomBegin - this.m_TopBegin) / this.m_VerticesNumX) / 2) * this.m_VerticesNumX) + this.m_TopBegin; this.m_MiddleMiddle = this.m_MiddleBegin + num2; this.m_MiddleEnd = this.m_MiddleBegin + num; } private bool IsVisible(Microsoft.DirectX.Vector2 Center, Microsoft.DirectX.Vector2 Camera) { float num = this.Width * 5f; float num2 = Center.X - Camera.X; float num3 = Center.Y - Camera.Y; return ((((num2 > -num) && (num2 < num)) && (num3 > -num)) && (num3 < num)); } public int MiddleIndex { get { return this.m_MiddleMiddle; } } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值