farcar – Intersections

Program:

GIF:

Code:

//randomly creates (x,y) and radius parameters for lines and circles
function setup() { 
  createCanvas(720, 480);
	background(color(249, 234, 219));
	var lineX = [];
	var lineY = [];
	var lineR = [];
	for(var i = 0; i < 100; i++) {
		append(lineX, random(width));
		append(lineY, random(height));
		append(lineR, random(2*PI));
		strokeWeight(1);
		lineByAngle(lineX[i],lineY[i],lineR[i],200);
		//checks if any lines intersect and draws circles on their intersection points
		for(var j = 0; j < i; j++) {
			//returns the (x,y) coordinates of two line intersections. Otherwise, returns false
			var hit = collideLineLine(lineX[i],lineY[i],lineX[i]+200*sin(lineR[i]),lineY[i]+200*cos(lineR[i]),lineX[j],lineY[j],lineX[j]+200*sin(lineR[j]),lineY[j]+200*cos(lineR[j]),true)
			if(hit.x != false) {
				fill(color(244, 66, 66, 50));
				strokeWeight(0);
				ellipse(hit.x,hit.y,20);
			}
		}
	}
} 
 
//redraws the canvas whenever clicked
function draw() { 
	if(mouseIsPressed)
		setup();
}
 
//draws line based on an (x,y) origin pair in a radial system
function lineByAngle(x, y, rad, len) {
	line(x,y,x+len*sin(rad),y+len*cos(rad));
}