How to use the same equation to get shapes of different sizes and locations.

조회 수: 2 (최근 30일)
Cai Beacham
Cai Beacham 2020년 10월 26일
편집: VBBV 2020년 10월 26일
Hi,
I'm trying to get 3 circles stacked ontop of eachother (a snowman) each of a different size (heights of: 80, 50, 30). I'm not sure how to use the same equation (given below) for each of the different parameters. I've tried using a matrix but matlab isn't liking that. Am I making an obvious mistake?
I want to make it look neat so don't want to write out the same equation multiple times.
angles = linspace(0, 2*pi, 500);
radius = [40 25 15];
CenterX = [0 0 0];
CenterY = [40 105 145];
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(x, y, 'b-', 'LineWidth', 2);
hold on;
plot(CenterX, CenterY, 'k+', 'LineWidth', 3, 'MarkerSize', 14);
grid on;
axis equal;
xlabel('X', 'FontSize', 14);
ylabel('Y', 'FontSize', 14);

채택된 답변

VBBV
VBBV 2020년 10월 26일
편집: VBBV 2020년 10월 26일
Try this
angles = linspace(0, 2*pi, 500);
radius = [40 25 15];
CenterX = [0 0 0];
CenterY = [40 105 145];
for i = 1:length(radius)
for j = 1:length(angles)
x(i,j) = radius(i)*cos(angles(j)) + CenterX(i);
y(i,j) = radius(i)*sin(angles(j)) + CenterY(i);
end
end
plot(x(1,:), y(1,:), 'b-',x(2,:), y(2,:),'b', x(3,:), y(3,:),'b','LineWidth', 2);
hold on
plot(CenterX, CenterY, 'b+', 'LineWidth', 3, 'MarkerSize', 14);
grid
axis equal;
xlabel('X', 'FontSize', 14);
ylabel('Y', 'FontSize', 14);

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2020년 10월 26일
hi
this is my suggestion
radius = [40 25 15];
CenterX = [0 0 0];
CenterY = [40 105 145];
for ci = 1:length(radius)
[x,y] = XY(radius(ci),CenterX(ci),CenterY(ci));
plot(x, y, 'b-', 'LineWidth', 2);
hold on;
plot(CenterX, CenterY, 'k+', 'LineWidth', 3, 'MarkerSize', 14);
grid on;
axis equal;
xlabel('X', 'FontSize', 14);
ylabel('Y', 'FontSize', 14);
end
function [x,y] = XY(radius,CenterX,CenterY)
samples = 360;
angles = (0:samples-1)/samples*2*pi;
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
end

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by