How can I make my function plot lines to make a rectangle?

조회 수: 5 (최근 30일)
Eduardo Gallegos
Eduardo Gallegos 2022년 11월 23일
편집: Florian Bidaud 2022년 11월 23일
Hi, I can't figure out how to make my function plot lines.
Here is my code.
clear all, format compact, format shortg; close all; fclose all; clc;
% Rectangle 1
rect1.pos= [30,20]; % x and y coordinates of the lower left corner
rect1.width = 500;
rect1.height= 400;
rect1.color = 'b';
figure(1)
draw_rect_Eduardo_Gallegos_call(rect1)
axis equal;
% Rectangle 2
rect2.pos= [350,300]; % x and y coordinates of the lower left corner
rect2.width = 220;
rect2.height= 250;
rect2.color = 'r';
draw_rect_Eduardo_Gallegos_call(rect2)
% Rectangle 3
rect3.pos= [300,250]; % x and y coordinates of the lower left corner
rect3.width = 150;
rect3.height= 400;
rect3.color = 'c';
hold on;
draw_rect_Eduardo_Gallegos_call(rect3)
Here is the function code
function [] = draw_rect_Eduardo_Gallegos_call(rect)
plot(rect.height,rect.width,'o' )
end

채택된 답변

Florian Bidaud
Florian Bidaud 2022년 11월 23일
편집: Florian Bidaud 2022년 11월 23일
Hi,
Your function just plots one point. You need to plot all the points of your rectangle, and closing it by returning at the first point.
This should work as you want
function [] = draw_rect_Eduardo_Gallegos_call(rect)
plot([rect.pos(1) rect.pos(1) rect.pos(1)+rect.width rect.pos(1)+rect.width rect.pos(1)], ...
[rect.pos(2) rect.pos(2)+rect.height rect.pos(2)+rect.height rect.pos(2) rect.pos(2)], ...
'Color',rect.color)
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by