How do I plot this square?

6 views (last 30 days)
Tamara Dunford
Tamara Dunford on 2 Dec 2017
Answered: Star Strider on 2 Dec 2017
I would like to plot a red square with the vertices(1,2),(3,2),(3,4),(1,4).
I would then like to plot 2 blue diagonals of the square using dotted lines.
I need the plotting window in the region of [0,5]x[0,5] and the axes adjusted to look like a square.
Heres what i have so far:
x=[1,2,2,1]
y={2,3,4,1]
  2 Comments
Star Strider
Star Strider on 2 Dec 2017
Is this homework?
Tamara Dunford
Tamara Dunford on 2 Dec 2017
no just practicing

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 2 Dec 2017
Since it’s not homework, here you go:
figure(1)
patch([1 3 3 1], [2 2 4 4],'r')
hold on
plot([1 3], [2 4], ':b', 'Linewidth',1.5)
plot([1 3], [4 2], ':b', 'Linewidth',1.5)
hold off
axis([0 5 0 5])
axis equal
Since you want to learn more, I will let you figure out how it works. There are other ways to create the square (such as fill).. I prefer patch simply because I have more control over what it does.

Ghady Hajj
Ghady Hajj on 2 Dec 2017
x = [1 1 3 3 1 1 3];
y = [4 2 2 4 4 4 2];
d1_1 = [1 3];
d1_2 = [4 2];
d2_1 = [1 3];
d2_2 = [2 4];
plot(x,y,'r', 'LineWidth',1)
hold on
plot(d1_1,d1_2,'b', 'LineWidth',1)
plot(d2_1,d2_2,'b', 'LineWidth',1)
% to set both axis from 0 to 5
xlim([0,5])
ylim([0,5])
% to set the increment in each axis to 1
set(gca,'xtick',0:1:5)
set(gca,'ytick',0:1:5)
% or replace these lines:
% x = [1 1 3 3 1 1 3];
% y = [4 2 2 4 4 4 2];
% plot(x,y,'r', 'LineWidth',1)
% by
% rectangle('Position',[1 2 2 2]);
% for simplicity
Hope this will do the job for you. Cheers :)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!