2014-10-10 129 views
0

我是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 

這裏是pentagra米代碼:

[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
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

我,我說我是新來的matlab對不起,您的代碼看起來相當複雜,你能解釋一下嗎? – Waj 2014-10-10 19:37:09

+0

@ Kingboom4我已添加一些評論 – Cheery 2014-10-10 19:40:56

+0

@ Kingboom4我添加了一些代碼。 – Cheery 2014-10-10 20:46:16

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