首頁 > 軟體

C語言程式設計使用MATLAB繪製冰墩墩

2022-02-09 10:01:02

不能放圖,大家自行繪製試試看。

1 程式說明

這個程式就是用一系列橢圓,圓角矩形及心形拼在起,以下說明一下各部分怎麼生成:

橢圓形:

橢圓形是用的如下橢圓資料生成函數生成資料點,之後再用fill函數繪製而成,其中Mu為橢圓中心點,Sigma為協方差矩陣,S為半徑平方,pntNum是生成資料點個數:

% 橢圓資料計算函數,輸入協方差矩陣、中心點、半徑生成橢圓資料

% 橢圓資料計算函數,輸入協方差矩陣、中心點、半徑生成橢圓資料
function [X,Y]=getEllipse(Mu,Sigma,S,pntNum)
    % (X-Mu)*inv(Sigma)*(X-Mu)=S
    invSig=inv(Sigma);
    [V,D]=eig(invSig);
    aa=sqrt(S/D(1));
    bb=sqrt(S/D(4));
    t=linspace(0,2*pi,pntNum);
    XY=V*[aa*cos(t);bb*sin(t)];
    X=(XY(1,:)+Mu(1))';
    Y=(XY(2,:)+Mu(2))';
end

圓角矩形:
使用MATLAB自帶的圓角矩陣生成函數,詳細資訊可以去MATHWORKS官網檢視:

rectangle(‘Position’,pos,‘Curvature’,cur)

心形:

使用如下心形函數:

t=linspace(-2.9,2.9,1000);
X=16.*(sin(t)).^3;
Y=13.*cos(t)-5.*cos(2.*t)-2.*cos(3.*t)-cos(4.*t);
fill(X,Y,[180,39,45]./255,'EdgeColor',[180,39,45]./255,'LineWidth',2)

 2 完整程式碼

function bingdundun
ax=gca;
ax.DataAspectRatio=[1 1 1];
ax.XLim=[-5 5];
ax.YLim=[-5 5];
hold(ax,'on')
% =========================================================================
% 繪製冰糖外殼
[X,Y]=getEllipse([0,0],[1,0;0,1.3],3.17^2,200);
plot(X,Y,'Color',[57,57,57]./255,'LineWidth',1.8)
%
[X,Y]=getEllipse([1.7,2.6],[1.2,0;0,1.8],.65^2,200);
plot(X,Y,'Color',[57,57,57]./255,'LineWidth',1.8)
plot(-X,Y,'Color',[57,57,57]./255,'LineWidth',1.8)
[X,Y]=getEllipse([1.7,2.6],[1.2,0;0,1.8],.6^2,200);
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
fill(-X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
%
[X,Y]=getEllipse([-3.5,-1],[1.1,.3;.3,1.1],.75^2,200);
plot(X,Y,'Color',[57,57,57]./255,'LineWidth',1.8)
[X,Y]=getEllipse([-3.5,-1],[1.1,.3;.3,1.1],.68^2,200);
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
[X,Y]=getEllipse([3.5,1],[1.1,.3;.3,1.1],.75^2,200);
plot(X,Y,'Color',[57,57,57]./255,'LineWidth',1.8)
[X,Y]=getEllipse([3.5,1],[1.1,.3;.3,1.1],.68^2,200);
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
%
X=[-3.8,-2,-3];
Y=[-.51+.13,1+.13,-1];
plot(X,Y,'Color',[57,57,57]./255,'LineWidth',1.8)
plot(-X,-Y,'Color',[57,57,57]./255,'LineWidth',1.8)
X=[-3.8,-2,-3];
Y=[-.51+.03,1+.03,-1];
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
fill(-X,-Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
%
[X,Y]=getEllipse([0,-.1],[1,0;0,1.6],.9^2,200);
Y(Y<0)=Y(Y<0).*.2;Y=Y-4.2;X=X-1.2;
plot(X,Y,'Color',[57,57,57]./255,'LineWidth',2)
plot(-X,Y,'Color',[57,57,57]./255,'LineWidth',2)
rectangle('Position',[-2.1 -4.2 1.7 3],'Curvature',0.4,...
    'FaceColor',[1 1 1],'EdgeColor',[57,57,57]./255,'LineWidth',1.8)
rectangle('Position',[2.1-1.7 -4.2 1.7 3],'Curvature',0.4,...
    'FaceColor',[1 1 1],'EdgeColor',[57,57,57]./255,'LineWidth',1.8)
[X,Y]=getEllipse([0,-.1],[1,0;0,1.6],.8^2,200);
Y(Y<0)=Y(Y<0).*.2;Y=Y-4.1;X=X-1.2;
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
fill(-X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
%
[X,Y]=getEllipse([0,0],[1,0;0,1.3],3.1^2,200);
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',1.8)
% =========================================================================
% 耳朵
[X,Y]=getEllipse([1.7,2.6],[1.2,0;0,1.8],.5^2,200);
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
fill(-X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
% 胳膊
[X,Y]=getEllipse([-3.5,-1],[1.1,.3;.3,1.1],.6^2,200);
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
[X,Y]=getEllipse([3.5,1],[1.1,.3;.3,1.1],.6^2,200);
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
X=[-3.8,-2,-3];
Y=[-.51,1,-1];
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255)
fill(-X,-Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255)
tt=linspace(-2.9,2.9,1000);
X=16.*(sin(tt)).^3;
Y=13.*cos(tt)-5.*cos(2.*tt)-2.*cos(3.*tt)-cos(4.*tt);
X=X.*.018+3.6;
Y=Y.*.018+1.1;
fill(X,Y,[180,39,45]./255,'EdgeColor',[180,39,45]./255,'LineWidth',2)
% 腿
[X,Y]=getEllipse([0,-.1],[1,0;0,1.6],.7^2,200);
Y(Y<0)=Y(Y<0).*.2;Y=Y-4.1;X=X-1.2;
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
fill(-X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
rectangle('Position',[-1.95 -4.3 1.4 3],'Curvature',0.4,...
    'FaceColor',[57,57,57]./255,'EdgeColor',[57,57,57]./255)
rectangle('Position',[1.95-1.4 -4.3 1.4 3],'Curvature',0.4,...
    'FaceColor',[57,57,57]./255,'EdgeColor',[57,57,57]./255)
% 身體
[X,Y]=getEllipse([0,0],[1,0;0,1.3],3^2,200);
fill(X,Y,[1,1,1],'EdgeColor',[57,57,57]./255,'LineWidth',2.5)
% 五環
cList=[132,199,114;251,184,77;89,120,177;158,48,87;98,205,247];
for i=1:5
    [X,Y]=getEllipse([0,0],[1.6,0;0,1.3],(2.05-0.05.*i)^2,200);
    Y(Y<0)=Y(Y<0).*.8;Y=Y+.5;
    fill(X,Y,[1,1,1],'EdgeColor',cList(i,:)./255,'LineWidth',2.5)
end
% 眼睛
[X,Y]=getEllipse([1.2,1.2],[1.2,-.5;-.5,1.1],.65^2,200);
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
fill(-X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
[X,Y]=getEllipse([.95,1.3],[1,0;0,1],.35^2,200);
fill(X,Y,[57,57,57]./255,'EdgeColor',[1,1,1],'LineWidth',1.6)
fill(-X,Y,[57,57,57]./255,'EdgeColor',[1,1,1],'LineWidth',1.6)
[X,Y]=getEllipse([.95,1.3],[1,0;0,1],.1^2,200);
fill(X+.18,Y,[1,1,1],'EdgeColor',[57,57,57]./255,'LineWidth',.5)
fill(-X+.18,Y,[1,1,1],'EdgeColor',[57,57,57]./255,'LineWidth',.5)
% 嘴巴
[X,Y]=getEllipse([0.05,.2],[1.2,.15;.15,.8],.69^2,200);
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
[X,Y]=getEllipse([0,.75],[1,0.2;0.2,.3],.4^2,200);
fill(X,Y,[1,1,1],'EdgeColor',[1,1,1],'LineWidth',2)
[X,Y]=getEllipse([0,0],[.8,0;0,.2],.6^2,200);
fill(X,Y,[180,39,45]./255,'EdgeColor',[180,39,45]./255,'LineWidth',2)
% 鼻子
[X,Y]=getEllipse([0,-.1],[1,0;0,1.6],.2^2,200);
Y(Y<0)=Y(Y<0).*.2;Y=-Y+.9;
fill(X,Y,[57,57,57]./255,'EdgeColor',[57,57,57]./255,'LineWidth',2)
% =========================================================================
% 冬奧會標誌及五環
% 五環
tt=linspace(0,2*pi,100);
X=cos(tt).*.14;
Y=sin(tt).*.14;
plot(X,Y-2.8,'Color',[57,57,57]./255,'LineWidth',1.2)
plot(X-.3,Y-2.8,'Color',[106,201,245]./255,'LineWidth',1.2)
plot(X+.3,Y-2.8,'Color',[155,79,87]./255,'LineWidth',1.2)
plot(X-.15,Y-2.9,'Color',[236,197,107]./255,'LineWidth',1.2)
plot(X+.15,Y-2.9,'Color',[126,159,101]./255,'LineWidth',1.2)
% 文字
text(0,-2.4,'BEIJING 2022','HorizontalAlignment','center',...
    'FontSize',8,'FontName','Comic Sans MS')
% 標誌
fill([.1,-.12,-.08],[0,0-0.05,-0.15]-1.5,[98,118,163]./255,'EdgeColor',[98,118,163]./255)
fill([-.08,-.35,.1],[-0.1,-.2,-.1]-1.6,[98,118,163]./255,'EdgeColor',[98,118,163]./255)
fill([-.08,-.08,.1,.1],[-0.1,-0.15,-.2,-.15]-1.5,[192,15,45]./255,'EdgeColor',[192,15,45]./255)
plot([-.35,-.3,-.25,-.2,-.15,-.1,-.05,.1]+.02,...
     [0,.02,.04,.06,.04,.02,0,.02]-1.82,'Color',[120,196,219]./255,'LineWidth',1.8)
plot([-.33,.05]+.02,[0,-.08]-1.82,'Color',[190,215,84]./255,'LineWidth',1.8)
plot([.05,-.2]+.02,[-.08,-.15]-1.82,'Color',[32,162,218]./255,'LineWidth',1.8)
plot([-.2,.05]+.02,[-.15,-.2]-1.82,'Color',[99,118,151]./255,'LineWidth',1.8)
% =========================================================================
    % 橢圓資料計算函數,輸入協方差矩陣、中心點、半徑生成橢圓資料
    function [X,Y]=getEllipse(Mu,Sigma,S,pntNum)
        % (X-Mu)*inv(Sigma)*(X-Mu)=S
        invSig=inv(Sigma);
        
        [V,D]=eig(invSig);
        aa=sqrt(S/D(1));
        bb=sqrt(S/D(4));
        
        t=linspace(0,2*pi,pntNum);
        XY=V*[aa*cos(t);bb*sin(t)];
        X=(XY(1,:)+Mu(1))';
        Y=(XY(2,:)+Mu(2))';
    end
end

以上就是C語言程式設計使用MATLAB繪製冰墩墩的詳細內容,更多關於MATLAB繪製冰墩墩的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com