-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayTraffic.m
executable file
·41 lines (37 loc) · 1.13 KB
/
displayTraffic.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
%% function displayTraffic animates cars on a ring road
% u - car positions and velocities
% L - length of the ring road
function displayTraffic(u, L)
[numVars,N] = size(u);
carPositions = u(1:numVars/2, :);
carPositions = rem(carPositions, L);
stepSize = 5;
close all; clc
figure;
% specify the plotting colors
rbg = zeros(numVars/2,3);
dx = 0.8;
for i=1:numVars/2
f = i*2/numVars;
g = (6-2*dx)*f+dx;
rgb(i,1) = max(0,(3-abs(g-4)-abs(g-5))/2);
rgb(i,2) = max(0,(4-abs(g-2)-abs(g-4))/2);
rgb(i,3) = max(0,(3-abs(g-1)-abs(g-2))/2);
end
% Preallocate movie structure.
mov(1:floor(N/stepSize)) = struct('cdata', [],'colormap', []);
%set(gca,'nextplot','replacechildren');
% plot the points
for i = 1:stepSize:N
angle = carPositions(:, i)*(2*pi)/L;
x = cos(angle);
y = sin(angle);
clf;
scatter(x,y,[],rgb);
shg;
pause(.01)
mov(i) = getframe(gcf);
end
% create avi file
movie2avi(mov, 'traffic.avi', 'compression', 'None');
end