Sepho-Intersections

//// Based on the Starter Code for "Embedded Iteration + Randomness"
var boolDoRefresh;
 
var lines = [];
var lineNumber = 12;  //<<-----change this number to add/remove lines!
 
function setup() {
  createCanvas(720, 480);
  background(20,20,50)
  noStroke();
  boolDoRefresh = true;
}
 
// Math for the intersection of two lines from Paul Bourke http://paulbourke.net/geometry/pointlineplane/
//takes two lines and returns the x,y value for the inersection of those lines
function intersec(a,b){
  var x1 = a[0];
  var y1 = a[1];
  var x2 = a[2];
  var y2 = a[3];
 
  var x3 = b[0];
  var y3 = b[1];
  var x4 = b[2];
  var y4 = b[3];
 
  var ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4-y3) * (x2-x1) - (x4-x3) * (y2-y1));
  var ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4-y3) * (x2-x1) - (x4-x3) * (y2-y1));
  var x = (x1 + ua * (x2 - x1));
  var y = (y1 + ua * (y2 - y1));
 
  if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
			return [-20,-20];
	} 
 
  return[x,y];
}
 
//moves the lines according to each points velocity.
function updateLines(lines){
  for(var i = 0; i < lines.length; i++){
    var l = lines[i];
    l[0] += l[4];
    l[1] += l[5];
    l[2] += l[6];
    l[3] += l[7];
  }
}
 
 
function draw() {
  	updateLines(lines);
 
    //Draws all of the lines:
    background(20,20,50)
    stroke(255, 50);
 		fill(255);
    for(var i = 0; i < lines.length; i++){
  		line(lines[i][0],lines[i][1],lines[i][2],lines[i][3]);
    }
 
    //Draws a red dot at all of the intersections:
    for(var i = 0; i < lines.length; i++){
      for(var j = 0; j < lines.length; j++){
        if(i != j){
          la = lines[i];
          lb = lines[j];
          noStroke()
          fill(255,0,0);
          ellipse(intersec(la,lb)[0],intersec(la,lb)[1],3);
        }
      }
    }
 
  if (boolDoRefresh) {
    boolDoRefresh = false;
    //Creates new lines:
    lines = [];
    for(var i = 0; i < lineNumber; i++){
    	lines.push([random(width),random(height),random(width),random(height),random(-1,1), random(-1,1),random(-1,1),random(-1,1)])
  	}  
  }
}
 
function mousePressed() {
  boolDoRefresh = true;
}