Plot a small circle moving along a larger semi-circle

2 views (last 30 days)
I draw my semi circle with this code
%Equation for semi circle
th = linspace( pi, 0, 100);
R = 1; %or whatever radius you want
x = R*cos(th);
y = R*sin(th);
%draws the semi circle
h=line(60*x,60*y,...
'Parent', axs,...
'LineWidth',2) ;
after that i draw my circle on top of semi circle with
ball=rectangle('Position',[-5,60,r_ball,r_ball],...
'Curvature',[1,1],...
'FaceColor','b',...
'Parent',axs);
the initial position of the ball is placed at the top of the semicircle in the center. the problem is the way the rectangle() determines the position of the circle. it creates the smallest square that can contain the circle and plots that. so when i use the set() to modify its position it uses one of the vertices from the square ( which is why i must use -5 offset to visually center the ball on the semi circle)
problem with this, when i continue to animate the circle along the semi circle the ball begins to clip the hemisphere once it starts to reach the ends (where Y component is 0). which makes sense due to the offset. removing the offset will of course make the ball follow completely off the hemisphere until it reaches the ends.
how would i fix this so that the edge of the ball, follows smoothly the edge of the semi circle.

Accepted Answer

Joseph Cheng
Joseph Cheng on 5 May 2015
Edited: Joseph Cheng on 5 May 2015
well i'd say you were very close but should have tackled the problem from another angle.
%Equation for semi circle
th = linspace( pi, 0, 100);
R = 1; %or whatever radius you want
x = R*cos(th);
y = R*sin(th);
%draws the semi circle
h=line(60*x,60*y,...
'LineWidth',2) ;
axis equal
%ball=rectangle('Position',[-5,60,10,10 ],...
% 'Curvature',[1,1],...
% 'FaceColor','b');
th = linspace( pi, 0, 15);
R = 60+5; %radius of path of center of circle as it rolls around semicircle
x2 = R*cos(th)-5; %offset to LL corner of the rectangle
y2 = R*sin(th)-5; %offset to LL corner of the rectangle
h2=line(x2,y2,...
'LineWidth',2,'color','r') ;
axis equal
for ind = 1:length(x2)
ball=rectangle('Position',[x2(ind),y2(ind),10,10],...
'Curvature',[1,1],...
'FaceColor','b');
end
What you should have done is thought it as what is the path of the center of the circle and then offset it by where you reference the center of the circle.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!