如何利用Matlab网格生成器画太极

注:首先要懂几何函数的定义!

代码:1. 主函数:

clc,clear;
close all;
g1=[1 1 1 1; 2 0 -2 0;0 -2 0 2; 0 2 0 -2; 2 0 -2 0;
1 2 2 1; 0 0 0 0;0 0 0 0;0 0 0 0;2 2 2 2]; % 大○
g2=[1 1 1 1;0 1 0 -1;1 0 -1 0;-2 -1 2 1;-1 0 1 0;
2 2 1 1;1 1 2 2;0 0 0 0;-1 -1 1 1;1 1 1 1]; % 分界线
g3=[1 1 1 1;0.5 0 -0.5 0;0 -0.5 0 0.5;1 1.25 1 0.75;1.25 1 0.75 1;
3 3 3 3;1 1 1 1;0 0 0 0;1 1 1 1;0.25 0.25 0.25 0.25]; % 上○
g4=[1 1 1 1;0.5 0 -0.5 0;0 -0.5 0 0.5;-1 -0.75 -1 -1.25;-0.75 -1 -1.25 -1;
4 4 4 4; 2 2 2 2; 0 0 0 0;-1 -1 -1 -1;0.25 0.25 0.25 0.25]; % 下○
g=[g1 g2 g3 g4];
[p,e,t] = initmesh(g,'hmax',0.1);
figure
pdemesh(p,e,t);

%% Identify nodes and elem in different regions.
[elem1,elem2,elem3,elem4]=Idtaiji_4R(p,t);

%% 染色
node=p';
h1 = trisurf(elem1,node(:,1),node(:,2),zeros(size(node,1),1));
set(h1,'facecolor',[0.2 0.2 0.2],'edgecolor','k');
view(2); axis equal; axis tight; axis off;
hold on
%
h2 = trisurf(elem2,node(:,1),node(:,2),zeros(size(node,1),1));
set(h2,'facecolor','w','edgecolor','k');
view(2); axis equal; axis tight; axis off;
hold on
%
h3 = trisurf(elem3,node(:,1),node(:,2),zeros(size(node,1),1));
set(h3,'facecolor','w','edgecolor','k');
view(2); axis equal; axis tight; axis off;
hold on
%
h4 = trisurf(elem4,node(:,1),node(:,2),zeros(size(node,1),1));
set(h4,'facecolor',[0.2 0.2 0.2],'edgecolor','k');
view(2); axis equal; axis tight; axis off;
2. 子函数
function [elem1,elem2,elem3,elem4]=Idtaiji_4R(p,t)
nt=size(t,2);
i1=1;
i2=1;
i3=1;
i4=1;
for k=1:nt
if t(4,k)==1
elem1(i1,1:3)=t(1:3,k)';
i1=i1+1;
end
if t(4,k)==2
elem2(i2,:)=t(1:3,k)';
i2=i2+1;
end
if t(4,k)==3
elem3(i3,:)=t(1:3,k)';
i3=i3+1;
end
if t(4,k)==4
elem4(i4,:)=t(1:3,k)';
i4=i4+1;
end
end 

结果:

 

 

posted @ 2024-08-03 21:31  月下吴钩寒  阅读(11)  评论(0)    收藏  举报