moon add moonbit-community/Quadtreefn example_create_quadtree() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let tree = new(boundary)
}fn example_insert_and_query() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let mut tree = new(boundary)
// Insert points
tree = insert(tree, Point::{ x: 10.0, y: 20.0 }, "point1", 4)
tree = insert(tree, Point::{ x: 30.0, y: 40.0 }, "point2", 4)
// Rectangular query
let result = Array::new()
query(tree, Rect::{ x: 0.0, y: 0.0, width: 50.0, height: 50.0 }, result)
assert_eq(result.length(), 2)
}fn example_advanced_queries() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let mut tree = new(boundary)
// Insert points
tree = insert(tree, Point::{ x: 15.0, y: 25.0 }, "point1", 4)
tree = insert(tree, Point::{ x: 50.0, y: 50.0 }, "point2", 4)
// Circular query
let center = Point::{ x: 15.0, y: 25.0 }
let radius : Float = 10.0
let circle_result = Array::new()
query_circle(tree, center, radius, circle_result)
// Polygonal query
let polygon = Array::new()
polygon.push(Point::{ x: 0.0, y: 0.0 })
polygon.push(Point::{ x: 50.0, y: 0.0 })
polygon.push(Point::{ x: 25.0, y: 50.0 })
let polygon_result = Array::new()
query_polygon(tree, polygon, polygon_result)
// Ray query
let origin = Point::{ x: 0.0, y: 0.0 }
let direction = Point::{ x: 1.0, y: 1.0 }
let ray_result = Array::new()
query_ray(tree, origin, direction, 100.0, ray_result)
// Sector query
let sector_center = Point::{ x: 0.0, y: 0.0 }
let start_angle : Float = 0.0
let end_angle = @math.PI.to_float() / 2.0
let sector_radius : Float = 50.0
let sector_result = Array::new()
query_sector(tree, sector_center, start_angle, end_angle, sector_radius, sector_result)
} fn example_spatial_analysis() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let mut tree = new(boundary)
// Insert test points
tree = insert(tree, Point::{ x: 10.0, y: 20.0 }, 5.0, 4)
tree = insert(tree, Point::{ x: 15.0, y: 25.0 }, 7.0, 4)
tree = insert(tree, Point::{ x: 20.0, y: 30.0 }, 6.0, 4)
tree = insert(tree, Point::{ x: 80.0, y: 80.0 }, 8.0, 4)
tree = insert(tree, Point::{ x: 85.0, y: 85.0 }, 9.0, 4)
// K-nearest neighbors
let target = Point::{ x: 12.0, y: 22.0 }
let nearest = find_nearest(tree, target, 3)
assert_eq(nearest.length(), 3)
// nearest contains the 3 closest points to target
// Find density hotspot
let (hotspot_rect, count) = find_hotspot(tree, 10)
assert_true(count > 0)
// hotspot_rect contains the area with highest point density
// Spatial autocorrelation (Moran's I)
let autocorr = spatial_autocorrelation(tree)
assert_true(autocorr >= -1.0 && autocorr <= 1.0)
// autocorr indicates spatial clustering: >0 = clustered, <0 = dispersed
// Get tree statistics
let point_count = count(tree)
let tree_depth = depth(tree)
let (leaf_count, internal_count) = count_nodes(tree)
assert_eq(point_count, 5)
assert_true(tree_depth > 0)
assert_eq(leaf_count, 1)
assert_eq(internal_count, 0)
}fn example_tree_operations() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let tree1 = new(boundary)
let tree2 = new(boundary)
// Merge two trees
let merged = merge(tree1, tree2, 4)
// Find intersection
let intersect = intersection(tree1, tree2, 4)
// Find difference
let diff = difference(tree1, tree2, 4)
// Filter points
let filtered = filter(tree1, fn(pt, val) -> Bool { pt.x > 10.0 }, 4)
}fn example_clustering() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let mut tree = new(boundary)
// Insert points
tree = insert(tree, Point::{ x: 10.0, y: 20.0 }, "point1", 4)
tree = insert(tree, Point::{ x: 30.0, y: 40.0 }, "point2", 4)
tree = insert(tree, Point::{ x: 50.0, y: 60.0 }, "point3", 4)
// DBSCAN clustering
let clusters = dbscan_cluster(tree, 5.0, 3)
// Note: With these points, clusters.length() will be 0 because
// the points are too far apart (distance > 5.0)
assert_eq(clusters.length(), 0)
}fn example_serialization() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let mut tree = new(boundary)
// Insert points
tree = insert(tree, Point::{ x: 10.0, y: 20.0 }, "point1", 4)
// Serialize to JSON
let json = serialize(tree)
assert_true(json.contains("\"type\""))
assert_true(json.contains("\"boundary\""))
}fn example_memory_management() {
let boundary = Rect::{ x: 0.0, y: 0.0, width: 100.0, height: 100.0 }
let mut tree = new(boundary)
// Remove points
tree = remove(tree, Point::{ x: 10.0, y: 20.0 }, 4)
// Remove range
tree = remove_range(tree, Rect::{ x: 0.0, y: 0.0, width: 25.0, height: 25.0 }, 4)
// Compress empty nodes
tree = compress_node(tree)
// Clear all points
tree = clear(boundary)
}pub trait ToFloat {
to_float(Self) -> Float
}pub(all) struct Point {
x : Float
y : Float
}pub(all) struct Rect {
x : Float
y : Float
width : Float
height : Float
}fn[T] batch_update(node : QuadtreeNode[T], updates : Array[(Point, T)], capacity : Int) -> QuadtreeNode[T]fn[T] difference(tree1 : QuadtreeNode[T], tree2 : QuadtreeNode[T], capacity : Int) -> QuadtreeNode[T]fn[T] filter(node : QuadtreeNode[T], predicate : (Point, T) -> Bool, capacity : Int) -> QuadtreeNode[T]fn[T] insert_adaptive(node : QuadtreeNode[T], point : Point, value : T, min_capacity : Int, max_capacity : Int, density_threshold : Float) -> QuadtreeNode[T]fn[T] insert_many(node : QuadtreeNode[T], points : Array[Point], values : Array[T], capacity : Int) -> QuadtreeNode[T]fn[T] intersection(tree1 : QuadtreeNode[T], tree2 : QuadtreeNode[T], capacity : Int) -> QuadtreeNode[T]fn[T : Eq] move_point(node : QuadtreeNode[T], old_point : Point, new_point : Point, capacity : Int) -> QuadtreeNode[T]fn[T] query_circle(node : QuadtreeNode[T], center : Point, radius : Float, result : Array[(Point, T)]) -> Unitfn[T] query_polygon(node : QuadtreeNode[T], polygon : Array[Point], result : Array[(Point, T)]) -> Unitfn[T] query_ray(node : QuadtreeNode[T], origin : Point, direction : Point, max_distance : Float, result : Array[(Point, T)]) -> Unitfn[T] query_sector(node : QuadtreeNode[T], center : Point, start_angle : Float, end_angle : Float, radius : Float, result : Array[(Point, T)]) -> Unitfn[T] update(node : QuadtreeNode[T], point : Point, new_value : T, capacity : Int) -> QuadtreeNode[T]fn[T] update_incremental(node : QuadtreeNode[T], old_point : Point, new_point : Point, value : T, capacity : Int) -> QuadtreeNode[T]Install
Download zip