Plotting figures with user defined functions

5 ビュー (過去 30 日間)
Derrick Joseph
Derrick Joseph 2019 年 9 月 28 日
編集済み: Matt J 2019 年 9 月 28 日
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 Center および File ExchangeCreating, 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