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

2 views (last 30 days)
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);

Accepted Answer

VBBV
VBBV on 26 Oct 2020
Edited: VBBV on 26 Oct 2020
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);

More Answers (1)

Mathieu NOE
Mathieu NOE on 26 Oct 2020
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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!