How do I plot this square?

8 次查看(过去 30 天)
Tamara Dunford
Tamara Dunford 2017-12-2
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 个)

Star Strider
Star Strider 2017-12-2
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 2017-12-2
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 :)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by