建不了的模。 发表于 2014-12-9 13:28

基于Matlab绘制N角星


画N角星(N为大于5的奇数):七角星、九角星等
function PlotStarN(x0,y0,r,theta0,n,color)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Author     :   wacs5
%Email      :   wacs5@126.com
%Date       :   20100405
%Function   :   画n角星(n为大于5的奇数)
%Called     :   cos,cosd,deg2rad,error,fill,linspace,mod,plot,sin,sind
%Version    :   只要上述Reference函数可运行的Matlab版本都可以运行
%Usage      :   PlotStarN(x0,y0,r,theta0,n,color)
%           :       x0,y0是中心点坐标,缺省为(0,0)
%           :       r为半径,缺省为1
%           :       theta0为其中一个尖角的朝向,缺省为pi/2
%           :       n为星的角数,缺省为5
%           :       color为填充的颜色,缺省为红色
%Example    :   PlotStarN(0,0,1,pi/2,5,'r')
%See also   :   http://www.matlabsky.com/thread-6155-1-1.html
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (nargin<1)   x0=0;           end
if (nargin<2)   y0=0;           end
if (nargin<3)   r=1;            end
if (nargin<4)   theta0=pi/2;    end
if (nargin<5)   n=5;            end
if (nargin<6)   color='r';      end
if n<5 || mod(n,2)==0
    error('n必须是大于5的奇数');
end

FlagPlotEdge=0;  %不画边

theta1=theta0+linspace(0,2*pi,n+1);     %n角星的顶点的角度值
%%%%%%%%%%%%%%%%%%%%%%%%
%%n个顶点坐标
x1=x0+r*cos(theta1);
y1=y0+r*sin(theta1);
%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%
%%n角星边的交点坐标
r2=r*sind(180*(n-4)/2/n)/cosd(360/2/n); %交点的外接圆的半径
theta2=theta1+deg2rad(360/2/n);         %交点的角度值
x2=x0+r2*cos(theta2);
y2=y0+r2*sin(theta2);
%%%%%%%%%%%%%%%%%%%%%%%%

%%将顶点的交点混合隔开
x=;
y=;
x=;
y=;
fill(x,y,color)


if FlagPlotEdge==1
    xx=x1(mod(0:2:2*n,n)+1);
    yy=y1(mod(0:2:2*n,n)+1);
    plot(xx,yy,'k')
end
复制代码


figure;hold on
PlotStarN(-1.5, 1.5,1,pi/2,5,'r')
PlotStarN( 1.5, 1.5,1,pi/2,7,'b')
PlotStarN(-1.5,-1.5,1,pi/2,9,'g')
PlotStarN( 1.5,-1.5,1,pi/2,99,'y')
axis([-3,3,-3,3]);
axis equal
axis off


页: [1]
查看完整版本: 基于Matlab绘制N角星