当前位置: 首页 > news >正文

四叉树实现四边形网格

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
# 四叉树节点
class QuadNode:def __init__(self, x, y, width, height, depth):self.x = xself.y = yself.width = widthself.height = heightself.depth = depthself.children = []self.id = None  # 单元编号def is_leaf(self):return len(self.children) == 0# 四叉树
class Quadtree:def __init__(self, x, y, width, height, max_depth):self.root = QuadNode(x, y, width, height, 0)self.max_depth = max_depthdef subdivide(self, node):if node.depth >= self.max_depth:returnw, h = node.width / 2, node.height / 2d = node.depth + 1node.children = [QuadNode(node.x,       node.y,       w, h, d),QuadNode(node.x + w,   node.y,       w, h, d),QuadNode(node.x,       node.y + h,   w, h, d),QuadNode(node.x + w,   node.y + h,   w, h, d)]for child in node.children:self.subdivide(child)def get_leaf_nodes(self, node=None):if node is None:node = self.rootif node.is_leaf():return [node]else:leaves = []for child in node.children:leaves.extend(self.get_leaf_nodes(child))return leaves# 编号 & 绘图函数
def draw_quadtree_with_numbering(quadtree):fig, ax = plt.subplots(figsize=(8, 8))leaves = quadtree.get_leaf_nodes()# 单元编号排序:先按y排,再按x排(自底向上、左到右)leaves.sort(key=lambda n: (n.y, n.x))# 给单元编号for i, node in enumerate(leaves, 1):node.id = i# 统计所有节点坐标,去重node_coords = set()for node in leaves:corners = [(node.x, node.y),(node.x + node.width, node.y),(node.x, node.y + node.height),(node.x + node.width, node.y + node.height)]node_coords.update(corners)# 节点编号排序:按 y, x 排序(自底向上、左到右)sorted_nodes = sorted(list(node_coords), key=lambda p: (p[1], p[0]))node_id_map = {coord: idx+1 for idx, coord in enumerate(sorted_nodes)}# 画单元格和编号for node in leaves:rect = patches.Rectangle((node.x, node.y), node.width, node.height,linewidth=1, edgecolor='black', facecolor='none')ax.add_patch(rect)# 单元编号(圆圈 + 数字)center_x = node.x + node.width / 2center_y = node.y + node.height / 2circle = patches.Circle((center_x, center_y), radius=node.width * 0.08,edgecolor='blue', facecolor='lightyellow', linewidth=1)ax.add_patch(circle)ax.text(center_x, center_y, str(node.id), fontsize=8, color='red', ha='center', va='center')# 画节点及编号for coord in sorted_nodes:ax.plot(coord[0], coord[1], 'ko', markersize=3)ax.text(coord[0], coord[1], str(node_id_map[coord]), fontsize=7, color='green',ha='center', va='center', bbox=dict(boxstyle="circle,pad=0.15", fc="white", ec="green", lw=0.8))ax.set_xticks([])ax.set_yticks([])ax.set_xticklabels([])ax.set_yticklabels([])ax.axis('off')plt.show()# 生成单元编号,顺序为左下、左上、右上、右下(逆时针)# 生成单元顶点编号数组,顺序为逆时针elements = []for node in leaves:ll = (node.x, node.y)  # 左下ul = (node.x, node.y + node.height)  # 左上ur = (node.x + node.width, node.y + node.height)  # 右上lr = (node.x + node.width, node.y)  # 右下cell_ids = [node_id_map[ll], node_id_map[lr], node_id_map[ur], node_id_map[ul]]elements.append(cell_ids)elements = np.array(elements)  # NC x 4nodes = np.array(sorted_nodes)  # NN x 2return elements, nodesif __name__ == "__main__":tree = Quadtree(0, 0, 1, 1, max_depth=1)tree.subdivide(tree.root)elements, nodes = draw_quadtree_with_numbering(tree)print(f"单元数量: {len(elements)}")print(f"节点数量: {len(nodes)}")# 打印前5个单元和节点print("前5个单元:")for e in elements[:5]:print(e)print("前5个节点:")for n in nodes[:5]:print(n)

在这里插入图片描述
cells
[1 2 4 3]
Nodes
[0 0]
[1 0]
[0 1]
[1 1]

在这里插入图片描述

相关文章:

  • MATLAB中的table数据类型:高效数据管理的利器
  • vr中风--模型部署
  • 短视频一键搬运 v1.7.1|短视频无水印下载 一键去重
  • lost connection to mysql server at ‘reading initial communication packet‘
  • 在力扣刷题中触摸算法的温度
  • Java的Object类
  • DeepSeek R1-0528 新开源推理模型(免费且快速)
  • ADC同步采样
  • vue3项目 前端文件下载的两种工具函数
  • OpenCv高阶(二十)——dlib脸部轮廓绘制
  • 大预言模型提示词技巧:释放AI潜力的关键
  • REALTECK瑞昱推出RTS5411T USB3.2 Gen1x1 超高速 USB 集线器控制器原厂代理分销经销一级代理分销经销
  • 每日八股文
  • 吉林大学操作系统上级实验四(hash存储讲解及顺序存储文件管理实现)
  • 图像数据与显存
  • 宝塔安装WordPress程序
  • Python打卡第39天
  • 嵌入式学习笔记 - freeRTOS任务优先级抢占,时间片抢占的实现机制
  • 330130-045-00-00 Bently Nevada 3300 XL延长电缆
  • 小白成长之路-Linux日志管理
  • 招标网站哪个好用/百度服务中心人工客服电话
  • 云南建设厅网站执业注册/推广文章的推广渠道
  • 深圳市罗湖区网站建设/简述seo的应用范围
  • 网站的风格与布局的设计方案/河南关键词优化搜索
  • 目前做网站的公司有哪些/电商平台排名
  • 零代码网站开发工具/seo网站页面优化包含