How to make rotate the circular pattern of balls in a while loop?

Hello! I need to make the 12 balls rotate like the blue ball they don't need to have the red mark, just move at BSF frequency, I tried doing the for loop to make the 12 balls inside the while loop but it just plots a lot of balls, also tried to draw the balls with a marker 'o' but can´t do draw the 12 balls pattern
.
close all
clear
clc
clf;
% Entry Values
Nb = 12; % Number of balls
BSF = 0.222; % Frequency (Hz)
%% Data for Animation
D2 = 130;
R2 = D2/2;
d1 = 90;
d = 0.7*d1;
ang=linspace(0,2*pi,50);
xpi=R2*cos(ang);
ypi=R2*sin(ang);
patch(xpi,ypi,[0.90,0.90,0.90]); % Draw static circle
hold on
% Red mark data (Hypocycloid)
sz_mai =(d+d1)/8;
Ra = 65;
ra = 10;
ka = Ra/ra;
ka_= ka-1;
rh = 55;
ramdi = (d+d1)/4;
pmdi = plot(NaN,NaN,'-o','color', 'k', 'LineWidth', 1, 'MarkerSize', sz_mai, 'MarkerFaceColor', 'r','LineWidth', 1) ;
% Plot Balls
% Moving ball
pb = plot(NaN,NaN,'-o','color', 'k', 'LineWidth', 1, 'MarkerSize',35, 'MarkerFaceColor', 'c','LineWidth', 1) ;
% Circular Pattern
a = linspace(0,2*pi,20);
[x8,y8] = pol2cart(a,10);
fill_b=zeros(1,Nb);
h=zeros(1,Nb);
for angle = 1:Nb
h(angle) = line(x8+rh*cosd(angle/Nb*360), y8+rh*sind(angle/Nb*360) ,'color', 'k');
fill_b(angle) = fill(x8+rh*cosd(angle/Nb*360), y8+rh*sind(angle/Nb*360),[0.65,0.65,0.65]);
end
% Plot red Mark
ph = plot(NaN,NaN,'-*','LineWidth', 5, 'color', 'r','MarkerFaceColor', 'r','MarkerSize', 0.01') ;
%% Loop for Animation
tic
while (toc < 5)
hold on
% Red Mark
t = toc ;
ph.XData = [rh*cos(-2*pi*BSF*t) ra*ka_*cos(-2*pi*BSF*t) + ra*cos(ka_*2*pi*BSF*t)] ;
ph.YData = [rh*sin(-2*pi*BSF*t) ra*ka_*sin(-2*pi*BSF*t) - ra*sin(ka_*2*pi*BSF*t)] ;
% Ball
pb.XData = (rh*cos(-2*pi*BSF*t)) ;
pb.YData = (rh*sin(-2*pi*BSF*t)) ;
% 12 Gray balls
% How to introduce the frequency BSF so that the gray circles pattern
% rotates????
drawnow
end

 Accepted Answer

I think you are very close to what you want.
All I had to do to make this animate is to comment out the `hold on` line and increase the end test for the while loop (I used 50 instead of 5).
Also you probably want to add an `axis equal` command before the while loop so that your circle looks like a circle.

4 Comments

Thank you for taking the time to answer, I did what you recommended, but how should I introduce the frequency BSF for the gray balls to rotate just like the blue ball does?
I wrote:
rotate( h, [0 0 -1], BSF, [0 0 0]);
rotate(fill_b, [0 0 -1], BSF, [0 0 0]);
it's moving but not at that speed
You are drawing the grey balls outside of the loop so they will not move. I'm not sure what you are expecting or wanting them to do. Please clarify.
Yes this is it, thanks for your help. When they move the angle between each other keeps changing because the last ball doesn't move from the start point and I need the angle to be the same while moving, sorry I did not explain myself correctly.
I'm glad that I was able to help.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Asked:

on 9 Jun 2021

Commented:

on 11 Jun 2021

Community Treasure Hunt

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

Start Hunting!