How to create a moving filled-color circle in non-square limits for x and y axes?
4 views (last 30 days)
Show older comments
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 Comment
Baran Uslu
on 18 Aug 2023
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.
Answers (1)
Suraj Kumar
on 23 May 2024
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:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!