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

6 views (last 30 days)
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

Accepted Answer

Florian Bidaud
Florian Bidaud on 23 Nov 2022
Edited: Florian Bidaud on 23 Nov 2022
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

More Answers (0)

Categories

Find more on Data Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!