# Why isnt it drawing the Rectangle?

2 ビュー (過去 30 日間)
dieter alfred 2020 年 7 月 15 日
コメント済み: dieter alfred 2020 年 7 月 16 日
Hello, I wrote a Script with Drawing Functions for use in later Projects. But I have a problem. If I call the function drawrectangle It is only drawing the left and the top line of the rectangle. I cannot find a solution for this problem. What is my mistake?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = drawcircle(radius,xincircle,yincircle)
xcircle = radius*sin(circle) + xincircle;
ycircle = radius*cos(circle) + yincircle;
plot(xcircle,ycircle);
end
function [] = drawline(linex1,liney1,linex2,liney2)
linex = linex1:0.01:linex2;
liney = liney1:0.01:liney2;
plot(linex,liney);
end
function [] = drawpoint(pointx,pointy,thickness)
scatter(pointx,pointy,thickness,'filled');
end
function [] = drawrectangle(cornerx,cornery,width,height)
drawline(cornerx,cornery,cornerx+width,cornery);
drawline(cornerx+width,cornery,cornerx+width,cornery-height);
drawline(cornerx+width,cornery-height,cornerx,cornery-height);
drawline(cornerx,cornery-height,cornerx,cornery);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xlim([-10,10]);
ylim([-10,10]);
axis equal;
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
drawcircle(1,0,0);
drawline(-10,-10,10,10);
drawpoint(0,2,100);
drawrectangle(-5,5,10,10);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold off
Small explanation of the Code: over the first % seperator, I define a variable that is needed in the draw circle function. Between the first and the second seperator are the functions. Between the second and third seperator I configure some parameters for the figure and there is the hold on command. Between the third and the fourth seperator I call the functions. This is the section where my later code will be written. After the last it is just the hold off command.

サインインしてコメントする。

### 採用された回答

Walter Roberson 2020 年 7 月 15 日

function [] = drawline(linex1,liney1,linex2,liney2)
linex = linex1:0.01:linex2;
liney = liney1:0.01:liney2;
plot(linex,liney);
end
when you plot(), the length of x must be the same as the length of y. But since you are using a fixed step size, that would only happen if (linex2-linex1) is the same as (liney2-liney1) to within 0.01, which is not the case for you.
When you draw a straight line, you do not need to draw the intermediate points unless you want to put markers at them:
function [] = drawline(linex1,liney1,linex2,liney2)
linex = [linex1, linex2];
liney = [liney1,liney2];
plot(linex,liney);
end
##### 1 件のコメント表示非表示 なし
dieter alfred 2020 年 7 月 16 日
Thank you very much. Now it is working.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by