Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Creative Coding
- * Week 2, 03 - n squares
- * by Indae Hwang and Jon McCormack
- * Updated 2016
- * Copyright (c) 2014-2016 Monash University
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- 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.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- * In the next iteration of the square drawing sketch, this version selects a random number of squares
- * and a random gap between them. From this it calculates the width of each square then draws the squares
- * using two nested for loops.
- *
- * A simple drop shadow is also drawn to give the illusion of depth.
- *
- */
- void setup() {
- size(600, 600);
- rectMode(CORNER);
- noStroke();
- frameRate(1); // set the frame rate to 1 draw() call per second
- }
- void draw() {
- background(180); // clear the screen to grey
- int gridSize = 5; // (int) random(3, 12); // select a random number of squares each frame
- int gap = 10; // (int) random(5, 50); // select a random gap between each square
- // calculate the size of each square for the given number of squares and gap between them
- float cellsize = ( width - (gridSize + 1) * gap ) / (float)gridSize;
- // print out the size of each square
- println("cellsize = " + cellsize);
- // calculate shadow offset
- float offsetX = cellsize/16.0;
- float offsetY = cellsize/16.0;
- for (int i=0; i<gridSize; i++) {
- for (int j=0; j<gridSize; j++) {
- float x = gap * (i+1) + cellsize * i;
- float y = gap * (j+1) + cellsize * j;
- x = x + random(-gap, gap); // offsetting the square by some random value
- y = y + random(-gap, gap); // offsetting the square by some random value
- fill(140, 180); // shadow
- rect(x+offsetX, y+offsetY, cellsize, cellsize);
- if (random(1)>0.75) { // flip a 'coin' to select the color of the square
- fill(255, 0, 0, 180); // paint it dark red
- } else {
- fill(247, 57, 57, 180); // paint it light red
- }
- rect(x, y, cellsize, cellsize); // draw rectangle
- }
- }
- } //end of draw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement