How to draw a square with specific plot points

488 vues (au cours des 30 derniers jours)
Paul
Paul le 8 Oct 2013
Commenté : Pranav le 29 Juil 2024
I'm trying to create two squares, one larger one, and the other within it.
I attempted to create one using matrices. I create one with x = [0 0 1 1] and y = [0 1 1 0]
I wanted a line to connect each one of the points, to thus create a line. However, when I did that, I only received three lines connect between the dots for some reason. The bottom points (0,0) and (1,0) for some reason were not connected. I then attempted to make a second square, and the same thing happened, with the two lower points in regards to their Y points did not connect. I tried using the rectangle function as well, but can't get a square within a square to appear. Any help would be great. Thanks

Réponse acceptée

Image Analyst
Image Analyst le 8 Oct 2013
Modifié(e) : Image Analyst le 29 Juil 2024
You need 5 points - the last one is to "close off" the square, otherwise it won't be closed because you didn't draw the line back to the starting point. So, try this
% Draw first, outer rectangle.
x1=0;
x2=1;
y1=0;
y2=1;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 3);
hold on;
% Draw the second, inner rectangle.
x1=0.25;
x2=.75;
y1=0.25;
y2=.75;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'r-', 'LineWidth', 3);
% Define the plot region outer limits of the axes.
xlim([-0.5, 1.5]);
ylim([-0.5, 1.5]);
grid on;
axis equal % Make sure the aspect ratio is correct so that they look like squares.
  2 commentaires
Paul
Paul le 8 Oct 2013
perfect!
If you ever had to create two similar objects like this (for example two squares), would you recommend going about it another way? I tried with the rectangle function as well, it seemed potentially more convenient.
Once again, thanks for the response.
Image Analyst
Image Analyst le 8 Oct 2013
I think the way I did it is easier to understand than rectangle. In my mind I just visualize starting at the upper left corner and marching around the square in a clockwise fashion. First I do the x's then I do the y's. And so it's really quick for me to construct the 5 element x and y matrices.

Connectez-vous pour commenter.

Plus de réponses (4)

Camilo Malagon Nieto
Camilo Malagon Nieto le 8 Mai 2019
Worth to say that there is a function now to make a rectangle and smooth the corners.
Cheers,
  1 commentaire
Tamar Hoory
Tamar Hoory le 2 Juin 2022
thanks for the useful answer!

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 29 Juil 2024
Depending on what you want to do with those shapes, creating a polyshape array may be useful.
x = [0 0 1 1];
y = [0 1 1 0];
P1 = polyshape(x, y);
plot(P1)
axis square
P2 = translate(scale(P1, 0.5), [0.25 0.25]);
figure
plot([P1, P2])
axis square

NAGENDRA KUMAR
NAGENDRA KUMAR le 12 Sep 2017
Déplacé(e) : Image Analyst le 29 Juil 2024
Please write this code
x = [0 0 1 1 0];
y = [0 1 1 0 0];
plot(x,y,'r', 'LineWidth',3)

Pranav
Pranav le 29 Juil 2024
The IMAGE ANALYST did a great job at answering this question.
  2 commentaires
Image Analyst
Image Analyst le 29 Juil 2024
Thanks! 🙂
Pranav
Pranav le 29 Juil 2024
Anytime, Keep up the great work!!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by