gpt4 book ai didi

matlab - 我如何在 MATLAB 中重现这个心形网格?

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:33 25 4
gpt4 key购买 nike

我想绘制一个心形线框,如下图所示( source ):

heart-shape

我尝试使用这个 MATLAB 程序来实现它:

n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
isosurface(F,0)
lighting phong
caxis
axis equal
colormap('flag');
view([55 34]);

但是我并没有得到如图所示的想要的框架形状。

我已经确定了问题:要创建线框,我们通常使用命令 mesh()。但是这个绘图工具只允许我们绘制两个变量的函数,例如 z=f(x,y)。但是我的程序使用了三个变量:F(x,y,z)

我该如何解决这个问题?

最佳答案

这是我重现整个图形的最佳尝试:

enter image description here

生成轮廓心形网格:

我使用了 contourc函数在 x-y、x-z 和 y-z 平面中生成一系列轮廓。请注意,在您要重现的图像中,心形背面的网格线未渲染。我能想到的重现情节的那个方面的最快和最简单的方法是使用 isosurface在网格的内表面下方渲染一个白色表面,挡住背面的视线。

下面是heart函数的代码:

function heart

% Initialize the volume data, figure, and axes:

[X,Y,Z] = meshgrid(linspace(-3,3,101));
F = -X.^2.*Z.^3-(9/80).*Y.^2.*Z.^3+(X.^2+(9/4).*Y.^2+Z.^2-1).^3;
hFigure = figure('Position',[200 200 400 400],'Color','w');
hAxes = axes('Parent',hFigure,'Units','pixels',...
'Position',[1 1 400 400],'NextPlot','add',...
'DataAspectRatio',[1 1 1],'Visible','off',...
'CameraViewAngle',10,...
'XLim',[32 70],'YLim',[39 63],'ZLim',[34 73]);
view([-39 30]);

% Create and plot contours in the y-z plane:

for iX = [35 38 41 45 48 51 54 57 61 64 67]
plane = reshape(F(:,iX,:),101,101);
cData = contourc(plane,[0 0]);
xData = iX.*ones(1,cData(2,1));
plot3(hAxes,xData,cData(2,2:end),cData(1,2:end),'k');
end

% Create and plot contours in the x-z plane:

for iY = [41 44 47 51 55 58 61]
plane = reshape(F(iY,:,:),101,101);
cData = contourc(plane,[0 0]);
yData = iY.*ones(1,cData(2,1));
plot3(hAxes,cData(2,2:end),yData,cData(1,2:end),'k');
end

% Create and plot contours in the x-y plane:

for iZ = [36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 69 71]
plane = F(:,:,iZ);
cData = contourc(plane,[0 0]);
startIndex = 1;
if size(cData,2) > (cData(2,1)+1)
startIndex = cData(2,1)+2;
zData = iZ.*ones(1,cData(2,1));
plot3(hAxes,cData(1,2:(startIndex-1)),...
cData(2,2:(startIndex-1)),zData,'k');
end
zData = iZ.*ones(1,cData(2,startIndex));
plot3(hAxes,cData(1,(startIndex+1):end),...
cData(2,(startIndex+1):end),zData,'k');
end

% Fill the inside of the mesh with an isosurface to
% block rendering of the back side of the heart:

p = patch(isosurface(F,-0.001));
set(p,'FaceColor','w','EdgeColor','none');

end

将图放在一起:

为了重现整个图形,我首先使用上面的 heart 函数生成了心形网格,然后在它周围添加了其他元素。我还使用了来自 The MathWorks File Exchange 的一些提交:

这是函数 I_Heart_Math 的代码(生成上图):

function I_Heart_Math

% Initialize heart plot and adjust figure and axes settings:

heart;
set(gcf,'Position',[200 200 700 300],'Name','Original image');
offset = get(gca,'CameraPosition')-get(gca,'CameraTarget');
offset = 35.*offset./norm(offset);
set(gca,'Position',[65 -9 300 300],'CameraViewAngle',6,...
'XLim',[21+offset(1) 70],'YLim',[16+offset(2) 63],...
'ZLim',[32 81+offset(3)]);

% Create the axes and labels, offsetting them in front of the
% heart to give the appearance they are passing through it:

arrowStarts = [81 51 51; 51 86 51; 51 51 32]+repmat(offset,3,1);
arrowEnds = [21 51 51; 51 16 51; 51 51 81]+repmat(offset,3,1);
arrow(arrowStarts,arrowEnds,5,40,40);
text('Position',[22 52 48]+offset,'String','x','FontSize',12);
text('Position',[50 17 49]+offset,'String','y','FontSize',12);
text('Position',[46.5 51 81.5]+offset,'String','z','FontSize',12);

% Create the equation text:

text('Position',[51 47 28],'FontName','Bookman','FontSize',8,...
'HorizontalAlignment','center',...
'String',{'(x^2+^9/_4y^2+z^2-1)^3-x^2z^3-^9/_{80}y^2z^3=0'; ...
'-3 \leq x,y,z \leq 3'});

% Create the large-type text:

hI = text('Position',[4 52 69.5],'String','I',...
'FontAngle','italic','FontName','Trebuchet MS',...
'FontSize',116,'FontWeight','bold');
hM = text('Position',[80.5 50 42.5],'String','Math',...
'FontAngle','italic','FontName','Trebuchet MS',...
'FontSize',116,'FontWeight','bold');

% Create an anti-aliased version of the figure too (the larger
% fonts need some adjustment to do this... not sure why):

set(hI,'Position',[4 52 68],'FontSize',86);
set(hM,'Position',[80.5 50 41],'FontSize',86);
myaa;
set(hI,'Position',[4 52 69.5],'FontSize',116);
set(hM,'Position',[80.5 50 42.5],'FontSize',116);
set(gcf,'Name','Anti-aliased image');

end

关于matlab - 我如何在 MATLAB 中重现这个心形网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1526898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com