How can I make the circle move around the rectangle?

7 views (last 30 days)
Hi, I want to make the circle to move and rotate around the rectangle while the rectangle stays at one place and move in its own trajectory (the photo shown is an example)
Here is the matlab code that i did so far
rectangle('Position',[0, 0, 1.78, 0.62],'Curvature', 0.5)
axis equal
hold on
for i = 1:length(t)
center = [x2(i),y2(i)];
h = viscircles(center,r3,'color','r');
pause(0.1)
delete(h)
end
hold off
  1 Comment
Jeffrey Clark
Jeffrey Clark on 16 Jun 2022
So your only problem is finding the points on the rounded rectangle (x2 and y2)? Since rectangle doesn't make its points available you will have to reverse engineer to find the straight and circular line segments (test by overplotting the points you come up with what rectangle did and then provide some reasonable steps along the rounded rectange. Instead of viscircles you could also use rectangle to produce a circle as seen in documentation example.
Since I don't have the Image processing Toolbox I would have created the rounded rectangle and circle using math and plot; would still have had to generate points along the straight lines and maybe reduce the points along the circular rounds.

Sign in to comment.

Answers (1)

Tridib
Tridib on 24 Apr 2025
Hi @wan s,
Your code does not calculate the circle’s center as it moves around the rectangle or update the rectangle’s position if needed.
To fix this, calculate the circle’s center for each frame as it moves along the rectangle by parameterizing the rectangle and finding the center position for each frame. Also, update the rectangle’s position in each frame (if you need to move the rectangle as well), and draw a line on the circle as a visual indication to show its rotation.
rect_w = 1.2; %rectangle width
rect_h = 0.5; %rectangle height
rect_y = 0; % y position of the rectangle to keep it fixed vertically
r = 0.08; % circle radius
% perimeter parameterization
perim = 2*(rect_w + rect_h);
num_steps = 200; % total animation steps
% rectangle motion along x
rect_x0 = 0.6; % Initial x position
rect_dx = 1; % how much the rectangle moves along x
figure;
axis equal
axis([-0.5 3 -1 1]) % visible area of the plot
hold on
% animation loop
for k = 1:num_steps
% calculating rectangle's current x-position for this frame
rect_x = rect_x0 + rect_dx * sin(2*pi*k/num_steps); % smooth back and forth movement along the x-axis using a sine wave
% parameter t moves around the rectangle's perimeter
% determines how far along the perimeter the circle has traveled for this frame
% it wraps around using mod so after one full trip, it starts again
t = mod(perim * (k/num_steps), perim);
% this determines which edge of the rectangle the circle is currently on
% and calculates its center coordinates (cx, cy) on the perimeter accordingly
% also sets tangent_angle to the direction the circle is moving along the current edge.
if t < rect_w
% Bottom edge (left to right)
cx = rect_x + t;
cy = rect_y;
tangent_angle = 0; % tangent along +x
elseif t < rect_w + rect_h
% Right edge (bottom to top)
cx = rect_x + rect_w;
cy = rect_y + (t - rect_w);
tangent_angle = pi/2; % tangent along +y
elseif t < 2*rect_w + rect_h
% Top edge (right to left)
cx = rect_x + rect_w - (t - (rect_w + rect_h));
cy = rect_y + rect_h;
tangent_angle = pi; % tangent along -x
else
% Left edge (top to bottom)
cx = rect_x;
cy = rect_y + rect_h - (t - (2*rect_w + rect_h));
tangent_angle = -pi/2; % tangent along -y
end
% the angle increases proportional to distance traveled
% Calculates the rotation angle (theta) of the line on the circle, making it
% rotate as the circle rolls along the perimeter. The 0.1 factor slows down the rotation
theta = 2*pi*(t/r)*0.1; % This makes the line rotate as if the circle is rolling
% The line rotates relative to the tangent (direction of motion)
% calculates the angle at which to draw the line on the circle,
% combining the direction of motion with the rolling rotation
line_angle = tangent_angle + theta;
% compute end point of the line
lx = cx + r*cos(line_angle);
ly = cy + r*sin(line_angle);
cla; % clear axes
% draw rectangle at its current position
rectangle('Position', [rect_x, rect_y, rect_w, rect_h], 'Curvature', 0.05, 'LineWidth', 2);
% draw moving circle
viscircles([cx cy], r, 'Color', 'r');
% draw rotating line on the circle
plot([cx, lx], [cy, ly], 'b-', 'LineWidth', 2);
pause(0.02)
end
hold off
For more help, refer to the following documentation:
Hope this helps!

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!