燈籠中有很多大面積的顏色區(qū)域,是需要使用北太天元的 fill 函數(shù)來完成,fill 函數(shù)的幫助如下:
>> help fill 填充的二維多邊形 語法: fill(X,Y) 示例: % 函數(shù)創(chuàng)建紅色多邊形。 % X 是頂點的x坐標,Y是頂點的y坐標 % 例如 一個三角形的三個頂點的坐標是p1(0,0), p2(1,0), p3(0.5,0.5) % 畫出紅色的三角形 x = [0, 1, 0.5]; y = [0, 0, 0.5]; fill(x,y,'r')
為了調(diào)用 fill 函數(shù),我們需要先創(chuàng)建橢圓數(shù)據(jù)點生成函數(shù),用來生成橢圓和圓形的邊界的 X 和 Y 坐標
% 橢圓數(shù)據(jù)點生成函數(shù) function [X,Y]=getEllipse(Mu,XR,YR,theta,pntNum) % Mu | 中心點 % XR,YR | 旋轉(zhuǎn)前X,Y半軸長度 % theta | 旋轉(zhuǎn)角度 % pntNum | 生成數(shù)據(jù)點個數(shù) tList = linspace(0,2*pi,pntNum); X = cos(tList).*XR; Y = sin(tList).*YR; rotateMat = [cos(theta),-sin(theta);sin(theta),cos(theta)]; XY = rotateMat*[X;Y]+Mu(:); X = XY(1,:); Y = XY(2,:); end
下面我們就可以開始燈籠的繪制了
clf hold on n = 0.4; x = 1; a = 0.1; w = 2.7;h = 2.5; %燈籠主體橢圓的寬和高 % 繪制燈籠主體 [X0,Y0]=getEllipse([0,0],2.9,h,0,200); fill(X0,Y0,[184,20,25]./255,'EdgeColor',[153,12,40]./255,'LineWidth',1.2) % 繪制輻線 [X,Y]=getEllipse([0,0],w,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) [X,Y]=getEllipse([0,0],w-n,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) [X,Y]=getEllipse([0,0],w-2*n,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) [X,Y]=getEllipse([0,0],w-3*n,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) [X,Y]=getEllipse([0,0],w-4*n+0.05,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) [X,Y]=getEllipse([0,0],w-5*n+0.1,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) [X,Y]=getEllipse([0,0],w-6*n+0.1,h,0,200);plot(X,Y,'Color',[236,136,74]./255,'LineWidth',1.2) plot([0,0],[-h,h],'Color',[236,136,74]./255,'LineWidth',1.2) % 計算其它部件需要的邊界坐標 X1 = X0(abs(X0)<=x); X2 = X1(1:size(X1,2)/2); X3 = X1(size(X1,2)/2+1:end); Y1 = Y0(abs(X0)<=x); Y2 = Y1(1:size(Y1,2)/2); Y3 = Y1(size(Y1,2)/2+1:end); XX1 = X0(abs(X0)<=a); XX2 = XX1(1:size(XX1,2)/2); XX3 = XX1(size(XX1,2)/2+1:end); YY1 = ones(1,size(XX2,2)).*(-w-0.2); YY2 = ones(1,size(XX2,2)).*(-h-2.5); YY3 = ones(1,size(XX2,2)).*(-h-2); % 繪制其它部分 Y4 = ones(1,size(Y2,2)).*(w+0.2); fill(X1,[Y2,Y4],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) Y5 = ones(1,size(Y2,2)).*-(w+0.2); fill(X1,[Y3,Y5],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) fill(XX1,[YY1,YY3],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) fill(XX1.+0.7,[YY1,YY3],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) fill(XX1.-0.7,[YY1,YY3],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) fill(XX1.+0.35,[YY1,YY3],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) fill(XX1.-0.35,[YY1,YY3],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) fill(XX1,[-YY1,-YY2],[76,24,38]./255,'EdgeColor',[236,136,74]./255,'LineWidth',1.2) [X6,Y6]=getEllipse([0,h+1],0.2,0.2,0,200); fill(X6,Y6,[184,20,25]./255,'EdgeColor') % 調(diào)整坐標軸 axis([-5 5 -5 5]) hold off title("新春快樂!")