【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《MATLAB趣味编程(五星红旗)》,欢迎阅读!
本程序模拟画了一面五星红旗,纯属学习MATLAB之作。 本程序由realghost编写,如有转载请保留此信息。 如要交流请与 slqinyi@163.com 联系。
function nationflag() axis equal
axis([0 5 0 3.09]) axis off hold on
fill([0 5 5 0],[0 0 3.09 3.09],'r') drawfive(1,2,pi/10,0.5,'y')
drawfive(1+cos(2*pi/10),2+sin(2*pi/10),0,0.15,'y') drawfive(1+cos(2*pi/30),2+sin(2*pi/30),pi/5,0.15,'y') drawfive(1+cos(-2*pi/30),2+sin(-2*pi/30),pi/10,0.15,'y') drawfive(1+cos(-2*pi/10),2+sin(-2*pi/10),-pi/15,0.15,'y')
function drawfive(x,y,theta,r,color) axis equal
t = linspace(0+theta,2*pi+theta,6); xr = x+r*cos(t); yr = y+r*sin(t); xk = zeros(1,10); yk = zeros(1,10); xk(1) = xr(1); yk(1) = yr(1); [xk(2) yk(2)] = linepoint([xr(1) yr(1)],[xr(3) yr(3)],[xr(2) yr(2)],[xr(5) yr(5)]);
xk(3) = xr(2); yk(3) = yr(2); [xk(4) yk(4)] = linepoint([xr(2) yr(2)],[xr(4) yr(4)],[xr(1) yr(1)],[xr(3) yr(3)]);
xk(5) = xr(3); yk(5) = yr(3); [xk(6) yk(6)] = linepoint([xr(2) yr(2)],[xr(4) yr(4)],[xr(3) yr(3)],[xr(5) yr(5)]);
xk(7) = xr(4); yk(7) = yr(4); [xk(8) yk(8)] = linepoint([xr(3) yr(3)],[xr(5) yr(5)],[xr(1) yr(1)],[xr(4) yr(4)]);
xk(9) = xr(5); yk(9) = yr(5); [xk(10) yk(10)] = linepoint([xr(1) yr(1)],[xr(4) yr(4)],[xr(2) yr(2)],[xr(5) yr(5)]);
fill(xk,yk,color)
axis equal
function [x,y]=linepoint(p1,p2,p3,p4)
%ÇóÒÔp1,p1Ϊ¶ËµãµÄÖ±ÏßÓëÒÔp3,p4Ϊ¶ËµãµÄÖ±ÏߵĽ»Õ½×ø±ê x1 = p1(1); y1 = p1(2); x2 = p2(1); y2 = p2(2); x3 = p3(1);
y3 = p3(2); x4 = p4(1); y4 = p4(2);
if x1==x2%µÚÒ»ÌõÖ±Ïß´¹Ö±ÓÚxÖá x = x1;
k2 = (y4-y3)/(x4-x3); y = k2*(x-x3)+y3; else
if x3==x4 x = x3;
k1 = (y2-y1)/(x2-x1); y = k1*(x-x1)+y1; else
k1 = (y2-y1)/(x2-x1); k2 = (y4-y3)/(x4-x3);
sol = [-k1 1;-k2 1]\[y1-k1*x1;y3-k2*x3]; x = sol(1); y = sol(2); end end
效果图:
本文来源:https://www.wddqxz.cn/c237a6220722192e4536f623.html