How to create a moving filled-color circle in non-square limits for x and y axes?

4 次查看(过去 30 天)
I'm using viscircle to create a moving circle in a non-square limits for x and y axes, but this makes the moving circle become ellipse. Can someone help me to get a circle and that is aslo filled-color?
I also have another question. Is there a way to save the same amount of images as .gif but with a smaller size?
close all
load('z_p.mat')
dt = 1e-4;
h = figure;
filename = 'zp.gif';
for m = 1:100:length(z_p)
cla % clear the axes
% fix the axis limits
xlim([0 length(z_p)*dt]);
ylim([1.2*min(z_p(:,1)) 1.2*max(z_p(:,1))]);
centers = [m*dt z_p(m,1)]; % circule center
radius = 0.2; % circule radius
viscircles(centers,radius,'Color','b','LineWidth',2);
hold on; p1 = plot((1:m)*dt,z_p(1:m,1));
grid on
drawnow
% capture the plot as an image
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% write to the GIF File
if m == 1
imwrite(imind,cm,filename,'gif','Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
  1 个评论
Baran Uslu
Baran Uslu 2023-8-18
Dear Ahmad,
Can the problem be with the axes settings, because when a circle becomes an ellipse we must think about an aspect problem. Hope you already fixed it.

请先登录,再进行评论。

回答(1 个)

Suraj Kumar
Suraj Kumar 2024-5-23
Hi AHMAD,
To achieve a moving circle filed with colour in non-square limits for x and y axes you can go through the following steps along with the code snippets:
1. You can use the command axis equal after setting the axes limits to ensure the aspect ratio is maintained.
xlim([0 length(z_p)*dt]);
ylim([1.2*min(z_p(:,1)) 1.2*max(z_p(:,1))]);
axis equal;
2. The “viscircles” function does not natively support filling of the circle. To create a filled circle, you can use the “rectangle” function with the “Curvature” property set to [1,1], which transforms the rectangle into a circle.
3. The “FaceColor” and “EdgeColor” properties can also be used to fill the interior part and set the outline, effectively creating a filled circle.
rectangle('Position',[centers(1)-radius, centers(2)-radius, 2*radius, 2*radius],...
'Curvature',[1,1], 'FaceColor','b', 'EdgeColor','b');
hold on;
p1 = plot((1:m)*dt,z_p(1:m,1));
grid on
drawnow
You can refer the following image for better understanding:
For more insights on the axis in MATLAB and “rectangle” function you can go through the following documentations:
  1. https://www.mathworks.com/help/matlab/ref/axis.html
  2. https://www.mathworks.com/help/matlab/ref/rectangle.html

Community Treasure Hunt

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

Start Hunting!

Translated by