Spoon-Intersections

Intersections with 12 lines.

 

Intersections with 100 lines

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 var boolDoRefresh;
 const NUM_LINES = 12;
 
 function setup() {
   createCanvas(720, 480);
   background('#FFFFFF');
   strokeWeight(5);
   boolDoRefresh = true;
 }
 
 function draw() {
   if(boolDoRefresh) {
     background('#FFFFFF');
     var lines = constructLines();
     findIntersections(lines);
     boolDoRefresh = false;
   }
 }
 
 function mousePressed() {
    boolDoRefresh = true;
 }
 
 function constructLines() {
   const LINE_LENGTH = 250;
   var lines = [];
   for(var i = 0; i < NUM_LINES; i++) {
     var x1 = random(720);
     var y1 = random(480);
     var x2 = random(100) > 50 ? x1 - random(LINE_LENGTH) : x1 + random(LINE_LENGTH);
     var y2 = ((random(100) > 50 ? 1 : -1) * sqrt(sq(LINE_LENGTH) - sq(x1 - x2))) + y1;
 
     line(x1, y1, x2, y2);
     lines[i] = [x1, y1, x2, y2];
   }
   return lines;
 }
 
 function findIntersections(lines) {
   //uses calculations found at http://paulbourke.net/geometry/pointlineplane/
   //draws circles around any points where lines intersect
   for(var i = 0; i < NUM_LINES; i++) {
     for(var j = i + 1; j < NUM_LINES; j++) {
       var x1 = lines[i][0];
       var x2 = lines[i][2];
       var y1 = lines[i][1];
       var y2 = lines[i][3];
 
       var x3 = lines[j][0];
       var x4 = lines[j][2];
       var y3 = lines[j][1];
       var y4 = lines[j][3];
 
       var u1 = (((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3)))
            / (((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1)));
       var u2 = (((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3)))
            / (((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1)));
 
       if(u1 > 0 && u1 < 1 && u2 > 0 && u2 < 1) {
         var x = x1 + (u1 * (x2 - x1));
         var y = y1 + (u1 * (y2 - y1));
         fill('#FFC600');
         ellipse(x, y, 15);
       }
     }
   }
 }