Delaunay算法 matlab代码
时间: 2023-10-09 16:14:08 浏览: 146
Delaunay 三角剖分法matlab代码
以下是用Matlab实现Delaunay三角剖分的代码:
```matlab
clc;
clear;
close all;
% 生成随机离散点
rand('state', 0);
node = 8;
x = rand(1,node);
y = rand(1,node);
% 使用delaunay函数进行三角剖分
TRI = delaunay(x,y);
% 绘制三角剖分图
figure;
xmin = min(x(:));
xmax = max(x(:));
ymin = min(y(:));
ymax = max(y(:));
xl = xmax - xmin;
yl = ymax - ymin;
axis([xmin-xl*0.1, xmax xl*0.1, ymin-yl*0.1, ymax yl*0.1]);
hold on;
n = size(TRI, 1);
for i = 1 : n
t1 = TRI(i, :);
for j = 1 : length(t1)-1
xt = [x(t1(j)) x(t1(j+1))];
yt = [y(t1(j)) y(t1(j+1))];
plot(xt, yt, 'k-', 'LineWidth', 2);
pause(0.1);
end
xt = [x(t1(end)) x(t1(1))];
yt = [y(t1(end)) y(t1(1))];
plot(xt, yt, 'k-', 'LineWidth', 2);
pause(0.1);
end
% 构建邻接矩阵
W = zeros(node);
for i = 1 : n
for j = 1 : length(TRI(i, :))-1
W(TRI(i, j), TRI(i, j+1)) = 1;
W(TRI(i, j+1), TRI(i, j)) = 1;
end
W(TRI(i, end), TRI(i, 1)) = 1;
W(TRI(i, 1), TRI(i, end)) = 1;
end
% 将未连接的点权重设为10000
for i = 1 : node
for j = 1 : node
if ~W(i, j)
W(i, j) = 10000;
end
end
end
```
这段代码实现了Delaunay三角剖分算法,并使用随机生成的离散点进行了演示。首先通过`delaunay`函数对离散点进行三角剖分,然后利用`plot`函数绘制三角形的边界。最后,根据剖分结果构建了邻接矩阵,并将未连接的点权重设为10000。
阅读全文