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

5 次查看(过去 30 天)
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