xxxxxxxxxx
var boolDoRefresh;
var numsticks;
var border;
var sticklength;
function setup() {
createCanvas(600, 600);
boolDoRefresh = true;
border = 40;
numsticks = 35;
sticklength = 250;
}
function draw() {
if (boolDoRefresh) {
background(255, 243, 212);
var sticks = []
for(var i=0; i<numsticks; i++) {
var stick = []
stick = generateLine(border,border, width-border, height-border, sticklength);
append(sticks, stick);
}
for(var j=0; j<sticks.length; j++) {
for(var k=j+1; k<sticks.length;k++) {
var intersect = true;
var s1 = sticks[j];
var s2 = sticks[k];
var m1 = (s1[3]-s1[1]) / (s1[2]-s1[0]);
var m2 = (s2[3]-s2[1]) / (s2[2]-s2[0]);
var x = (s1[0]*m1 - s2[0]*m2 + s2[1] - s1[1]) / (m1-m2);
var y = m1*(x-s1[0]) + s1[1];
if(s1[0] < s1[2]) {
if(x<s1[0] || x>s1[2]) intersect=false;
} else {
if(x>s1[0] || x<s1[2]) intersect=false;
}
if(s2[0] < s2[2]) {
if(x<s2[0] || x>s2[2]) intersect=false;
} else {
if(x>s2[0] || x<s2[2]) intersect=false;
}
if(intersect) {
strokeWeight(0);
fill(237, 215, 204);
circle(x,y, 12);
}
}
}
for(var j=0; j<sticks.length; j++) {
strokeWeight(2);
var colors = [[255, 153, 153],
[237, 198, 130],
[131, 212, 139],
[148, 180, 255]]
stroke(colors[floor(random()*4)]);
line(sticks[j][0], sticks[j][1], sticks[j][2], sticks[j][3]);
}
boolDoRefresh = false;
}
}
function mousePressed() {
boolDoRefresh = true;
}
function generateLine(minX,minY, maxX,maxY, length) {
var x1, y1, x2, y2;
do {
x1 = minX + random()*(maxX-minX);
y1 = minY + random()*(maxY-minY);
var angle = random()*2*PI;
x2 = x1 + length*sin(angle);
y2 = y1 + length*cos(angle);
} while(x2>maxX || y2>maxY || x2<minX || y2<minY);
return [x1,y1,x2,y2];
}