使用十边形绘制五边形

时间:2014-10-10 19:04:09

标签: matlab

我是matlab的新手,我被困在我的一个任务中,我需要使用十边形画一个五角星,我正在使用一个叫做'circle'的函数,它给出了所需圆圈的所有坐标,该函数采用圆心(cx,xy)的坐标,取半径(r)和向量数(n)。

这是我理想的想法:

Ideal output http://i528.photobucket.com/albums/dd323/kingboom4/expectedOutput.jpg

这就是我有多远:

How far I've got http://i528.photobucket.com/albums/dd323/kingboom4/output.jpg

这是'圈'功能代码:

function [xx,yy] = circle(cx, cy, r, n)
t=linspace(0, 2*pi, n+1);
x=r*cos(t);
y=r*sin(t);
xx= x+cx; 
yy= y+cy; 
end

这是五角星代码:

[x,y]=circle(0,0,5,5);
[x1,y1]=circle(0,0,4,10);

px=[0 x(1) x1(2) x(2) x1(4) x(3) x1(6) x(4) x1(8) x(5) x1(10)];
py=[0 y(1) y1(2) y(2) y1(4) y(3) y1(6) y(4) y1(8) y(5) y1(10)];
plot(x,y,x1,y1,px,py);

axis([-11 11 -11 11]);
axis equal;

2 个答案:

答案 0 :(得分:2)

function [xx, yy] = circle(cx, cy, r, n, phase)
 if nargin < 5  % checks if phase argument is provided
     phase = 0; % if not - default value is 0
 end;
 t = linspace(0, 2 * pi, n + 1);
 x = r * cos(t + phase); % phase added to rotate the coordinates
 y = r * sin(t + phase); % the same
 xx = x + cx; 
 yy = y + cy; 
end

并且

[x, y] = circle(0, 0, 5, 5); % coordinates of the points on external radius
[x1, y1] = circle(0, 0, 1.5, 5, 2 * pi / 5 / 2); 
           % less radius, same number of points, but rotated on half
           % of the angle between the points - for the internal radius

px = zeros(1, 2 * numel(x)); % prepare vectors where all x
py = zeros(1, 2 * numel(x)); % and y coordinates will be combined

px(1 : 2 : end) = x;  % interleave x values in one array
px(2 : 2 : end) = x1; % with x1 values in the same array

py(1 : 2 : end) = y;  % the same for y
py(2 : 2 : end) = y1; % and y1


plot(px, py); % plot pentagon
rectangle('Position', [-5, -5, 10, 10], 'Curvature', [1, 1]); % circle around

axis([-11 11 -11 11]);
axis equal;

结果

enter image description here

另一个例子。这个想法是一样的,但基于简单的数学计算,实现略有不同。

n = 5;
n1 = n + 1;
r1 = 5;
r2 = 1.5;
pi2 = pi / 2;

angles = linspace(0, 2 * pi, n1);

calcXY = @(angle, r, shift, phase) shift + r .* cos(angle + phase);

px = zeros(1, 2 * n1);
py = zeros(1, 2 * n1);

indexes = 1 : 2 : 2 * n1;
px(indexes) = calcXY(angles, r1, 0, 0);
py(indexes) = calcXY(angles, r1, 0, - pi2);

indexes = 2 : 2 : 2 * n1;
px(indexes) = calcXY(angles, r2, 0, 2 * pi2 / n);
py(indexes) = calcXY(angles, r2, 0, 2 * pi2 / n - pi2);


plot(px, py);
rectangle('Position', [-r1, -r1, 2 * r1, 2 * r1], 'Curvature', [1, 1]);

axis([-11 11 -11 11]);
axis equal;

对于n = 10

enter image description here

答案 1 :(得分:0)

只需要将十边形缩小就可以了。

'圈'功能:

function [xx,yy] = circle(cx, cy, r, n)
t=linspace(0, 2*pi, n+1);
x=r*cos(t);
y=r*sin(t);
xx= x+cx; 
yy= y+cy; 
end

五角星代码:

[x,y]=circle(0,0,5,5);
[x1,y1]=circle(0,0,1,10);
[x2,y2]=circle(0,0,5,100);

px=[x(1)  x1(2) x(2) x1(4) x(3) x1(6) x(4) x1(8) x(5) x1(10) x(1)];
py=[y(1)  y1(2) y(2) y1(4) y(3) y1(6) y(4) y1(8) y(5) y1(10) y(1)];

plot(x2,y2,px,py);

axis([-11 11 -11 11]);
axis equal;

结果:

Result http://i528.photobucket.com/albums/dd323/kingboom4/output-1.jpg