need help to write a program to draw the rectangle decrived down below.
조회 수: 29 (최근 30일)
이전 댓글 표시
rect1.x = 30; % x-coordinate of the lower left corner
rect1.y = 30; % y-coordinate of the lower left corner
rect1.width = 500;
rect1.height= 400;
rect1.color = 'b';
% Rectangle 2
rect2.x = 350; % x-coordinate of the lower left corner
rect2.y = 300; % y-coordinate of the lower left corner
rect2.width = 220;
rect2.height= 250;
rect2.color = 'r';
% Rectangle 3
rect3.x = 300; % x-coordinate of the lower left corner
rect3.y = 250; % y-coordinate of the lower left corner
rect3.width = 150;
rect3.height= 400;
rect3.color = 'c';
rect1.x = 30; % x-coordinate of the lower left corner
rect1.y = 30; % y-coordinate of the lower left corner
rect1.width = 500;
rect1.height= 400;
rect1.color = 'b';
% Rectangle 2
rect2.x = 350; % x-coordinate of the lower left corner
rect2.y = 300; % y-coordinate of the lower left corner
rect2.width = 220;
rect2.height= 250;
rect2.color = 'r';
% Rectangle 3
rect3.x = 300; % x-coordinate of the lower left corner
rect3.y = 250; % y-coordinate of the lower left corner
rect3.width = 150;
rect3.height= 400;
rect3.color = 'c';
답변 (1개)
Ameer Hamza
2020년 5월 8일
편집: Ameer Hamza
2020년 5월 8일
Always use an array instead of naming the variable like rect1, rect2, .... It makes the code much simpler. Following code draw rectangles using patches
rect(1).x = 30; % x-coordinate of the lower left corner
rect(1).y = 30; % y-coordinate of the lower left corner
rect(1).width = 500;
rect(1).height= 400;
rect(1).color = 'b';
% Rectangle 2
rect(2).x = 350; % x-coordinate of the lower left corner
rect(2).y = 300; % y-coordinate of the lower left corner
rect(2).width = 220;
rect(2).height= 250;
rect(2).color = 'r';
% Rectangle 3
rect(3).x = 300; % x-coordinate of the lower left corner
rect(3).y = 250; % y-coordinate of the lower left corner
rect(3).width = 150;
rect(3).height= 400;
rect(3).color = 'c';
figure;
axes();
hold on
for i=1:numel(rect)
r = rect(i);
rect_x = [r.x r.x+r.width r.x+r.width r.x];
rect_y = [r.y r.y r.y+r.height r.y+r.height];
patch(rect_x, rect_y, r.color);
end

댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!