## Sepho-IterationExercise

```// Starter Code for "Embedded Iteration + Randomness"
var boolDoRefresh;

function setup() {
createCanvas(400, 400);
background(20,20,50)
noStroke();
boolDoRefresh = true;
}

function draw() {
if (boolDoRefresh) {
// DRAW STUFF HERE....
boolDoRefresh = false;
background(20,20,50)
for (var x = 35; x <= width - 30; x += 30) {
for (var y = 35; y <= height - 30; y += 30) { var rando = random(5); if(rando > 1) {
fill(255,250,150);
rect(x-13, y-13, 26, 26);
fill(20,20,50);
rect(x-10, y-10, 20, 20);
fill(255,250,150);
rect(x-5, y-5, 10, 10);
}
else {
fill(20,20,50);
rect(x-13, y-13, 26, 26);
fill(255,250,150);
rect(x-10, y-10, 20, 20);
fill(20,20,50);
rect(x-5, y-5, 10, 10);
}
}
}
}
}

function mousePressed() {
boolDoRefresh = true;
}
```

## airsun-IterationExercise

```//Clair(sijing) Sun
//sijings@andrew.cmu.edu
//Assignment-5

var lineP=[];
var boolDoRefresh;
var numberofL=10;

function setup() {
createCanvas(480, 480);
boolDoRefresh = false;
for (var i=0; i<numberofL; i++){
var x1 = random(0,width);
var x2 = random(0,width);
var y1 = random(0,height);
var y2 = random(9,height);
lineP[i] = [x1,y1,x2,y2];
}
}

function draw() {
background(200);

//regenerate if mousePressed
if (boolDoRefresh) {
for (var i=0; i<numberofL; i++){
var x1 = random(0,width);
var x2 = random(0,width);
var y1 = random(0,height);
var y2 = random(9,height);
lineP[i] = [x1,y1,x2,y2];
}
boolDoRefresh=false
}

//for drawing the lines
for (var i = 0; i < numberofL; i += 1){
line(lineP[i][0],lineP[i][1],lineP[i][2],lineP[i][3]);
}

//for drawing the intersections, spliting to two lines each time
for (var j = 0; j < numberofL; j += 1){
for (var i = 0; i < numberofL; i += 1){
var x1 = lineP[j][0];
var y1 = lineP[j][1];
var x2 = lineP[j][2];
var y2 = lineP[j][3];
var x3 = lineP[i][0];
var y3 = lineP[i][1];
var x4 = lineP[i][2];
var y4 = lineP[i][3];
intersect(x1, y1, x2, y2, x3, y3, x4, y4);
}
}

}

// Modified from line intercept math by Paul Bourke http://paulbourke.net/geometry/pointlineplane/
// Determine the intersection point of two line segments
// Modified from http://paulbourke.net/geometry/pointlineplane/javascript.txt
function intersect(x1, y1, x2, y2, x3, y3, x4, y4) {
if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) {
return false
}

var denom = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))

if (denom === 0) {
return false
}

var ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom
var ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom

// is the intersection along the segments
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
return false
}

fill(0);
ellipse (x1 + ua * (x2 - x1),y1 + ua * (y2 - y1),15);
}

function mousePressed() {
boolDoRefresh = true;
}
```

## chaine-IterationExercise

```var boolDoRefresh; var size = 40;   function setup(){ createCanvas(400,400); boolDoRefresh = true; }   function draw(){ if (boolDoRefresh){ for (var i = 40; i < height-40; i+=40){ for (var j = 40; j < width-40; j+=40){ if (random(1) < 0.1){ stroke('#fae'); strokeWeight(2); ellipse(i+18,j+18,30,30); stroke('rgba(0,255,0,0.25)'); ellipse(i+18,j+18,15,15); } else{ stroke(color(102, 178, 255)); strokeWeight(1); rect(i,j,35,35); stroke(color(0, 204, 204)); rect(i+7.5,j+7.5,20,20); } } } } boolDoRefresh = false; }   function mousePressed(){ boolDoRefresh = true; }```

## sapeck-IterationExercise

```/* Sapeck 9/6/2017 "sapeck-IterationExercise" 60-212 Carnegie Mellon University Copyright (C) 2018-present Sapeck This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */   // Based on starter Code for "Embedded Iteration + Randomness" var boolDoRefresh;   function setup() { createCanvas(400, 400) boolDoRefresh = true }   function draw() { if (boolDoRefresh) { background(255);   var rows = width/50 var cols = width/50   for (var i=0;i<rows;i++) { for (var j=0;j<cols;j++) { if (random(50) < 5) { fill(color(255,0,0)) ellipse((i*50) + (45/2) + 2.5, (j*50) + (45/2) + 2.5, 45, 45) } else { fill(color(0,255,0)) rect(i*50 + 2.5, j*50 + 2.5, 45, 45) } } }   boolDoRefresh = false } }   function mousePressed() { boolDoRefresh = true }```

## nannon-IterationExercise

```var boolDoRefresh;   function setup() { createCanvas(400, 400); background(240,240,255); boolDoRefresh = true; }   function rando(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; }   console.log(rando(0,100))       function draw() { if (boolDoRefresh) { //margins = 16px //box size= 32 //spacer=16 setup() var margin =16 var spacer=16 var boxsize=32 for (i=0; i<8; i++) { for (j=0;j<8;j++) { var randomint=rando(0,100) var randopercent = rando(5,12) var x=margin+j*(spacer+boxsize) var y=margin+i*(spacer+boxsize) if (randomint<randopercent) { ellipse(x+16, y+16, 28,28) }   else { rect(x, y, boxsize, boxsize) } } }   boolDoRefresh = false; } }   function mousePressed() { boolDoRefresh = true; }```

## breep-IterationExercise

```// Starter Code for "Embedded Iteration + Randomness" used from Woodpress site var boolDoRefresh; function setup() { createCanvas(400, 400); background(255, 128, 0); noStroke(); boolDoRefresh = true; } function draw() { var squareSpacing = 40 if (boolDoRefresh) { clear(); background(255, 128, 0);   for (var x = 5; x &lt;= width - 10; x += squareSpacing) {   for (var y = 5; y &lt;= height - 10; y += squareSpacing) { var shape = random(0, 100); fill(0, 111, 255);   if (shape &gt; 95) { fill(0, 255, 31); ellipse(x + 15, y + 15, 30, 30); }   else { rect(x, y, 30, 30); } } } boolDoRefresh = false; } }   function mousePressed() { boolDoRefresh = true; }```

## shuann-IterationExercise

GIF:

```// Starter Code for "Embedded Iteration + Randomness" var boolDoRefresh; var gridSize = 45; var offset = 5;   function setup() { createCanvas(400, 400); boolDoRefresh = true; }   function draw() { if (boolDoRefresh) { // DRAW STUFF HERE.... background(240); for (var x = offset*4; x &lt;= width - gridSize; x += gridSize){ for (var y = offset*4; y &lt;= height - gridSize; y += gridSize) { stroke("pink"); fill("white"); var rand = random(); if (rand &lt;= 0.05){ ellipse(x + (gridSize-offset)/2, y + (gridSize-offset)/2, gridSize/2); } else { rect(x, y, gridSize-offset, gridSize-offset); } } } boolDoRefresh = false; } }   function mousePressed() { boolDoRefresh = true; }```

## yuvian-IterationExercise

```var canvasWidth = 500;
var canvasHeight = canvasWidth;
var sideLength = canvasWidth/8 - 10;

function setup() {
var bg_color = color(17, 22, 158);
background(bg_color);
createCanvas(canvasWidth,canvasHeight);
noStroke();

for (var x = 0; x < 8; x++) {
for (var y = 0; y < 8; y++) {
fill(random(50,170),random(130,200), random(230,255));
rect(x*canvasWidth/8,y*canvasWidth/8,sideLength,sideLength);
}
}
}

function draw() {
var bg_color = color(17, 22, 158);
rectMode(CORNER);
ellipseMode(CORNER);

for (var x = 0; x < 8; x++) {
for (var y = 0; y < 8; y++) {
var chance = random(0,1);
if (mouseIsPressed) {
if (chance < 0.05 ) {
noStroke();
fill(bg_color);
rect(x*canvasWidth/8, y*canvasWidth/8, canvasWidth/8, canvasWidth/8);
//circles are bright red
fill(255, 43, 92);
ellipse(x*canvasWidth/8+3,y*canvasWidth/8+3,sideLength-3, sideLength-3);
} else {
noStroke();
//squares lean blue
fill(random(50,170),random(130,200), random(230,255));
rect(x*canvasWidth/8,y*canvasWidth/8,sideLength,sideLength);
}
}
}
}
}
```

## nerual-IterationExercise

```var boolDoRefresh; var s = 20; var n = 9;   function setup() { createCanvas(400, 400); boolDoRefresh = true; }   function draw() { if (boolDoRefresh) { //background(0); var space = width / (n + 2); for (var x = space; x &lt; width - space; x += space){ for (var y = space; y &lt; width - space; y += space){ rect(x, y, s, s); r = random(101) if (r &lt;= 5){ rect(x+5, y+5, s-10, s-10); } } } boolDoRefresh = false; } }   function mousePressed() { background(255); boolDoRefresh = true; }```

## paukparl-IterationExercise

sketch:

gif:

```var justClicked; var gridSize;   function setup() { createCanvas(400, 400); justClicked = true; gridSize = width/10; fill(50); stroke(255); strokeWeight(3); }   function draw() { if (justClicked) {   // redraw background(100, 100, 108); for (var y = gridSize; y &lt; width; y += gridSize) { for (var x = gridSize; x &lt; height; x += gridSize) { var rand = random(); if (rand &lt; 0.05) { ellipse(x, y, 10); } else { ellipse(x, y, 20); } } }   justClicked = false; } }   function mousePressed() { justClicked = true; }```