How do I plot a square given the length of the sides and the center of x and y?

6 vues (au cours des 30 derniers jours)
I have this code to graph a square based on the input data that the user enters, which is L (length of the sides) and x0, y0 (the center of X and Y). When my square is graphed it comes out with these lines and I don't know how to remove or limit them.
r = input("Enter the radius of the circle: ");
l = input("Enter the length of the sides of the square: ");
x0 = input("Enter the center of x: ");
y0 = input("Enter the center of y: ");
%SQUARE
figure(1),clf;
x = linspace(0,1,5);
y = zeros(1,5);
hold on
x = cosd(0) * l * x - sind(0) * l * y;
y = sind(0) * l * x + cosd(0) * l * y;
x2 = cosd(0) * l * x - sind(0) * l * y;
y2 = l + sind(0) * l * x + cosd(0) * l * y;
x3 = cosd(90) * l * x - sind(90) * l * y;
y3 = sind(90) * l * x + cosd(90) * l * y;
x4 = l + cosd(90) * l * x - sind(90) * l * y;
y4 = sind(90) * l * x + cosd(90) * l * y;
plot(x,y)
plot(x2,y2)
plot(x3,y3)
plot(x4,y4)
hold off
axis equal

Réponse acceptée

Matt J
Matt J le 11 Nov 2021
One way,
[x0,y0,L]=deal(2,1,4); %example input parameters
p=polyshape( [x0,y0]+[-1 -1; -1 1; 1 1; 1 -1]*L/2 );
plot(p)
  2 commentaires
Matt J
Matt J le 11 Nov 2021
You're welcome, but please Accept-click the answer to indicate that it provided a solution.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Install Products 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!

Translated by