// MIT License - Copyright (c) 2025 wallstop // Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE namespace Samples.UnityHelpers.SpatialStructures { using System.Collections.Generic; using UnityEngine; using WallstopStudios.UnityHelpers.Core.DataStructure; using WallstopStudios.UnityHelpers.Core.Random; /// /// Demonstrates QuadTree2D, KdTree2D, and SpatialHash2D with random points. /// public sealed class SpatialStructuresDemo : MonoBehaviour { [SerializeField] private int pointCount = 100; [SerializeField] private Vector2 areaSize = new Vector2(50f, 50f); [SerializeField] private float queryRadius = 5f; private void Start() { IRandom rng = PRNG.Instance; List points = new List(pointCount); for (int i = 0; i < pointCount; i++) { float x = rng.NextFloat(-areaSize.x * 0.5f, areaSize.x * 0.5f); float y = rng.NextFloat(-areaSize.y * 0.5f, areaSize.y * 0.5f); points.Add(new Vector2(x, y)); } Bounds bounds = new Bounds(Vector3.zero, new Vector3(areaSize.x, areaSize.y, 0.1f)); // QuadTree: radius query QuadTree2D quad = new QuadTree2D(points, p => p, bounds); List results = new List(); Vector2 origin = Vector2.zero; quad.GetElementsInRange(origin, queryRadius, results); Debug.Log($"QuadTree2D found {results.Count} points within {queryRadius} of {origin}"); // KdTree: approximate nearest neighbors KdTree2D kd = new KdTree2D(points, p => p); results.Clear(); kd.GetApproximateNearestNeighbors(origin, 8, results); Debug.Log($"KdTree2D nearest neighbors (approx): {results.Count}"); // SpatialHash: insert and query using (ISpatialHash2D grid = new SpatialHash2D(cellSize: queryRadius)) { foreach (Vector2 p in points) { grid.Insert(p, p); } results.Clear(); grid.Query(origin, queryRadius, results); Debug.Log($"SpatialHash2D found {results.Count} points within {queryRadius}"); } } } }