How do I plot this square?
조회 수: 8 (최근 30일)
이전 댓글 표시
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
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179769/image.png)
댓글 수: 0
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 :)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!