Betsy Ross Problem

Matlab学习中的betsy ross 问题。用matlab函数画1777年的美国国旗。

 

五角星绘制部分是自己想出来的方法去画上的。具体代码参考如下。

先是绘制矩形的函数

1 function DrawRect(a,b,L,W,c)
2 %Adda a Rectangle to the current window .Assumes hold is on.
3 %The Rectangle has vertices (a,b),(a+L,b),(a,b+W),(a+L,b+W)
4 %The c is color which is in the form of 'rgb vector' or one of the built-in
5 %clolors such as 'r','g','y','b'
6 x=[a,a+L,a+L,a];
7 y=[b,b,b+W,b+W];
8 fill(x,y,c);

然后是绘制五角星的代码

 1 function DrawStar(x1,y1,dr,c)
 2 %the center of the five-stars is  at (x1,y1)
 3 %the radius of five-stars
 4 %the color of the five-star
 5 dtheta=2*pi/5;
 6 hold on
 7 axis equal off
 8 j=0:4;
 9 %外部五个点
10 x=x1+dr*cos(j*dtheta+pi/2);
11 y=y1+dr*sin(j*dtheta+pi/2);
12 %内部五个点
13 xl=x1+dr*cos(j*dtheta+2*pi/3)/2;
14 yl=y1+dr*sin(j*dtheta+2*pi/3)/2;
15 %Seperately take the ten points into vector
16 X=[x(1),xl(1),x(2),xl(2),x(3),xl(3),x(4),xl(4),x(5),xl(5)];
17 Y=[y(1),yl(1),y(2),yl(2),y(3),yl(3),y(4),yl(4),y(5),yl(5)];
18 %then draw it.
19 fill(X,Y,c);

然后调用上面的两个函数,绘制整个旗帜

 1 function DrawFlag(a,b,L1,W1,L2,r1,r2)
 2 %Adds a 13-star,13-stripe Colonial flag to the current
 3 %Figure window.Assumes hold id on
 4 %The flag is L1-by-W1 with lower left corner at(a,b).
 5 %The length fo the blue area is L2.The ring of stars has radius r1 and its
 6 %center is the center of the blue area
 7 %the radius of the individual stars is r2
 8 clc
 9 close all
10 figure
11 hold on
12 s=W1/13;
13 axis equal off
14 for k=1:13
15     %Draw the kth stripe
16     bk=b+(k-1)*s;
17     if rem(k,2)==1&&k<=6
18         DrawRect(a,bk,L1,s,'r');
19     elseif rem(k,2)==0&&k<=6
20         DrawRect(a,bk,L1,s,'w');
21     elseif rem(k,2)==1&&k>=6
22         DrawRect(a+L2,bk,L1-L2,s,'r');
23     else DrawRect(a+L2,bk,L1-L2,s,'w');
24     end
25 end
26 DrawRect(a,b+6*s,L2,7*s,'b');
27 %draw star
28 theta=2*pi/13;
29 xc=(a+L2)/2;
30 yc=9.5*s;
31 for k=0:12
32     xr=xc+r1*cos(k*theta+pi/2);
33     yr=yc+r1*sin(k*theta+pi/2);
34     DrawStar(xr,yr,r2,'w');5
35 end
36 hold off
原文地址:https://www.cnblogs.com/sytu/p/4339799.html