Why isnt it drawing the Rectangle?
1 次查看(过去 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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!