sheep-Intersections


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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//credit to Paul Bourke for his algorithm on point intersection http://paulbourke.net/geometry/pointlineplane/
var state = true;
function setup() {
  createCanvas(600, 600);
 
}
 
function draw() {
 
  if (state === true) {
    var lines = [];
 
    var intersectsX = [];
    var intersectsY = [];
    var resultX = 0;
    var resultY = 0;
    var originx;
    var originy;
    var angle;
    var lineLength = 100;
    var radius = 20;
    var numLines = 12;
    var borders = 100;
    var i = 0;
 
    background(255,255,153);
		noStroke();
    ellipseMode(CENTER);
    while (i < numLines) {
      angle = random(1) * PI * 2;
      originx = random(borders, width - borders);
      originy = random(borders, height - borders);
      append(lines, [round(originx), round(originy), round(originx + (cos(angle) * lineLength)), round(originy + (sin(angle) * lineLength))]);
      i += 1;
 
    }
 
 
    for (var a = 0; a < lines.length; a++) {
      for (var b = a; b < lines.length; b++) {
        resultX = intersectX(lines[a][0], lines[a][1], lines[a][2], lines[a][3], lines[b][0], lines[b][1], lines[b][2], lines[b][3])
        resultY = intersectY(lines[a][0], lines[a][1], lines[a][2], lines[a][3], lines[b][0], lines[b][1], lines[b][2], lines[b][3])
        if (!(resultX < 0 || resultX > 1 || resultY < 0 || resultY > 1)) {
          append(intersectsX, lines[a][0] + resultX * (lines[a][2] - lines[a][0]));
          append(intersectsY, lines[a][1] + resultX * (lines[a][3] - lines[a][1]));
 
 
        }
 
 
      }
 
 
 
    }
 
    for (var j = 0; j < intersectsX.length; j++) {
			noStroke();
      fill(200, 10, 0,40);
      ellipse(intersectsX[j], intersectsY[j], radius, radius);
    }
    for (var a = 0; a < lines.length; a++) {
			stroke(100);
      line(lines[a][0], lines[a][1], lines[a][2], lines[a][3]);
      fill(0);
 
    }
    state = false;
  }
 
}
 
function mouseClicked() {
  state = true;
}
 
function intersectX(x1, y1, x2, y2, x3, y3, x4, y4) {
  return round((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
}
 
function intersectY(x1, y1, x2, y2, x3, y3, x4, y4) {
 
  return round((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
}