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_fwcell(cell)
function plot_fwcell(cell)
% PLOT_FREEWAY - plot freeway cell using data in the cell structure.
%
% Call:   plot_fwcell(cell)
%
% Parameter:   cell - freeway cell structure.
%
% Returns:   none.
%
% Last modified:   11/11/2006.

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

K = 2;
M = 30;
C = [0.5 0.5 0.5];

if isempty(cell)
  return;
end

ih = ishold;

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

Yr   = [0 0 (-K*dr) (-K*dr) 0];
ylim = Yr(3);
wdt  = abs(cell.PMend - cell.PMstart)/M;

if isfield(cell, 'auxlanes')
  nml = round(cell.lanes);
  nal = round(cell.auxlanes);
else
  nml = floor(cell.lanes);
  nal = round((cell.lanes - nml) / 0.3);
end

Xml = [cell.PMstart cell.PMstart cell.PMend cell.PMend cell.PMstart];

% plot auxiliary lanes
if nal > 0
  for i = 1:nal
    Yml = dr * [i (i-1) (i-1) i i];
    fill(Xml, Yml, C);
    hold on;
    plot(Xml, Yml, 'w:');
  end
end

% plot mainline lanes
if nml > 0
  for i = 1:nml
    Yml = dr * [(nal+i) (nal+i-1) (nal+i-1) (nal+i) (nal+i)];
    fill(Xml, Yml, 'k');
    hold on;
    plot(Xml, Yml, 'w:');
  end
end

Yml = dr * [(nal+nml) 0 0 (nal+nml) (nal+nml)];
if dr > 0
  ylim = [ylim Yml(1)];
else
  ylim = [Yml(1) ylim];
end
plot(Xml, Yml, 'k');

% plot on-ramp
if ~isempty(cell.ORname)
  x  = cell.PMstart;
  nl = cell.ORlanes; 
  for i = 1:cell.ORlanes
    if dr > 0
      Xor  = [(x+i*wdt) (x+(i+1)*wdt) (x+i*wdt) (x+(i-1)*wdt) (x+i*wdt)];
      Xort = [(x+wdt) (x+(nl+1)*wdt) (x+nl*wdt) x (x+wdt)];
    else
      Xor  = [(x-i*wdt) (x-(i+1)*wdt) (x-i*wdt) (x-(i-1)*wdt) (x-i*wdt)];
      Xort = [(x-wdt) (x-(nl+1)*wdt) (x-nl*wdt) x (x-wdt)];
    end
    fill(Xor, Yr, 'k');
    plot(Xor, Yr, 'w:');
  end
  plot(Xort, Yr, 'k');
end

% plot off-ramp
if ~isempty(cell.FRname)
  x  = cell.PMend;
  nl = cell.FRlanes; 
  for i = 1:cell.FRlanes
    if dr > 0
      Xfr  = [(x-i*wdt) (x-(i+1)*wdt) (x-i*wdt) (x-(i-1)*wdt) (x-i*wdt)];
      Xfrt = [(x-wdt) (x-(nl+1)*wdt) (x-nl*wdt) x (x-wdt)];
    else
      Xfr  = [(x+i*wdt) (x+(i+1)*wdt) (x+i*wdt) (x+(i-1)*wdt) (x+i*wdt)];
      Xfrt = [(x+wdt) (x+(nl+1)*wdt) (x+nl*wdt) x (x+wdt)];
    end
    fill(Xfr, Yr, 'k');
    plot(Xfr, Yr, 'w:');
  end
  plot(Xfrt, Yr, 'k');
end

axis([xlim ylim]);

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

if ih == 0
  hold off;
end

return;

Contact us at files@mathworks.com