Plotting figures with user defined functions

5 次查看(过去 30 天)
I'm trying to plot an image of a train sketch using a rectangle function and a circle function but I don't understand why it's not giving the right output.
Here's my code:
plotrectangle(1, 1.5, 3, 2)
hold all;
plotrectangle(3, 3.5, .5, .5)
plotcircle(2.5,4,1)
plotcircle(1.5, 4.5, 2)
plotcircle(1.5,1,2)
plotcircle(3.5,1,2)
function [] = plotrectangle(x, y, l, w)
figure;
rectangle('Position', [x y l w]);
axis( [0 10 0 10] )
end
function [] = plotcircle(c1,c2,r)
t = 0:0.0001:2*pi;
x = r*cos(t)+c1;
y = r*sin(t)+c2;
plot(x, y);
axis( [0 5 0 5 ] )
end

回答(1 个)

Matt J
Matt J 2019-9-28
编辑:Matt J 2019-9-28
For some reason, you've given circle radii that are all off by a factor of 6.
plotrectangle(1, 1.5, 3, 2)
hold on
plotrectangle(3, 3.5, .5, .5)
plotcircle(2.5,4.5,1)
plotcircle(1.5, 4.5, 2)
plotcircle(1.5,1,3)
plotcircle(3.5,1,3)
hold off
axis equal
function [] = plotrectangle(x, y, l, w)
rectangle('Position', [x y l w]);
end
function [] = plotcircle(c1,c2,r)
r=r/6;
t = 0:0.0001:2*pi;
x = r*cos(t)+c1;
y = r*sin(t)+c2;
plot(x, y);
end

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by