Plotting figures with user defined functions

5 Ansichten (letzte 30 Tage)
Derrick Joseph
Derrick Joseph am 28 Sep. 2019
Bearbeitet: Matt J am 28 Sep. 2019
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

Antworten (1)

Matt J
Matt J am 28 Sep. 2019
Bearbeitet: Matt J am 28 Sep. 2019
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

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by