Advertisement
xeromino

squares

May 12th, 2017
290
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Creative Coding
  3.  * Week 2, 03 - n squares
  4.  * by Indae Hwang and Jon McCormack
  5.  * Updated 2016
  6.  * Copyright (c) 2014-2016 Monash University
  7.  
  8.  This program is free software: you can redistribute it and/or modify
  9.  it under the terms of the GNU General Public License as published by
  10.  the Free Software Foundation, either version 3 of the License, or
  11.  (at your option) any later version.
  12.  
  13.  This program is distributed in the hope that it will be useful,
  14.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  GNU General Public License for more details.
  17.  
  18.  You should have received a copy of the GNU General Public License
  19.  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21.  * In the next iteration of the square drawing sketch, this version selects a random number of squares
  22.  * and a random gap between them. From this it calculates the width of each square then draws the squares
  23.  * using two nested for loops.
  24.  *
  25.  * A simple drop shadow is also drawn to give the illusion of depth.
  26.  *
  27.  */
  28.  
  29. void setup() {
  30.   size(600, 600);
  31.   rectMode(CORNER);
  32.   noStroke();
  33.   frameRate(1);  // set the frame rate to 1 draw() call per second
  34. }
  35.  
  36.  
  37. void draw() {
  38.  
  39.   background(180); // clear the screen to grey
  40.  
  41.   int gridSize = 5; // (int) random(3, 12);   // select a random number of squares each frame
  42.   int gap = 10; // (int) random(5, 50); // select a random gap between each square
  43.  
  44.   // calculate the size of each square for the given number of squares and gap between them
  45.   float cellsize = ( width - (gridSize + 1) * gap ) / (float)gridSize;
  46.  
  47.   // print out the size of each square
  48.   println("cellsize = " + cellsize);
  49.  
  50.   // calculate shadow offset
  51.   float offsetX = cellsize/16.0;
  52.   float offsetY = cellsize/16.0;
  53.  
  54.  
  55.   for (int i=0; i<gridSize; i++) {
  56.     for (int j=0; j<gridSize; j++) {
  57.  
  58.       float x = gap * (i+1) + cellsize * i;
  59.       float y = gap * (j+1) + cellsize * j;
  60.       x = x + random(-gap, gap); // offsetting the square by some random value
  61.       y = y + random(-gap, gap); // offsetting the square by some random value
  62.  
  63.       fill(140, 180); // shadow
  64.       rect(x+offsetX, y+offsetY, cellsize, cellsize);
  65.  
  66.       if (random(1)>0.75) { // flip a 'coin' to select the color of the square
  67.         fill(255, 0, 0, 180); // paint it dark red
  68.       } else {
  69.         fill(247, 57, 57, 180); // paint it light red
  70.       }
  71.       rect(x, y, cellsize, cellsize); // draw rectangle
  72.     }
  73.   }
  74. } //end of draw
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement