当前位置: 首页 > 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]

在这里插入图片描述

http://www.xdnf.cn/news/718507.html

相关文章:

  • OpenGL —— 2.9.1、摄像机之模拟CS鼠标视角转动,可切换线框模式显示(附源码,glfw+glad)
  • 力扣刷题Day 64:括号生成(22)
  • 什么是物化视图(Materialized View)?
  • Redis的大Key问题如何解决?
  • [预训练]Encoder-only架构的预训练任务核心机制
  • 【Day39】
  • 【Doris基础】Apache Doris数据模型全面解析:选择最适合你的数据组织方式
  • ProfibusDP转DeviceNet协议转换网关应用于S7-300PLC控制埃斯顿DeviceNet焊机项目
  • unity—特效闪光衣服的设置
  • 亚马逊桌布运营中的利润核算与优化:从成本管控到决策升级
  • MonoPCC:用于内窥镜图像单目深度估计的光度不变循环约束|文献速递-深度学习医疗AI最新文献
  • 5.3.1_2二叉树的层次遍历
  • 博客摘录「 Activiti7工作流引擎:基础篇(二) 自动生成表结构」2024年9月13日
  • 更换Homebrew 源
  • 【C/C++】闭包的几个用处
  • 如何用Go创建一个 deployment 到容器拉起来的全流程
  • python 制作复杂表格报告
  • Java 开发上门家政系统源码:全流程数字化管理,适配家政公司 / 个体户接单派单
  • MTK平台-- wifi 暗屏待机 low power问题分析
  • 自增长主键的优缺点分析
  • 20中数组去重的方法20种数组去重的方法
  • 在Start routine里替换掉source package里面的non-ASCII字符
  • 25平航杯复现
  • React 第四十九节 Router中useNavigation的具体使用详解及注意事项
  • 可视化图解算法47:包含min函数的栈
  • 一种监控录像视频恢复的高效解决方案,从每一帧中寻找可能性
  • Docker 部署项目
  • Windows10家庭版添加本地安全策略(需要联网)
  • NC52 有效括号序列【牛客网】
  • YOLO12改进-模块-引入AFE模块 增强模型对复杂场景(如杂乱背景、小目标、半透明物体)的特征提取能力