xxxxxxxxxx
89
function setup() {
createCanvas(800, 800);
nSquares = 20;
strokeWeight(0.4)
stroke(255, 255, 255);
noLoop();
}
function distance(x1, y1, x2, y2){
return (y1 - y2)*(y1 - y2) + (x1 - x2)*(x1 - x2)
}
function fillSquare(squareW, startX,startY, box1, box2, percentOptions){
orientation = random([0, 1])
percent = random(percentOptions)
threshold = squareW * 7 * percent;
if(orientation){
point1 = [(box1[0] * squareW) + startX, (box1[1] * squareW) +startY]
point2 = [(box2[0] * squareW) + startX, (box2[1] * squareW) +startY]
spacingX = abs(point1[0] - point2[0]) * percent;
spacingY = abs(point1[1] - point2[1]) * percent;
if(random() < 0.7){
while(distance(point1[0], point1[1], point2[0], point2[1]) > threshold ){
line( point1[0], point1[1], point2[0], point2[1]);
point1 = [point1[0] + spacingX, point1[1]]
point2 = [point2[0], point2[1] - spacingY]
}
}
point1 = [(box1[0] * squareW) + startX, (box1[1] * squareW) +startY]
point2 = [(box2[0] * squareW) + startX, (box2[1] * squareW) +startY]
if(random() < 0.9){
while(distance(point1[0], point1[1], point2[0], point2[1]) > threshold ){
point1 = [point1[0], point1[1] + spacingY]
point2 = [point2[0] - spacingX, point2[1]]
line( point1[0], point1[1], point2[0], point2[1]);
}
}
return;
}
point1 = [(box2[0] * squareW) + startX, (box1[1] * squareW) +startY]
point2 = [(box1[0] * squareW) + startX, (box2[1] * squareW) +startY]
spacingX = abs(point1[0] - point2[0]) * percent;
spacingY = abs(point1[1] - point2[1]) * percent;
while(distance(point1[0], point1[1], point2[0], point2[1]) > threshold ){
line( point1[0], point1[1], point2[0], point2[1]);
point1 = [point1[0] - spacingX, point1[1]]
point2 = [point2[0], point2[1] - spacingY]
}
point1 = [(box2[0] * squareW) + startX, (box1[1] * squareW) +startY]
point2 = [(box1[0] * squareW) + startX, (box2[1] * squareW) +startY]
if(random() < 0.7){
while(distance(point1[0], point1[1], point2[0], point2[1]) > threshold ){
point1 = [point1[0], point1[1] + spacingY]
point2 = [point2[0] + spacingX, point2[1]]
line( point1[0], point1[1], point2[0], point2[1]);
}
}
}
function makeGrid(percentOptions){
squareW = width / nSquares;
for (var r = 0; r < nSquares; r++ ) {
for (var c = 0; c < nSquares; c++) {
box1 = random([[0, 0.0, 0.0], [1, 0.5, 0.0], [2, 0, 0.5]])
if(box1[0] == 0){
box2 = random([[ 1.0, 0.5], [0.5, 1.0]])
}
if(box1[0] == 1 || box1[0] == 2){
box2 = [1.0, 1.0]
}
box1 = [box1[1], box1[2]]
fillSquare(squareW, r * squareW, c * squareW, box1, box2, percentOptions);
}
}
}
function draw() {
makeGrid([0.1, 0.2, 0.5]);
makeGrid([0.1, 0.2, 0.5]);
makeGrid([ 0.2, 0.5]);
save("mySVG.svg"); // give file name
print("saved svg");
noLoop(); // we just want to export once
}