dentonpotter

Ellipse baubles in a grid of squares

Aug 15th, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. /*
  2. * Creative Coding Exercise
  3. * CC-BY Karen Cropper
  4. * Licence CC-BY 4.0 August 2015
  5. *
  6. * This code creates a grid of squares on a rectangular display area
  7. * The layout is calculated based on a margin and then fitting a
  8. * number of squares to the height, then using the square size to
  9. * calculate the gap between squares in the x direction.
  10. *
  11. * The rectangles and ellipses all draw from the centre
  12. * Fills from the centre of the display window
  13. *
  14. * Automatically saves image no overwriting adds a date/time part
  15. *
  16. */
  17.  
  18. // setup function -- called once when the program begins
  19. void setup() {
  20.  
  21. // set the display window in pixels
  22. size(1200, 600);
  23.  
  24. // set the rectangle mode to draw from the centre
  25. rectMode(CENTER);
  26.  
  27. // Only want everything coded in the draw section to run once
  28. noLoop();
  29.  
  30.  
  31. }// end of set up
  32.  
  33. // draw function -- called continuously while the program is running
  34. void draw() {
  35.  
  36. /* Defining color mode as HSB and
  37. * variables so that can change them easily in one place
  38. * when want to try out different colour combinations
  39. */
  40. colorMode(HSB, 360, 100, 100); //set mode to HSB
  41. color baseCol = color(260, 100, 100); // background colour
  42. color mainSq = color(360, 100, 100); // squares
  43. color otherSq = color(60,100,100); // square random
  44. color line = color(260, 0, 0); // stroke/line where appears
  45. color mainEl = color(60, 100, 100, 100); // ellipses
  46. color otherEl = color(360, 100, 100, 100); // ellipses random
  47.  
  48. /* Defining variables for calculation inside draw function
  49. * bacause if define in set up wont be called here
  50. */
  51. float marg = height/15; // margin in pixels
  52. int yNum = 7; // number of squares in y height direction must be odd
  53. float yGap = 2*marg/yNum; // gap between squares in y direction
  54. float xNum; // number of squares in x width direction to be calculated
  55. float xGap; // gap between squares in x direction to be calculated
  56. float sqSize; // size of square to be calculated
  57. int numEl = 8; // number of times ellipse will draw on one point
  58.  
  59. // put in background
  60. background (baseCol);
  61.  
  62. //calculate square size
  63. sqSize =(( height - (2 * marg ) - ((yNum - 1)*yGap))/yNum);
  64. // print in window to check values
  65. println (height+", "+marg+", "+yGap+", "+sqSize+" :");
  66.  
  67. // calculate gap in x direction
  68.  
  69. xNum = floor(width - (2 * marg) + yGap)/(sqSize + yGap);
  70. println("xNum is "+xNum);
  71.  
  72. xGap = ((width - (2 * marg ) - (xNum*sqSize))/(xNum - 1));
  73. // print in window to check values
  74. println (width+", "+marg+", "+xGap+", "+xNum+" :");
  75.  
  76. // draw the shapes
  77. // setting integers for x and y postition so can use for other shapes
  78. float xPos, yPos;
  79.  
  80. // loops
  81. int iStop = (floor(xNum/2))+1; // because starting at centre then going out either side
  82. int jStop = (yNum/2)+1; // because starting at centre and going above and below
  83. for (int i=-(floor(xNum/2)); i<iStop; i++) { // this is filling the rows
  84. for (int j=-(yNum/2); j<jStop; j++) { // this is filling the columns
  85.  
  86. // calc x and y position for centre point
  87. xPos = (width/2)+ ((xGap + sqSize) * i);
  88. yPos = (height/2) + ((yGap + sqSize) * j);
  89.  
  90. // select colour case
  91. int colPrep = (int) random (5); // generating frequency of state change
  92. int colCase; // setting variable for colour
  93.  
  94. if (colPrep == 0){
  95. colCase = 0;
  96. }
  97. else {
  98. colCase = 1;
  99. }
  100.  
  101. // change colour randomly
  102. switch(colCase) {
  103. case 0:
  104. fill(otherSq);
  105. noStroke();
  106. break;
  107. case 1:
  108. fill(mainSq);
  109. noStroke();
  110. break;
  111. }
  112. //draw square
  113. rect (xPos, yPos, sqSize, sqSize, height/120); // last item is corner radius
  114.  
  115. // loop for ellipse
  116. for (int k=0; k<numEl; k++) {// this is making ellipses
  117.  
  118. // Added in scaling test for large version to get strokeWeight right
  119. if (height >3000) {
  120. strokeWeight(5);
  121. } else {
  122. strokeWeight(1);
  123. } //end else
  124.  
  125.  
  126. // change colour randomly - using same number as for square
  127. switch(colCase) {
  128. case 0:
  129. fill(otherEl);
  130. stroke(line);
  131. break;
  132. case 1:
  133. fill(mainEl);
  134. stroke(line);
  135. break;
  136. }
  137. //draw ellipse
  138. ellipse(xPos, yPos, random(sqSize), random(sqSize));
  139.  
  140.  
  141. }// end of k
  142.  
  143. }//end of j
  144.  
  145. }//end of i
  146.  
  147. // saving without pressing a key because will only be running through once (noLoop)
  148. String humanName = "ellipsesquares"; // change this to a meaningful name
  149.  
  150. // this then makes a unique filename with the date in a sortable order
  151. // saving as png as I think it is crisper for this code
  152. // you can change the part at the end to jpg or tga or if you leave that out it will default to TIF
  153. String fileName = humanName+"_"+year()+month()+day()+"_"+hour()+minute()+second()+".png";
  154.  
  155. saveFrame(fileName);
  156. println("The file "+fileName+" has been saved"); // confirms that has been saved and what name
  157.  
  158. } // end of draw function
Advertisement
Add Comment
Please, Sign In to add comment