How to use the same equation to get shapes of different sizes and locations.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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);
0 commentaires
Réponse acceptée
VBBV
le 26 Oct 2020
Modifié(e) : VBBV
le 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);
0 commentaires
Plus de réponses (1)
Mathieu NOE
le 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
0 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!