ookey-Intersections
embedded app:
gif:
code:
var linecount = 12; var linelen = 200; var linearray = []; var circleRad = 15; var bckgrnd = '#fbd2d7'; function setup() { createCanvas(720, 480); } function draw() { background(bckgrnd); drawlines(linearray); drawintersect(linearray); } function drawlines(a) { strokeWeight(2); stroke('#000000'); for (i = 0; i < a.length; i++) { //each element should be an array of four integers x1 = a[i][0]; y1 = a[i][1]; x2 = a[i][2]; y2 = a[i][3]; line(x1,y1,x2,y2); } } function drawintersect(a) { fill(color(50, 55, 100, 90)); noStroke(); intersects = calcintersect(a); for (i = 0; i < intersects.length; i++) { ellipse(intersects[i][0], intersects[i][1], circleRad, circleRad); } } //using equations from Paul Bourke function calcintersect(a) { inter = []; for (i = 0; i < a.length; i++) { [x1, y1, x2, y2] = a[i]; for (j = i + 1; j < a.length; j++) { [x3, y3, x4, y4] = a[j]; ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)); ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)); if ((0 < ua) && (ua < 1) && (0 < ub) && (ub < 1)) { x = x1 + ua * (x2 - x1); y = y1 + ua * (y2 - y1); inter.push([x,y]); } } } return inter; } function mouseClicked() { linearray = []; for (i = 0; i < linecount; i++) { x1 = Math.floor(Math.random() * width); y1 = Math.floor(Math.random() * height); x2 = (Math.floor(Math.random() * (2 * linelen)) - linelen) + x1; y2 = Math.floor(Math.pow((Math.pow(linelen,2) - Math.pow(x2 - x1, 2)), 0.5)) + y1; linearray.push([x1, y1, x2, y2]); } } |