灯笼中有很多大面积的颜色区域,是需要使用北太天元的 fill 函数来完成,fill 函数的帮助如下:
>> help fill 填充的二维多边形 语法: fill(X,Y) 示例: % 函数创建红色多边形。 % 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')
为了调用 fill 函数,我们需要先创建椭圆数据点生成函数,用来生成椭圆和圆形的边界的 X 和 Y 坐标
% 椭圆数据点生成函数 function [X,Y]=getEllipse(Mu,XR,YR,theta,pntNum) % Mu | 中心点 % XR,YR | 旋转前X,Y半轴长度 % theta | 旋转角度 % pntNum | 生成数据点个数 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') % 调整坐标轴 axis([-5 5 -5 5]) hold off title("新春快乐!")