Random walks on a grid, Javascript

Some quick Javascript which plots random walks from a point…

View it here.

<html>
<head>

<script type="text/javascript">

  var ittr=8;
  var delta=-0.005;

  function draw_frame() {
    var canvas = document.getElementById("m_canvas");
    if(canvas.getContext) {
      var ctx = canvas.getContext("2d");
      ctx.lineWidth  = 0;
      ctx.fillStyle = "rgb(0,0,0)";


      var x=250;
      var y=250;
      for(var n=0;n<200;n++) {
        var r=Math.floor(Math.random()*4);
        if(r==0) { x+=1; y+=0; }
        if(r==1) { x+=0; y+=1; }
        if(r==2) { x-=1; y+=0; }
        if(r==3) { x-=0; y-=1; }
        ctx.fillRect(x*1,y*1,1,1);
      }

      ctx.fillStyle = "rgb(0,200,200)";
      ctx.fillRect(250,250,3,3);
    }
  }

</script>

</head>

<body  onload="setInterval(draw_frame,10);" >
<canvas id="m_canvas" width="500" height="500">You need a Javascipt</canvas>

</body>

</html>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>