Making animatedlines that dynamically change linestyle depending on conditions

5 visualizaciones (últimos 30 días)
I have been trying to make a 2D boid simulation with the animatedline command in a 'wrapped universe'. To do so, I used mod(point,xsize) to wrap my birbs if the new position after a timestep brought it outside the 'bounds' of the universe. The issue I have is that when using animatedline, my addpoint command joins the position of the current timestep with the previous one when it gets 'wrapped'.
This results in horizontal and vertical lines on the screen. I do not want these lines and I do not believe each addpoint() can designate the style of each line connecting the points, otherwise I would use linestyle 'none' for those points.
How can I achieve a similar solution to the code below without animatedline's inability to have dynamic linestyle changes? I know MATLAB has toolbox for boid simulations, but I really really wanted one of my own.
%% Make universe and variables
xsize = 100;
ysize = 100;
%zsize = 100;
timesteps = 100000;
stepsize = 0.01;
nbirbs = 50;
h = animatedline;
axis = ([0 xsize 0 ysize]);
%% Make birb tracker with positions and velocity
for i=1:nbirbs
birbs(i).al = animatedline;
birbs(i).xy = zeros(timesteps,2); % Position in [m]
birbs(i).v = zeros(timesteps,2); % Velocity in [m]/[s]
%initial values of positon and velocity
birbs(i).xy(1,:) = xsize*rand(1,2);
temptheta = 2*pi*rand;
birbs(i).v(1,:) = [cos(temptheta) sin(temptheta)];
end
%% Iterate birb tracker and plot as we go
addpoints(birbs(1).al,birbs(1).xy(1,1),birbs(1).xy(1,2)); %Add initial points
for i = 2:timesteps % Find new positions and plot
for bird = 1:nbirbs
birbs(bird).xy(i,:) = mod(birbs(bird).xy(i-1,:) + stepsize*birbs(bird).v(1,:),xsize); %Find new birb position at next timestep
addpoints(birbs(bird).al,birbs(bird).xy(i,1),birbs(bird).xy(i,2)); %Add the point to the animated line for that birb
end
drawnow limitrate % Draw every birb's position after each TIMESTEP
end
drawnow %Idk what this is doing

Respuesta aceptada

Steven Lord
Steven Lord el 24 de Feb. de 2021
h = animatedline('LineStyle', '-');
axis([0 360 -1 1])
for x = 0:360
if mod(x, 30) == 0
addpoints(h, NaN, NaN);
else
addpoints(h, x, sind(x))
end
end
xline(150, 'r')
If you were to zoom in, you'd see the red line at x = 150 is exactly inside the gap created by the NaN value I added to break the line when x was 150.
  1 comentario
Jonathan Ward
Jonathan Ward el 24 de Feb. de 2021
Editada: Jonathan Ward el 24 de Feb. de 2021
Thank you Steven, this was exactly what I was looking for! I had no idea NaN was a thing, have a great evening! I set the condition for a NaN point when the distance between the point i and i-1 was >95. then i plot i-1 with i by pretend i-1 was 'wrapped' to make it look perfectly normal. cheers!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by