var refresh; var rows = 9; var cols = 9; var spacing = 55; var xColor; var dudeColor; function setup() { createCanvas(550, 550); refresh = true; colorMode(HSB, 100); } function draw() { if (refresh) { background(0, 0, 100); var myHue = Math.random() * 100; dudeColor = color(myHue, 30, 95); xColor = color((myHue + 50) % 100, 40, 95); fill(dudeColor); stroke(100, 100, 30); for(var i = 1; i <=rows; i++) for(var j = 1; j <= cols; j++){ if(Math.random() > .1) drawDude(i, j); else drawX(i, j); } refresh = false; } } function drawDude(x, y){ stroke(0, 0, 100); strokeWeight(0); ellipse(x * spacing, y * spacing, 40, 40); ellipse(x * spacing - 4, y * spacing - 20, 10, 20); ellipse(x * spacing + 9, y * spacing - 18, 10, 20); strokeWeight(3); ellipse(x * spacing - 10, y * spacing - 4, 2, 2); ellipse(x * spacing + 10, y * spacing - 2, 2, 2); strokeWeight(2); ellipse(x * spacing, y * spacing + 1, 1, 6); ellipse(x * spacing, y * spacing + 8, 15, 1); } function drawX(x, y){ stroke(xColor); strokeWeight(4); line(x * spacing + 5, y * spacing + 5, x * spacing - 5, y * spacing - 5); line(x * spacing - 5, y * spacing + 5, x * spacing + 5, y * spacing - 5); } function mousePressed() { refresh = true; } |
Category: 01-IterationExercise
casher-IterationExercise
var boolDoRefresh; function setup() { createCanvas(400, 400); background('white'); boolDoRefresh = true; } function draw() { if (boolDoRefresh) { background('white'); for (var x = 0; x <= width; x += 50) { for (var y = 0; y <= width; y+= 50) { var maybeACircle = int(random(0,10)); strokeWeight(1.5); if (maybeACircle == 0) { fill('blue'); ellipse(x+23, y+23, 45, 45); } else { fill('white'); rect(x, y, 45, 45); } } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } |
ocannoli-IterationExercise
var boolDoRefresh; function setup() { createCanvas(400, 400); boolDoRefresh = true; } function draw() { if (boolDoRefresh) { background(255,255,255); stroke(51); strokeWeight(4); var gridSize=8; for(var i=0, x=10;i<=gridSize;i++, x+=50){ for(var k=0,y=10;k<=gridSize;k++,y+=50) { var r=random(3); if(r<0.3){ ellipse(x+10,y+10,20,20); } else{ rect(x,y,20,20); } } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } |
Spoon-IterationExcercise
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 | // using starter code for "Embedded Iteration + Randomness" var boolDoRefresh; function setup() { createCanvas(625, 625); strokeWeight(5); boolDoRefresh = true; } function draw() { if(boolDoRefresh) { for(var x = 50; x < 625; x += 75) { for(var y = 50; y < 625; y += 75) { drawItemAtPosition(x, y); } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } function drawItemAtPosition(x, y) { //draws an item with a center on the position given. var drawYellow = random(255) > 235; if(drawYellow) fill('#FFC600'); else fill('#FFFFFF'); rect(x - 25, y - 25, 50, 50); } |
harsh-IterationExercise
var boolDoRefresh; function setup() { createCanvas(400, 400); background(255); boolDoRefresh = true; } function draw() { var border = 4; var numRects = 20; var gridWidth = width/numRects; var gridHeight = height/numRects; var numCircles = numRects; var rectGrid = []; var randomNums =[]; if (boolDoRefresh) { background(255, 153, 153); for(var k=0; k<numCircles; k++){ var newRandomNumX = Math.round(random(0,numRects)); var newRandomNumY = Math.round(random(0,numRects)); randomNums.push([newRandomNumX,newRandomNumY]); } for(var i=0; i<numRects; i++){ for(var j=0; j<numRects; j++){ var rowCol = [i,j]; var isCircle = false; for(var k=0; k<randomNums.length-1;k++){ var curRandom = randomNums[k]; if(curRandom[0] == rowCol[0] && curRandom[1] == rowCol[1]){ isCircle = true; } } if(isCircle == false){ push(); noStroke(); fill(255, 255, 153); rect(i*gridWidth+border,j*gridHeight+border,gridWidth-border*2,gridHeight-border*2); pop(); } else{ push(); noStroke(); fill(0, 102, 153); ellipse(i*gridWidth+gridWidth/2,j*gridHeight+gridWidth/2,gridWidth/2,gridHeight/2); pop(); } } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } |
dinkolas-IterationExercise
var s = 15; //spacing var d = 25; //diameter var front; //colors var back; var shadow; var drawBool; function setup() { createCanvas(10 * d + 11 * s, 10 * d + 11 * s); front = color(245, 230, 240); back = color(200, 150, 160); shadow = color(150, 120, 140); drawBool = true; } function drawCircle(x, y) { noStroke() fill(shadow); ellipse(x, y, d, d); fill(back); ellipse(x - 5, y + 6, d, d); noFill() stroke(front) strokeWeight(10) ellipse(x, y, d + 10, d + 10); } function drawSquare(x, y) { noStroke() fill(shadow); rect(x, y, d, d); fill(back); rect(x - 9, y + 10, d, d); noFill() stroke(front) strokeWeight(16) rect(x - 3, y - 3, d + 6, d + 6); } function mousePressed() { drawBool = true; } function draw() { if (drawBool) { background(front); for (var i = 0; i < 10; i = i + 1) { for (var j = 0; j < 10; j = j + 1) { if (random(20) >= 1) { drawSquare(s + (d + s) * i, s + (d + s) * j); } else { drawCircle(s + d / 2 + (d + s) * i, s + d / 2 + (d + s) * j) } } } drawBool = false; } } |
rigatoni – Iteration Exercise
We have entered the T H I R D D I M E N S I O N
/*A lot of the code I have written was informed by both the References section of p5 as well Dan Shiffman's Youtube Channel, The Learning Train. The specific tutorials I looked at are listed below: The Learning Channel 18.3 The Learning Channel Coding Challenge #86 */ let redraw = true function setup() { createCanvas(500, 500, WEBGL); camera(500, -500, (height/2.0) / tan(PI*45.0 / 360), 0, 0, 0, 0, 1, 0) setLighting() } function draw() { if(redraw) { background(0); drawGrid() redraw=false } } function mouseClicked() { redraw=true } function drawGrid() { noStroke() translate(-250,0,250) for(x=0; x<10; x++) { translate(50,0,0) for(y=0; y<10; y++) { translate(0,0,-50) pickRandom(drawCube, drawSphere, .8) } //this is like setting a typewriter slide back to start translate(0,0,500) } } //the ratio is how likely pickRandom will choose method1 over method2 function pickRandom(method1, method2, ratio) { //using square roots helped reduce the weird looking "random" patterns if((random(1)*random(1))<(ratio*ratio)) { method1() } else { method2() } } function setLighting() { ambientLight(50,0,0) directionalLight(255,255,255, 25, 0, -25) } function drawCube() { ambientMaterial(255, 0, 0) box(43) } //Spheres also cast a point light around them so it looks better function drawSphere() { ambientMaterial(0, 255, 0) pointLight(255, 255, 255) sphere(20) } |
chewie-IterationExercise
// Starter Code for "Embedded Iteration + Randomness" var boolDoRefresh; var rand; var margin; var side; function setup() { createCanvas(400, 400); boolDoRefresh = true; margin = 10; side = (width-margin*9)/8 } function draw() { if (boolDoRefresh) { background(255); for (var row = 0; row<8; row++) { for (var col = 0; col<8; col++) { drawElem(row, col); } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } function drawElem(row, col) { var left = col*(side+margin)+margin; var top = row*(side+margin) + margin; rand = int(random(20)); if (rand == 0) { left += int(side/2); top += int(side/2); ellipse(left, top, side/2, side/2); } else rect(left, top, side, side); } |
weirdie-IterationExercise
var boolDoRefresh; function setup() { createCanvas(400, 400); background(21, 62, 71); boolDoRefresh = true; } function draw() { if (boolDoRefresh) { background(21, 62, 71); noStroke(); for (var r = 0; r < 8; r++) { for (var c = 0; c < 8; c++) { var wow = int(random(0,12)); if(wow == 0) { fill(28, 229, 242); ellipse(r*46+40, c*46+40, 40, 40); } else { fill(24, 191, 179); rect(r*46+20, c*46+20, 40, 40); } } } boolDoRefresh = false; } } function mousePressed() { boolDoRefresh = true; } |
chromsan-IterationExercise
let refresh = true, box_width = 40, box_padding = 10; function setup() { createCanvas(400, 400); background(255); noStroke(); } function draw() { if (refresh) { refresh = false; clear(); background(255); for (let x = 5; x <= width - box_width; x += (box_width + box_padding)) { for (let y = 5; y <= height - box_width; y += (box_width + box_padding)) { let rand = Math.ceil(random(15)); // 1 in 15 chance there is a circle if (rand == 1) { fill(66, 134, 244); ellipse((x + box_width/2), (y + box_width/2), box_width, box_width); } else { fill(195, 198, 204); rect(x, y, box_width, box_width); } } } } } function mousePressed() { refresh = true; } |