Code covered by the BSD License  

Highlights from
CTMSIM - an interactive freeway traffic macrosimulator

image thumbnail
from CTMSIM - an interactive freeway traffic macrosimulator by Alex Kurzhanskiy
Freeway traffic simulation based on Asymmetric Cell Transmission Model

plot_freeway(celldata)
function plot_freeway(celldata)
% PLOT_FREEWAY - plot freeway configuration using data in cell structure array.
%
% Call:   plot_freeway(celldata)
%
% Parameter:   celldata - array of freeway cell structures.
%
% Returns:   none.
%
% Last modified:   11/17/2006.

%
% Alex Kurzhanskiy   <akurzhan@eecs.berkeley.edu>
%

K = 2;
N = size(celldata, 2);  % number of cells

if N < 1
  return;
end

ih = ishold;

if celldata(1).PMstart <= celldata(N).PMend  % direction: from LEFT to RIGHT
  dr   = 1;
  xlim = [celldata(1).PMstart celldata(N).PMend];
else  % direction: from RIGHT to LEFT
  dr   = -1;
  xlim = [celldata(N).PMend celldata(1).PMstart];
end

ylim = 0;
wdt  = min(get_cell_lengths(celldata))/8;

for i = 1:N
  Xml = [celldata(i).PMstart celldata(i).PMstart celldata(i).PMend celldata(i).PMend celldata(i).PMstart];
  Yml = [(-dr)*ceil(celldata(i).lanes) 0 0 (-dr)*ceil(celldata(i).lanes) (-dr)*ceil(celldata(i).lanes)];
  Yr  = [Yml(1) Yml(1) (Yml(1)-K*dr) (Yml(1)-K*dr) Yml(1)];
  fill(Xml, Yml, 'k');
  hold on;
  plot(Xml, Yml, 'w');

  if abs(Yr(3)) > ylim
    ylim = abs(Yr(3));
  end

  if ~isempty(celldata(i).ORname)  % plot on-ramp
    w = wdt * celldata(i).ORlanes;
    x = celldata(i).PMstart;
    if dr > 0
      Xor = [(x+wdt) (x+wdt+w) (x+w) x (x+wdt)];
    else
      Xor = [(x-wdt) (x-wdt-w) (x-w) x (x-wdt)];
    end
    fill(Xor, Yr, 'b');
    plot(Xor, Yr, 'b');
  end

  if ~isempty(celldata(i).FRname)  % plot off-ramp
    w = wdt * celldata(i).FRlanes;
    x = celldata(i).PMend;
    if dr > 0
      Xfr = [(x-wdt) (x-wdt-w) (x-w) x (x-wdt)];
    else
      Xfr = [(x+wdt) (x+wdt+w) (x+w) x (x+wdt)];
    end
    fill(Xfr, Yr, 'g');
    plot(Xfr, Yr, 'g');
  end
end

if dr < 0
  ylim = [0 ylim];
else
  ylim = -[ylim 0];
end
axis([xlim ylim]);

xlabel('Post Mile');
h = gca;
set(h, 'YTick', []);

if ih == 0
  hold off;
end

return;

Contact us at files@mathworks.com