jhylands

Simple flow program

Jan 14th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <head>
  5. <link rel="stylesheet" type="text/css" href="http://calander.site90.net/Traffic.css">
  6. <script type="text/javascript" src="http://calander.site90.net/createline.js" ></script>
  7. <script type="text/javascript">
  8. //define global variables
  9. var Lroads = 0; //count the number of roads for the line genorator
  10. //define nodes
  11. var nodes = new Array(); var nox = new Array(); var noy = new Array();
  12. //define roads
  13. var Aroad = new Array(); //start
  14. var Broad = new Array(); //finish
  15. var Croad = new Array(); //resistance
  16. //End of definition of global variables
  17. function main(){
  18.       //initiate nodes
  19.       nodes[0] = 5; nox[0] = 100; noy[0] = 100;
  20.       nodes[1] = 2; nox[1] = 100; noy[1] = 200;
  21.       for (i=2;i<7;i++){
  22.            nodes[i] = 10; nox[i] = 100 * i; noy[i] = 200;
  23.       }
  24.       nodes[7] = 1000; nox[7] = 600; noy[7] = 100;
  25.       drawnodes();
  26.       //initiate roads
  27.       //road 1
  28.       Aroad[0] = 0; //node 1
  29.       Broad[0] = 1; //node 2
  30.       Croad[0] = 2; //inverse o flow
  31.       init(0);
  32.       for (i=1;i<7;i++){
  33.           //road i
  34.           Aroad[i] = i; //node 1
  35.           Broad[i] = i+1; //node 2
  36.           Croad[i] = 1; //inverse to flow
  37.           init(i);
  38.      }
  39. }//end of main
  40. function init(road)
  41. {
  42.  
  43.       Lroads++;
  44.       glue = '<div id="' + Lroads + '"></div>';
  45.       writting(glue);
  46.       var demoElement = document.getElementById(Lroads);
  47.       demoElement.appendChild(createLine(nox[Aroad[road]], noy[Aroad[road]], nox[Broad[road]], noy[Broad[road]]));
  48. }
  49. //a place that HTML can be written to without wiping what has come before
  50. function writting(text){
  51.       document.getElementById('edspace').innerHTML = document.getElementById('edspace').innerHTML + text;
  52. }
  53. function drawnodes(){
  54.      for (i=0;i<nodes.length;i++){
  55.          glue="";
  56.          glue = glue + ('<div style="Position:absolute;left:' + nox[i] + 'px;top:' + noy[i] + 'px;" id="node:' + i + '">' + nodes[i] + '</div>');
  57.          writting(glue);
  58.      }    
  59. }
  60. function run(){
  61.    nodes[7] = nodes[7] * 2;
  62.    var flow = 0;
  63.    for (var i=0;i<Aroad.length;i++){
  64.       flow = Math.round((nodes[Aroad[i]] - nodes[Broad[i]])/Croad[i]);
  65.       nodes[Aroad[i]] = nodes[Aroad[i]] - flow;
  66.       nodes[Broad[i]] = nodes[Broad[i]] + flow;
  67.    }
  68.      
  69.       nodes[0] = Math.round(nodes[0]/4);
  70.    editnodes();
  71.    setTimeout('run();', 1000);
  72.    
  73. }
  74. function editnodes(){
  75.      for (i=0;i<nodes.length;i++){
  76.          document.getElementById('node:' + i).innerHTML = nodes[i];
  77.      }
  78. }
  79. </script>
  80. </head>
  81. <body onload="main();">
  82.  
  83. <h1>Flowing</h1>
  84. <p>V=IR dQ/dt = I so V=R.dQ/dt</p><p> <a href=”http://www.vwi.tu-dresden.de/~treiber/MicroApplet/IDM.html”>Speed</a> <a href=”http://www.vwi.tu-dresden.de/~treiber/MicroApplet/MOBIL.html”> Lane changing</a></p>
  85. <input type="button" value="run" onclick="run();" />
  86.  
  87. <x id="edspace"></x>
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment