MATLAB Answers

How to rotate a figure around an origin that is rotating?

5 ビュー (過去 30 日間)
MSolano
MSolano 2021 年 6 月 7 日
編集済み: MSolano 2021 年 6 月 9 日
Hello, I'm trying to rotate a circle P(r1,theta1) around an origin given at coordinates (r,theta) that is rotating at the same time, the resulting trajectory of the point should look like the image in the right. I can make the circle P rotate around the origin (0,0) with the code below but I have no idea how to do it move as it should. As you can see in the gif it doesn't move following the red circle path, I would really appreciate any idea. Thanks!
close all, clc, clear
%% 1: Generating Data
ang = linspace(0,2*pi,50); % Angle from 0 to 2pi
% Data to draw black fixed circle
xp=5*cos(ang); % Coordinate to draw black circle
yp=5*sin(ang); % Coordinate to draw black circle
% Data to draw outter circles pattern
ra = 1; % Radius of inner circle
xb = ra*cos(ang); % Coordinate to draw inner circle
yb = ra*sin(ang); % Coordinate to draw inner circle
Nb=6; % Number of balls
rho1 = 4; % Radius of inner circle, polar coordinate
rmb = 0.1; % Radius of the ball mark
rxmb= rmb*cos(ang); % Coordinate to draw ball mark
rymb= rmb*sin(ang); % Coordinate to draw ball mark
%% 2: Draw/Render Scenario
for angle=1:360
%Clear the figure
clf
% PLOT FIXED INNER CIRCLE
plot(xp,yp, 'color', 'k');
hold on
% PLOT CIRCULAR PATTERN OF GRAY CIRCLES
for iter=1:Nb % Draw until Nb balls
theta1 = 2*pi*(Nb-iter)/Nb; % Angle of position inner circles, polar coordinate (same spacing)
[x,y] = pol2cart(theta1,rho1); % Transform polar to cartesian coordinates
cox=x+xb; % Coordinate to draw moving circles
coy=y+yb; % Coordinate to draw moving circles
balls = plot(cox,coy,'color','r'); % Plot circles
rotate(balls,[0 0 1],angle); % Rotate moving pattern of circles
end
% Drawing mark in the ball and rotating
Rhom = 3;
Thetam = 2*pi;
[x_mb1,y_mb1] = pol2cart(Thetam,Rhom);
fill_mark_ball = fill(x_mb1 + rxmb, y_mb1 + rymb,'r');
rotate(fill_mark_ball,[0 0 1],angle);
%% 3: Take Snapshot
drawnow
end
  2 件のコメント
MSolano
MSolano 2021 年 6 月 8 日
This is exactly what I needed, I changed the equations for the hypocycloid and it worked! Thank you!!

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

採用された回答

darova
darova 2021 年 6 月 8 日
Try this
t = linspace(0,2*pi,20);
[x,y] = pol2cart(t,1);
n = 6; % number of small circles
h = zeros(1,n); % pre-allocation objects
plot(5*x,5*y)
h1 = line(0,0,'marker','.'); % dot
% draw smal circles
for i = 1:6
h(i) = line(x+4*cosd(i/n*360),y+4*sind(i/n*360));
end
for i = 1:20
% rotate dot using hypocycloid formulas
set(h1,'xdata',4*cosd(2*i)+cosd(5*i))
set(h1,'ydata',4*sind(2*i)-sind(5*i))
rotate(h,[0 0 1],2,[0 0 0]) % small circles
pause(1)
end
  1 件のコメント
MSolano
MSolano 2021 年 6 月 8 日
Thank you!

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

その他の回答 (1 件)

VBBV
VBBV 2021 年 6 月 7 日
編集済み: VBBV 2021 年 6 月 7 日
close all, clc, clear
%% 1: Generating Data
ang = linspace(0,2*pi,50); % Angle from 0 to 2pi
% Data to draw black fixed circle
xp=5*cos(ang); % Coordinate to draw black circle
yp=5*sin(ang); % Coordinate to draw black circle
% Data to draw outter circles pattern
ra = 1; % Radius of inner circle
xb = ra*cos(ang); % Coordinate to draw inner circle
yb = ra*sin(ang); % Coordinate to draw inner circle
Nb=6; % Number of balls
rho1 = 4; % Radius of inner circle, polar coordinate
rmb = 0.1; % Radius of the ball mark
rxmb= rmb*cos(ang); % Coordinate to draw ball mark
rymb= rmb*sin(ang); % Coordinate to draw ball mark
%% 2: Draw/Render Scenario
for angle=1:360
%Clear the figure
clf
% PLOT FIXED INNER CIRCLE
plot(xp,yp, 'color', 'k');
hold on
% PLOT CIRCULAR PATTERN OF GRAY CIRCLES
for iter=1:Nb % Draw until Nb balls
theta1 = 2*pi*(Nb-iter)/Nb; % Angle of position inner circles, polar coordinate (same spacing)
[x,y] = pol2cart(theta1,rho1); % Transform polar to cartesian coordinates
cox=x+xb; % Coordinate to draw moving circles
coy=y+yb; % Coordinate to draw moving circles
balls = plot(cox,coy,'color','r'); % Plot circles
rotate(balls,[0 0 1],angle); % Rotate moving pattern of circles
pause(0.5); % set this value according to outer circle move
end
% Drawing mark in the ball and rotating
Rhom = 3;
Thetam = 2*pi;
[x_mb1,y_mb1] = pol2cart(Thetam,Rhom);
fill_mark_ball = fill(x_mb1 + rxmb, y_mb1 + rymb,'r');
rotate(fill_mark_ball,[0 0 1],angle);
pause(0.5) %% use a pause command
%% 3: Take Snapshot
drawnow
end
Use a pause command at end of loop,
for iter=1:Nb % Draw until Nb balls
theta1 = 2*pi*(Nb-iter)/Nb; % Angle of position inner circles, polar coordinate (same spacing)
[x,y] = pol2cart(theta1,rho1); % Transform polar to cartesian coordinates
cox=x+xb; % Coordinate to draw moving circles
coy=y+yb; % Coordinate to draw moving circles
balls = plot(cox,coy,'color','r'); % Plot circles
rotate(balls,[0 0 1],angle); % Rotate moving pattern of circles
pause(0.5); % set this value according to outer circle move
end
  1 件のコメント
MSolano
MSolano 2021 年 6 月 8 日
Thanks for taking the time to answer, I tried with the pause (0.5) but it's just making the animation slower. I want to draw the 6 balls at once and at the same time draw the small mark that follows the path (red circumference). As if I wanted to animate the movement of the moon around earth (red circle) wich goes around the sun ([0,0] origin) at the same time.

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by