How to move a circle on a sine curve?

I tried to move the circle on the sine curve by changing the centre of the circle and did the following:
theta =30:330
x=5*cosd(theta)
y=5*sind(theta)
x(301)=0
y(301)=0
x(302)=5*cosd(30)
y(302)=5*sind(30)
h=fill(x,y,'y')
axis ([0,200,0,50])
axis equal
for i=0:200
x1=i+5*cosd(theta)
y1=sind(i)+5*sind(theta)
x1(301)=i
y1(301)=sind(i)
x1(302)=i+5*cosd(30)
y1(302)=sind(i)+5*sind(30)
set(h,'xdata',x1,'ydata',y1)
pause(0.01)
end
But the circle keeps on moving on a straight line although y component of the changing circle should be changing.

 採用された回答

Mike Garrity
Mike Garrity 2014 年 10 月 10 日

1 投票

Actually the Y component is changing. But it's only changing in the range -1 to 1. Your YLim goes from something like -80 to 80, so it's hard to see that small motion. If you scale it way up you can see the limits change.
But there's something interesting going on here. That "axis equal" is a little strange here. It's saying that you want the scale to be the same on the X & Y axes. To do that, it overrides the YLim you chose (i.e. [0 50]) and computes a new YLim with the correct scale. But the we it computes that new YLim is that it centers it around whatever is in the axes. What's in the axes is the pacman that you're trying to move up and down. So it keeps changing the YLim in such a way as to move the pacman into the center. Because your Y motion was small, it was hard to see this happening.
As Mischa said, what you need to do is to call "axis manual" after calling "axis equal". That will say "now that I have equal scales, stop changing the limits". Does that make sense?

1 件のコメント

Airas Akhtar
Airas Akhtar 2014 年 10 月 11 日
Thanks.It does make sense and I found another way a much simpler way.I just called axis equal before the limits of my axes.And alternatively the axis manual jist worked out fine. The concept you gave is very helpful.

サインインしてコメントする。

その他の回答 (1 件)

Mischa Kim
Mischa Kim 2014 年 10 月 10 日
編集済み: Mischa Kim 2014 年 10 月 10 日

1 投票

Airas, use something like
...
h = fill(x,y,'y');
axis ([0,200,0,50])
axis equal
axis manual
for ii = 0:200
x = x + 1*cosd(ii);
y = y + 1*sind(ii);
set(h,'Vertices', [x(:) y(:)])
pause(0.01)
drawnow
end

1 件のコメント

Airas Akhtar
Airas Akhtar 2014 年 10 月 11 日
Using your technique my code got short.Thanks

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by