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;
結果
而另一示例。這個想法是一樣的,但基於一個簡單的數學,實現略有不同。
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
我,我說我是新來的matlab對不起,您的代碼看起來相當複雜,你能解釋一下嗎? – Waj 2014-10-10 19:37:09
@ Kingboom4我已添加一些評論 – Cheery 2014-10-10 19:40:56
@ Kingboom4我添加了一些代碼。 – Cheery 2014-10-10 20:46:16