使用matlab繪制七夕表白玫瑰花束
Hey又是一年七夕節(jié)要到了,每年一次直男審美MATLAB繪圖大賽開始,真的這些代碼越寫越不知道咋寫,又不想每年把之前的代碼翻出來再發(fā)一遍,于是今年又對我之前寫的老代碼進行了點優(yōu)化組合,整了個花球變花束:

在此祝各位成雙成對和形單影只的都七夕節(jié)快樂叭?。。?/p>
先稍微說一下原理,再去給完整代碼哈。
玫瑰花
玫瑰花咋畫以及怎么旋轉(zhuǎn)到合適的位置可以參見這篇:
matlab實現(xiàn)繪制玫瑰花球的示例代碼_C 語言_腳本之家 (jb51.net)
寫的非常詳細的,可以看的時候順手點個贊。

周圍小花
是把以下這段很簡單的代碼進行了微調(diào)改了改顏色啥的:
rb=0:.01:1; tb=linspace(0,2,151); Wb=rb'*((abs((1-mod(tb*5,2))))/2+.3); Xb=Wb.*cospi(tb); Yb=Wb.*sinpi(tb); zb=@(a)(-cospi(Wb*a)+1).^.2; Zb=zb(1.2); g=@(i)i(:,1:30:151)/2; plot3(g(Xb),g(Yb),g(zb(1)*2.3),'k'); surface(Xb,Yb,Zb,'EdgeColor','none');

花桿和顏色
花桿就直接用貝塞爾曲線插了一下值,讓其比較順滑且扭轉(zhuǎn)了起來,顏色的話把代碼中的colorList換一下的話可以自行調(diào)整,這里給點顏色數(shù)值以供參考哈:

c1=[0.5600 0.2200 0.0300
0.6700 0.2900 0.0500
0.8300 0.4900 0.0900
0.8500 0.6200 0.1300
0.8800 0.7600 0.1300
0.9100 0.8300 0.2000
1.0000 0.9200 0.3100];
c2=[0.1800 0.0900 0.1800
0.3100 0.0800 0.2400
0.5100 0.0800 0.3400
0.6500 0.1200 0.3500
0.8600 0.3200 0.4500];
c3=[0.1100 0.3100 0.7500
0.3400 0.2900 0.6800
0.5100 0.2700 0.6400
0.5900 0.2700 0.6200
0.7600 0.2500 0.5700];
c4=[0.1400 0.1400 0.1400
0.2200 0.2200 0.2200
0.4100 0.4100 0.4100
0.6000 0.6000 0.6000
0.7500 0.7500 0.7500
0.8900 0.8900 0.8900];
c5=[0.3300 0.3300 0.6900
0.5300 0.4000 0.6800
0.6800 0.4200 0.6300
0.7800 0.4200 0.5700
0.9100 0.4900 0.4700
0.9600 0.7300 0.4400];
c6=[0.5300 0.8300 0.8100
0.5200 0.7500 0.8200
0.4900 0.6200 0.8400
0.4900 0.5600 0.8400
0.4700 0.4900 0.8500
0.4500 0.3500 0.8700
0.9500 0.9500 0.9500];hiahiahia奇怪的顏色增加了:

完整代碼
感覺要運行的話至少需要R17b版本(17b也不算新了最新版都是更了十幾代了),要把代碼改的適應更之前的版本老麻煩了。。。建議大家用點比較新的版本!!不然平白無故多寫老多代碼新版本還不一定用的了。
function roseBouquet
figure('Units','normalized','Position',[.2,.1,.6,.7])
%曲面數(shù)據(jù)計算 ==============================================================
% 玫瑰部分 -----------------------------------------------------------------
[xr,tr]=meshgrid((0:24)./24,(0:0.5:575)./575.*20.*pi+4*pi);
pr=(pi/2)*exp(-tr./(8*pi));
cr=sin(15*tr)/150;
ur=1-(1-mod(3.6*tr,2*pi)./pi).^4./2+cr;
yr=2*(xr.^2-xr).^2.*sin(pr);
rr=ur.*(xr.*sin(pr)+yr.*cos(pr));
hr=ur.*(xr.*cos(pr)-yr.*sin(pr));
% 百合花部分 ---------------------------------------------------------------
rb=0:.01:1;
tb=linspace(0,2,151);
wb=rb'*((abs((1-mod(tb*5,2))))/2+.3);
xb=wb.*cospi(tb);
yb=wb.*sinpi(tb);
zb=@(a)(-cospi(wb*a)+1).^.2;
Zb=zb(1.2);
g=@(i)i(:,1:30:151)/2;
%顏色映射表 ================================================================
colorList=[0.3300 0.3300 0.6900
0.5300 0.4000 0.6800
0.6800 0.4200 0.6300
0.7800 0.4200 0.5700
0.9100 0.4900 0.4700
0.9600 0.7300 0.4400];
% colorList=[0.9176 0.7490 0.3765
% 0.9294 0.7725 0.4902
% 0.9333 0.7451 0.5961
% 0.8902 0.6980 0.5294
% 0.8784 0.6941 0.6235
% 0.9216 0.7412 0.7529
% 0.8588 0.6039 0.7686
% 0.8510 0.4706 0.6392
% 0.7608 0.2118 0.3569];
colorMapr=setColorByH(hr,colorList);
colorMapb=setColorByH(Zb,colorList.*.4+.6);
function cMap=setColorByH(H,cList)
X=(H-min(min(H)))./(max(max(H))-min(min(H)));
xx=(0:size(cList,1)-1)./(size(cList,1)-1);
y1=cList(:,1);y2=cList(:,2);y3=cList(:,3);
cMap(:,:,1)=interp1(xx,y1,X,'linear');
cMap(:,:,2)=interp1(xx,y2,X,'linear');
cMap(:,:,3)=interp1(xx,y3,X,'linear');
end
% 旋轉(zhuǎn)函數(shù)預定義 ===========================================================
yaw_z=72*pi/180;
roll_x_1=pi/8;
roll_x_2=pi/9;
R_z_2=[cos(yaw_z) , -sin(yaw_z) , 0; sin(yaw_z) , cos(yaw_z) , 0; 0, 0, 1];
R_z_1=[cos(yaw_z/2), -sin(yaw_z/2), 0; sin(yaw_z/2), cos(yaw_z/2), 0; 0, 0, 1];
R_z_3=[cos(yaw_z/3), -sin(yaw_z/3), 0; sin(yaw_z/3), cos(yaw_z/3), 0; 0, 0, 1];
R_x_1=[1, 0, 0; 0, cos(roll_x_1), -sin(roll_x_1); 0, sin(roll_x_1), cos(roll_x_1)];
R_x_2=[1, 0, 0; 0, cos(roll_x_2), -sin(roll_x_2); 0, sin(roll_x_2), cos(roll_x_2)];
function [nX,nY,nZ]=rotateXYZ(X,Y,Z,R)
nX=zeros(size(X)); nY=zeros(size(Y)); nZ=zeros(size(Z));
for i=1:size(X,1)
for j=1:size(X,2)
v=[X(i,j);Y(i,j);Z(i,j)];
nv=R*v; nX(i,j)=nv(1); nY(i,j)=nv(2); nZ(i,j)=nv(3);
end
end
end
% 繪制花桿函數(shù)預定義 ========================================================
function drawStraw(X,Y,Z)
[m,n]=find(Z==min(min(Z)));
m=m(1);n=n(1);
x1=X(m,n);y1=Y(m,n);z1=Z(m,n)+.03;
xx=[x1,0,(x1.*cos(pi/3)-y1.*sin(pi/3))./3].';
yy=[y1,0,(y1.*cos(pi/3)+x1.*sin(pi/3))./3].';
zz=[z1,-.7,-1.5].';
strawPnts=bezierCurve([xx,yy,zz],50);
plot3(strawPnts(:,1),strawPnts(:,2),strawPnts(:,3),'Color',[88,130,126]./255,'LineWidth',2)
end
% 貝塞爾函數(shù) ---------------------------------------------------------------
function pnts=bezierCurve(pnts,N)
t=linspace(0,1,N);
p=size(pnts,1)-1;
coe1=factorial(p)./factorial(0:p)./factorial(p:-1:0);
coe2=((t).^((0:p)')).*((1-t).^((p:-1:0)'));
pnts=(pnts'*(coe1'.*coe2))';
end
%曲面旋轉(zhuǎn)及繪制 ============================================================
hold on
surface(rr.*cos(tr),rr.*sin(tr),hr+0.35,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMapr,'Tag','slandarer')
[nXr,nYr,nZr]=rotateXYZ(rr.*cos(tr),rr.*sin(tr),hr+0.35,R_x_1);
nYr=nYr-.4;
surface(nXr,nYr,nZr-.1,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMapr)
drawStraw(nXr,nYr,nZr-.1)
for k=1:4
[nXr,nYr,nZr]=rotateXYZ(nXr,nYr,nZr,R_z_2);
surface(nXr,nYr,nZr-.1,'EdgeAlpha',0.05,...
'EdgeColor',[0 0 0],'FaceColor','interp','CData',colorMapr)
drawStraw(nXr,nYr,nZr-.1)
end
% -------------------------------------------------------------------------
[nXb,nYb,nZb]=rotateXYZ(xb./2.5,yb./2.5,Zb./2.5+.32,R_x_2);
nYb=nYb-1.35;
for k=1:5
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_2);
surface(nXb,nYb,nZb,'EdgeColor','none','FaceColor','interp','CData',colorMapb)
drawStraw(nXb,nYb,nZb)
end
[nXb,nYb,nZb]=rotateXYZ(xb./2.5,yb./2.5,Zb./2.5+.32,R_x_2);
nYb=nYb-1.15;
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_1);
for k=1:5
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_2);
surface(nXb,nYb,nZb,'EdgeColor','none','FaceColor','interp','CData',colorMapb)
drawStraw(nXb,nYb,nZb)
end
[nXb,nYb,nZb]=rotateXYZ(xb./2.5,yb./2.5,Zb./2.5+.32,R_x_2);
nYb=nYb-1.25;
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_3);
for k=1:5
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_2);
surface(nXb,nYb,nZb,'EdgeColor','none','FaceColor','interp','CData',colorMapb)
drawStraw(nXb,nYb,nZb)
end
[nXb,nYb,nZb]=rotateXYZ(xb./2.5,yb./2.5,Zb./2.5+.32,R_x_2);
nYb=nYb-1.25;
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_3);
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_3);
for k=1:5
[nXb,nYb,nZb]=rotateXYZ(nXb,nYb,nZb,R_z_2);
surface(nXb,nYb,nZb,'EdgeColor','none','FaceColor','interp','CData',colorMapb)
drawStraw(nXb,nYb,nZb)
end
%axes屬性調(diào)整 ==============================================================
ax=gca;
ax.Position=[0,0,1,1];
grid on
ax.GridLineStyle='--';
ax.LineWidth=1.2;
ax.XColor=[1,1,1].*0.4;
ax.YColor=[1,1,1].*0.4;
ax.ZColor=[1,1,1].*0.4;
ax.DataAspectRatio=[1,1,1];
ax.DataAspectRatioMode='manual';
view(-15,35);
end完
再次祝各位成雙成對和形單影只的都七夕節(jié)快樂叭?。?!
到此這篇關于使用matlab繪制七夕表白玫瑰花束的文章就介紹到這了,更多相關matlab繪制玫瑰花內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
c++棧內(nèi)存和堆內(nèi)存的基本使用小結(jié)
本文主要介紹了c++棧內(nèi)存和堆內(nèi)存的基本使用小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
C++實現(xiàn)LeetCode(10.正則表達式匹配)
這篇文章主要介紹了C++實現(xiàn)LeetCode(10.正則表達式匹配),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
C++?system()函數(shù)的常用用法(全網(wǎng)最新)
system()用于從C?/C++程序調(diào)用操作系統(tǒng)命令,這里給大家講解下C++?system()函數(shù)的常用用法,感興趣的朋友跟隨小編一起看看吧2023-01-01

