Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Creative Coding Exercise
- * CC-BY Karen Cropper
- * Licence CC-BY 4.0 August 2015
- *
- * This code creates a grid of squares on a rectangular display area
- * The layout is calculated based on a margin and then fitting a
- * number of squares to the height, then using the square size to
- * calculate the gap between squares in the x direction.
- *
- * The rectangles and ellipses all draw from the centre
- * Fills from the centre of the display window
- *
- * Automatically saves image no overwriting adds a date/time part
- *
- */
- // setup function -- called once when the program begins
- void setup() {
- // set the display window in pixels
- size(1200, 600);
- // set the rectangle mode to draw from the centre
- rectMode(CENTER);
- // Only want everything coded in the draw section to run once
- noLoop();
- }// end of set up
- // draw function -- called continuously while the program is running
- void draw() {
- /* Defining color mode as HSB and
- * variables so that can change them easily in one place
- * when want to try out different colour combinations
- */
- colorMode(HSB, 360, 100, 100); //set mode to HSB
- color baseCol = color(260, 100, 100); // background colour
- color mainSq = color(360, 100, 100); // squares
- color otherSq = color(60,100,100); // square random
- color line = color(260, 0, 0); // stroke/line where appears
- color mainEl = color(60, 100, 100, 100); // ellipses
- color otherEl = color(360, 100, 100, 100); // ellipses random
- /* Defining variables for calculation inside draw function
- * bacause if define in set up wont be called here
- */
- float marg = height/15; // margin in pixels
- int yNum = 7; // number of squares in y height direction must be odd
- float yGap = 2*marg/yNum; // gap between squares in y direction
- float xNum; // number of squares in x width direction to be calculated
- float xGap; // gap between squares in x direction to be calculated
- float sqSize; // size of square to be calculated
- int numEl = 8; // number of times ellipse will draw on one point
- // put in background
- background (baseCol);
- //calculate square size
- sqSize =(( height - (2 * marg ) - ((yNum - 1)*yGap))/yNum);
- // print in window to check values
- println (height+", "+marg+", "+yGap+", "+sqSize+" :");
- // calculate gap in x direction
- xNum = floor(width - (2 * marg) + yGap)/(sqSize + yGap);
- println("xNum is "+xNum);
- xGap = ((width - (2 * marg ) - (xNum*sqSize))/(xNum - 1));
- // print in window to check values
- println (width+", "+marg+", "+xGap+", "+xNum+" :");
- // draw the shapes
- // setting integers for x and y postition so can use for other shapes
- float xPos, yPos;
- // loops
- int iStop = (floor(xNum/2))+1; // because starting at centre then going out either side
- int jStop = (yNum/2)+1; // because starting at centre and going above and below
- for (int i=-(floor(xNum/2)); i<iStop; i++) { // this is filling the rows
- for (int j=-(yNum/2); j<jStop; j++) { // this is filling the columns
- // calc x and y position for centre point
- xPos = (width/2)+ ((xGap + sqSize) * i);
- yPos = (height/2) + ((yGap + sqSize) * j);
- // select colour case
- int colPrep = (int) random (5); // generating frequency of state change
- int colCase; // setting variable for colour
- if (colPrep == 0){
- colCase = 0;
- }
- else {
- colCase = 1;
- }
- // change colour randomly
- switch(colCase) {
- case 0:
- fill(otherSq);
- noStroke();
- break;
- case 1:
- fill(mainSq);
- noStroke();
- break;
- }
- //draw square
- rect (xPos, yPos, sqSize, sqSize, height/120); // last item is corner radius
- // loop for ellipse
- for (int k=0; k<numEl; k++) {// this is making ellipses
- // Added in scaling test for large version to get strokeWeight right
- if (height >3000) {
- strokeWeight(5);
- } else {
- strokeWeight(1);
- } //end else
- // change colour randomly - using same number as for square
- switch(colCase) {
- case 0:
- fill(otherEl);
- stroke(line);
- break;
- case 1:
- fill(mainEl);
- stroke(line);
- break;
- }
- //draw ellipse
- ellipse(xPos, yPos, random(sqSize), random(sqSize));
- }// end of k
- }//end of j
- }//end of i
- // saving without pressing a key because will only be running through once (noLoop)
- String humanName = "ellipsesquares"; // change this to a meaningful name
- // this then makes a unique filename with the date in a sortable order
- // saving as png as I think it is crisper for this code
- // you can change the part at the end to jpg or tga or if you leave that out it will default to TIF
- String fileName = humanName+"_"+year()+month()+day()+"_"+hour()+minute()+second()+".png";
- saveFrame(fileName);
- println("The file "+fileName+" has been saved"); // confirms that has been saved and what name
- } // end of draw function
Advertisement
Add Comment
Please, Sign In to add comment