
function Line(x1,y1,x2,y2,gen) {
  this.x1 = x1 ;
  this.y1 = y1 ;
  this.x2 = x2 ;
  this.y2 = y2 ;
  this.gen = gen;
  this.sx = undefined;
  this.sy = undefined;
  this.linec  = 'rgba(0,0,0,0.9)';
  this.shadec = 'rgba(0,0,0,0.1)';
  this.spawn = function(){
    var n = [this.x2-this.x1,this.y2-this.y1];
    var len = Math.sqrt(n[0]*n[0]+n[1]*n[1]);
    if (this.gen>7 || len<2){
      return [];
    }
    n = [n[0]/len,n[1]/len];
    var slen =     (0.2+0.6*Math.random())*len;
    var dist = 0.5*(0.3    -Math.random())*len;
    //var dist =     (0.3    -Math.random())*slen;
    this.sx = this.x1+slen*n[0];
    this.sy = this.y1+slen*n[1];
    this.sx += dist*n[1];
    this.sy -= dist*n[0];
    return [new Line(this.x1,this.y1,
                     this.sx,this.sy,this.gen+1),
            new Line(this.sx,this.sy,
                     this.x2,this.y2,this.gen+1)];
  }
  this.lined = function(){
    CTX.strokeStyle = this.linec;
    CTX.lineWidth = 0.5;
    CTX.beginPath();
    CTX.moveTo(this.x1,this.y1); 
    CTX.lineTo(this.x2,this.y2);
    CTX.stroke();
  }
  this.shaded = function(){
    CTX.fillStyle = this.shadec;
    CTX.beginPath();
    CTX.moveTo(this.x1,this.y1); 
    CTX.lineTo(this.sx,this.sy);
    CTX.lineTo(this.x2,this.y2);
    CTX.closePath();
    CTX.fill();
  }
  this.dotsd = function(){
    var n = [this.x2-this.x1,this.y2-this.y1];
    var len = Math.sqrt(n[0]*n[0]+n[1]*n[1]);
    var scale = 0.07*len;
    n = [n[0]/len,n[1]/len];
    CTX.beginPath();
    CTX.fillStyle = 'rgba(0,0,0,0.5)';
    CTX.strokeStyle = 'rgba(200,0,0,0.7)';
    CTX.arc(this.sx+n[1]*scale,
            this.sy-n[0]*scale,1.5,0,2*Math.PI,true);
    CTX.arc(this.sx-n[1]*scale,
            this.sy+n[0]*scale,1.5,0,2*Math.PI,true); 
    CTX.closePath();
    CTX.fill();
    CTX.beginPath();
    //CTX.lineWidth = 0.5 ;
    CTX.moveTo(this.sx+n[1]*scale,this.sy-n[0]*scale); 
    CTX.lineTo(this.sx-n[1]*scale,this.sy+n[0]*scale);
    CTX.closePath();
    CTX.stroke();
  }
}

// functions

function drawlines(a){
  for (var i=0;i<a.length;i++){
    a[i].lined(); 
  }
}

function step(){
  var x=X.shift();

  if (x){
    var res=x.spawn();
    if (x.gen>0){
      x.shaded();
      x.lined();
    }
    if (x.gen<3){
      x.dotsd();
    }
    if (res.length>0){
      X.push.apply(X,res); 
    }
  }
  if (X.length>0){
    ON=setTimeout(step,TIC);
  }
}


function run(){
  CTX.fillStyle = "rgb(255,255,255)";
  CTX.fillRect(0,0,N,M);
  
  X.push(new Line(300,30,300,470,0));
  X.push(new Line(300,470,300,30,0));
  
  X.push(new Line(700,30,700,470,0));
  X.push(new Line(700,470,700,30,0));
  
  //X.push(new Line(750,20,750,480,0));
  //X.push(new Line(750,480,750,20,0));
  
  //X.push(new Line(500,20,500,480,0));
  //X.push(new Line(500,480,500,20,0));
  
  //X.push(new Line(250,20,250,480,0));
  //X.push(new Line(250,480,250,20,0));
  
  step();
}

/*
function colorString(){
  var n = 50 ;
  return 'rgba('+Math.floor(Math.random()*n)+','+
                 Math.floor(Math.random()*n)+','+
                 Math.floor(Math.random()*n)+
               ',0.2)';
}
*/
function wtf() {alert(ponies);} // essential function


