matlab如何画五星红旗

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

生成五角星顶点的子函数(GetPentagramVertices.m)

function [x y] = GetPentagramVertices(centerPos, radius, orientation)

% 子函数:获得五角星的十个顶点

%orientation 定位

Rad_ext = radius .* ones(1,5);

%ones生成1行五列的矩阵,所有参数为1

Ori_ext = ([0:72:359] + 90 +orientation).* pi/ 180;

%[0:72:359]以72度为间隔产生五个数

Rad_int = radius .* sind(18)./sind(126) .* ones(1,5);

% sind(30)=0.5

Ori_int = ([0:72:359] - 90 +orientation).* pi/ 180;

Ori_int = Ori_int([4 5 1 2 3]);

%强制换序

Rad_all = zeros(1,10);

Ori_all = zeros(1,10);

Rad_all([1:2:10 2:2:10]) = [Rad_ext Rad_int];

Ori_all([1:2:10 2:2:10]) = [Ori_ext Ori_int];

[x y] = pol2cart(Ori_all, Rad_all);

%pol2cart在MATLAB中,该函数用于把极坐标(柱坐标)转换为笛卡尔坐标(指平面直角坐标或空间直角坐标);把柱坐标(THETA,RHO,Z)转换为对应的三维笛卡尔坐标(X,Y,Z)。

x = x + centerPos(1);

y = y + centerPos(2);

程序是画静态的五星红旗,并将图像以'FiveStarRedFlag.png'保存在当前目录下。这个五星红旗的比例是按照《中华人民共和国国旗制法说明》绘制的

第一行是调整国旗分辨率的,可以自行修改,但是要保持3:2的比例。flag_dimension_in_pixel = [600 400];

figure_leftbottom_pos = [100 100];

bkg_color = [1 0 0];

star_color = [1 1 0];

flag_horiz_units_num = 30;

flag_verti_units_num = 20;

star_vertices_x = zeros(10, 5);

star_vertices_y = zeros(10, 5);

stars(5).center_pos = [];

stars(1).center_pos = [5 15];

stars(1).radius = 3;

stars(1).orientation = 0;

stars(2).center_pos = [10 18];

stars(2).radius = 1;

stars(2).orientation = 0;

stars(3).center_pos = [12 16];

stars(3).radius = 1;

stars(3).orientation = 0;

stars(4).center_pos = [12 13];

stars(4).radius = 1;

stars(4).orientation = 0;

stars(5).center_pos = [10 11];

stars(5).radius = 1;

stars(5).orientation = 0;

%类似于c#的自定义函数调用?

for i = 2:5

dists = deal(stars(i).center_pos - stars(1).center_pos);

stars(i).orientation = atan(dists(2)/dists(1)).*180/pi + 90;

end

%% Prepare the background

flag_figure = figure('Units', 'pixels', ...

'Position', [figure_leftbottom_pos, flag_dimension_in_pixel]);

flag_axes = axes('Parent', flag_figure, 'Units','pixels', ...

'Position', [1 1 flag_dimension_in_pixel], 'Color', [1 0 0]);

set(flag_axes, 'XLim', [0 flag_horiz_units_num], 'YLim', [0 flag_verti_units_num]);

rectangle('Position', [0 0 flag_horiz_units_num flag_verti_units_num], ...

'FaceColor', bkg_color ,'EdgeColor' ,bkg_color);

set(flag_axes, 'Visible','off');

%% Get the Star's Vertices

for i = 1:5

[star_vertices_x(:,i) star_vertices_y(:, i)] = ...

GetPentagramVertices(stars(i).center_pos, ...

stars(i).radius,stars(i).orientation);

end

star_handles = patch(star_vertices_x, star_vertices_y, star_color);

set(star_handles, 'EdgeColor', star_color);

flag_image = frame2im(getframe(flag_figure));

imwrite(flag_image, 'FiveStarRedFlag.png');

% 如装有Image Processing Toolbox的话,解注下面几行可以保存图形文件

%

% [filename, pathname, filterindex] = ...

% uiputfile('*','Save the Nation Flag as...')

%

% imwrite(flag_image, [pathname filename]);

相关文档
最新文档