Why isnt it drawing the Rectangle?
조회 수: 8 (최근 30일)
이전 댓글 표시
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?
radius = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [] = drawcircle(radius,xincircle,yincircle)
circle = 0:0.01:2*pi*radius;
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.
댓글 수: 0
채택된 답변
Walter Roberson
2020년 7월 15일
편집: 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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!