主函数 main.m
clear;clc;
tic
%画国旗
%% 参数初始化
D.chang = 300;
D.kuan = D.chang*2/3;%长宽比为2:3
%% 绘制底色
figure(1)
hold on
Fig.f1 = fill([0,D.chang,D.chang,0],[0,0,D.kuan,D.kuan],'r');
% P = [];
% P.x1,P.y1 = [D.chang/6,3*D.kuan/4];
P.anchorx = D.chang/6;
P.anchory = 3*D.kuan/4;
startlocus = [ D.chang/6,3*D.kuan/4;
D.chang/3,9*D.kuan/10;
12*D.chang/30,4*D.kuan/5;
12*D.chang/30,13*D.kuan/20;
D.chang/3,11*D.kuan/20];
% scatter(startlocus(:,1),startlocus(:,2))
[P.x1,P.y1] = getstartpoint(startlocus,D,P,D.kuan*3/20,1);
[P.x2,P.y2] = getstartpoint(startlocus,D,P,D.kuan/20,2);
[P.x3,P.y3] = getstartpoint(startlocus,D,P,D.kuan/20,3);
[P.x4,P.y4] = getstartpoint(startlocus,D,P,D.kuan/20,4);
[P.x5,P.y5] = getstartpoint(startlocus,D,P,D.kuan/20,5);
Fig.f2 = fill([P.x1],[P.y1],'y');
Fig.f3 = fill([P.x2],[P.y2],'y');
Fig.f4 = fill([P.x3],[P.y3],'y');
Fig.f5 = fill([P.x4],[P.y4],'y');
Fig.f6 = fill([P.x5],[P.y5],'y');
set(Fig.f1,'Linestyle','none')
set(Fig.f2,'Linestyle','none')
set(Fig.f3,'Linestyle','none')
set(Fig.f4,'Linestyle','none')
set(Fig.f5,'Linestyle','none')
set(Fig.f6,'Linestyle','none')
toc
子函数 getstartpoint.m
function [px,py] = getstartpoint(startlocus,D,P,scale,num)
%输入:五角星位置数组,初始参数D结构体,已绘制五角星顶点坐标P结构体,该五角星大小,该五角星编号
%输出:该绘制五角星顶点坐标,依次标号为1,2,3,4,5
%约束:2和5点连线过(P.anchorx,P.anchory)
%思路:先绘制出正五角星,再求转多少角度后25连线过锚点,整体旋转该角度
%% 绘制正五角星
theta = (-270:72:90-1)*pi/180;
%% 求转角
if num~=1
syms phi
tempx1 = startlocus(num,1)+scale*cos(theta(2)+phi);
tempy1 = startlocus(num,2)+scale*sin(theta(2)+phi);
tempx2 = startlocus(num,1)+scale*cos(theta(5)+phi);
tempy2 = startlocus(num,2)+scale*sin(theta(5)+phi);
k = (tempy2-tempy1)/(tempx2-tempx1);
b = tempy2-k*tempx2;
phii = solve(P.anchory-k*P.anchorx-b);
clear phi
phii = double(real(phii));
phiii = abs(phii);
row = find(phiii == min(phiii));
phii = phii(row);
else
phii = 0;
end
%% 整体旋转该角度
theta = (-270:72:90-1)*pi/180;
theta = theta+phii;
radio = scale*tand(18)/(tand(18)*cosd(36)+sind(36));
alpha = (-234:72:126-1)*pi/180;
alpha = alpha+phii;
px_row1 = startlocus(num,1)+scale*cos(theta);
py_row1 = startlocus(num,2)+scale*sin(theta);
px_row2 = startlocus(num,1)+radio*cos(alpha);
py_row2 = startlocus(num,2)+radio*sin(alpha);
px = zeros(1,2*length(px_row1));
py = zeros(1,2*length(px_row1));
px(1:2:end) = px_row1;
px(2:2:end) = px_row2;
py(1:2:end) = py_row1;
py(2:2:end) = py_row2;
效果图: