필터 지우기
필터 지우기

How do I plot this square?

조회 수: 8 (최근 30일)
Tamara Dunford
Tamara Dunford 2017년 12월 2일
답변: Star Strider 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일
Is this homework?
Tamara Dunford
Tamara Dunford 2017년 12월 2일
no just practicing

댓글을 달려면 로그인하십시오.

답변 (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