Python德劳内三角剖分详解
作者:微小冷 时间:2021-02-18 23:08:49
初步认识
对于熟悉matplotlib三维画图的人来说,最常用的应该是plot_surface,但这个函数的绘图逻辑是,将xy平面映射到z轴,所以没法一次性绘制球,只能把球分成两半,上半球和下半球分别绘制。
如果想一次性绘制封闭图形,则可通过tri_surface,其绘图逻辑便是将图形拆分成一个个三角面,然后在对这些三角面进行绘制。所以,将一个曲面拆分成三角面,便构成了一个非常现实的问题,德劳内三角剖分便是建立在这个问题背景之下的。
scipy.spatial中提供了Delaunay类,下面以二维散点为例,来初步认识一下。
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
pts = np.array([[0, 0], [0, 1], [2, 0], [2, 1]])
tri = Delaunay(pts)
plt.triplot(pts[:,0], pts[:,1], tri.simplices)
plt.plot(pts[:,0], pts[:,1], 'o')
plt.show()
效果如下
构造函数和属性
Delaunay的构造函数如下
Delaunay(points, furthest_site=False, incremental=False, qhull_options=None)
各参数含义为
points 输入散点
furthest_site 为True时,计算最远点
incremental 为True时,允许增量添加点
qhull_options 为qhull参数,具体可参考qhull
在Delaunay对象中,有下面几个必须知道的常用属性
points 即输入的点集
simplices 三角面顶点在点集中的序号
neighbors 三角面相邻三角面的序号
equations 三角面方程
实战-画个球
想要画个球,第一步是要得到一个球
# N为点数
def getBall(N):
pts = []
while len(pts) < N:
while True:
u = np.random.uniform(-1, 1)
v = np.random.uniform(-1, 1)
r2 = u**2 + v**2
if r2 < 1:
break
x = 2*u*np.sqrt(1-r2)
y = 2*v*np.sqrt(1-r2)
z = 1 - 2*r2
pts.append((x,y,z))
return np.vstack(pts)
下面测试一下
pts = getBall(200)
ax = plt.subplot(projection='3d')
ax.scatter(pts[:,0], pts[:,1], pts[:,2])
plt.show()
接下来将这些随机点生成三角面,并进行绘图
tri = Delaunay(pts)
ax = plt.subplot(projection='3d')
for i in tri.simplices:
ax.plot_trisurf(pts[i, 0], pts[i, 1], pts[i,2])
plt.show()
效果如下
看上去花花绿绿的这些三角形,便是通过德劳内三角剖分得到的,其equations属性可以查看这些三角面的方程参数
>>> tri.equations
array([[-2.35739179e-16, -1.64155539e-15, -1.54600295e-15,
-1.00000000e+00, 2.41181971e-16],
[-2.35739179e-16, -1.64155539e-15, -1.54600295e-15,
-1.00000000e+00, 2.41181971e-16],
[-2.35739179e-16, -1.64155539e-15, -1.54600295e-15,
-1.00000000e+00, 2.41181971e-16],
...,
[-2.35739179e-16, -1.64155539e-15, -1.54600295e-15,
-1.00000000e+00, 2.41181971e-16],
[-2.35739179e-16, -1.64155539e-15, -1.54600295e-15,
-1.00000000e+00, 2.41181971e-16],
[-2.35739179e-16, -1.64155539e-15, -1.54600295e-15,
-1.00000000e+00, 2.41181971e-16]])
``
来源:https://blog.csdn.net/m0_37816922/article/details/130140602
标签:Python,德劳内,三角剖分
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何用Access加密页面?
2010-06-10 18:41:00
Anaconda修改默认虚拟环境安装位置的方案分享
2022-11-19 22:36:52
![](https://img.aspxhome.com/file/2023/9/103239_0s.png)
python生成IP段的方法
2023-07-22 00:00:30
![](https://img.aspxhome.com/file/2023/1/120291_0s.jpg)
详解Python中sorted()和sort()的使用与区别
2022-05-06 17:39:09
如何利用Python开发一个简单的猜数字游戏
2022-05-21 20:38:08
![](https://img.aspxhome.com/file/2023/6/86316_0s.jpg)
Django实现简单分页功能的方法详解
2021-02-07 01:54:23
Python+threading模块对单个接口进行并发测试
2023-07-07 02:41:41
![](https://img.aspxhome.com/file/2023/5/117895_0s.jpg)
每个程序员都应该学习使用Python或Ruby
2023-09-05 06:03:52
![](https://img.aspxhome.com/file/2023/2/130932_0s.png)
MySQL与PHP的基础与应用专题之数据查询
2023-11-10 10:09:55
![](https://img.aspxhome.com/file/2023/8/78208_0s.png)
利用Python的pandas数据处理包将宽表变成窄表
2021-07-28 20:00:11
![](https://img.aspxhome.com/file/2023/1/112131_0s.png)
详解python 字符串和日期之间转换 StringAndDate
2021-11-17 03:40:56
安装PyInstaller失败问题解决
2022-03-18 04:21:41
![](https://img.aspxhome.com/file/2023/1/92431_0s.png)
Python中安装库的常用方法介绍
2022-04-03 08:13:17
![](https://img.aspxhome.com/file/2023/8/85038_0s.png)
javascript农历日历及世界时间代码
2007-12-21 13:25:00
![](https://img.aspxhome.com/file/UploadPic/200712/21/20071221134028383s.gif)
对Python函数设计规范详解
2023-08-02 15:59:17
Pytorch基本变量类型FloatTensor与Variable用法
2022-10-10 21:14:45
![](https://img.aspxhome.com/file/2023/8/101628_0s.jpg)
python动态参数用法实例分析
2021-03-02 06:27:51
python scipy求解非线性方程的方法(fsolve/root)
2022-01-06 15:46:00
![](https://img.aspxhome.com/file/2023/5/92215_0s.jpg)
详解django中视图函数的FBV和CBV
2022-05-26 01:11:10
python中input()与raw_input()的区别分析
2023-02-12 14:08:36
![](https://img.aspxhome.com/file/2023/4/94024_0s.png)