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

6 ビュー (過去 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 Center および File ExchangeData Exploration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by