shiv379

Creative Coding w3_04 - Spirals

Aug 26th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. /*
  2.  * Creative Coding
  3.  * Week 3, 04 - spinning top: curved motion with sin() and cos()
  4.  * by Indae Hwang and Jon McCormack
  5.  * Copyright (c) 2014 Monash University
  6.  *
  7.  * This sketch is the first cut at translating the motion of a spinning top
  8.  * to trace a drawing path. This sketch traces a path using sin() and cos()
  9.  *
  10.  */
  11.  
  12. int maxLines = 100; //allocate space for up to 100 lines
  13. float x[] = new float[maxLines], y[] = new float[maxLines];      // current drawing position
  14. float dx[] = new float[maxLines], dy[] = new float[maxLines];    // change in position at each frame
  15. float rad[] = new float[maxLines];       // radius for the moving hand
  16. int col[] = new int[maxLines];
  17.  
  18. float max = 3; //dx/dy max default
  19. float min = 0.5; //dx/dy min default
  20. int radiusScale = 300; //Radius scale default value
  21. int frameCounter= 0, fps = 40; //Default framerate (fps)
  22. float blendRate = 3; //Rate for image blending
  23. int numLines = 20; //set default number of lines
  24.  
  25. void setup() {
  26.  
  27.   size(1000, 500);
  28.   frameRate(fps);
  29.   colorMode(HSB, 100);
  30.  
  31.   for (int i =0; i < numLines; i++) {
  32.     // initial position in the centre of the screen
  33.     x[i] = width/2;
  34.     y[i] = height/2;
  35.  
  36.     // dx and dy are the small change in position each frame
  37.     dx[i] = random(min, max);
  38.     dy[i] = random(min, max);
  39.     col[i] = (int) random(100);
  40.   }
  41.   background(0);
  42. }
  43.  
  44.  
  45. void draw() {
  46.  
  47.   // blend the old frames into the background
  48.   blendMode(BLEND);
  49.   fill(0, blendRate);
  50.   rect(0, 0, width, height);
  51.  
  52.   for (int i =0; i < numLines; i++) {
  53.     rad[i] = radians(frameCount+(i*PI));
  54.     // calculate new position
  55.     if (mousePressed) {
  56.       x[i] = mouseX > x[i] ? x[i] + dx[i] : x[i] - dx[i];
  57.       y[i] = mouseY > y[i] ? y[i] + dy[i] : y[i] - dy[i];
  58.     } else {
  59.       x[i] += dx[i];
  60.       y[i] += dy[i];
  61.     }
  62.  
  63.     //When the shape hits the edge of the window, it reverses its direction and changes velocity
  64.     if (x[i] > width-100 || x[i] < 100) {
  65.       dx[i] = dx[i] > 0 ? -random(min, max) : random(min, max);
  66.     }
  67.  
  68.     if (y[i] > height-100 || y[i] < 100) {
  69.       dy[i] = dy[i] > 0 ? -random(min, max) : random(min, max);
  70.     }
  71.  
  72.     float bx = x[i] + 100 * sin(rad[i]);
  73.     float by = y[i] + 100 * cos(rad[i]);
  74.  
  75.     fill(180);
  76.  
  77.     float radius = radiusScale * sin(rad[i]*0.1);
  78.     float handX = bx + radius * sin(rad[i]*3);
  79.     float handY = by + radius * cos(rad[i]*3);
  80.  
  81.     stroke(col[i], 100, 100, 50);
  82.     line(bx, by, handX, handY);
  83.     ellipse(handX, handY, 2, 2);
  84.     }
  85.    
  86.   if (frameCounter > 0) {
  87.     showCommands();
  88.     frameCounter--;
  89.     //if (frameCounter > 0 ) { frameRate(fps); }
  90.   }
  91.    
  92.    if (keyPressed == true) {
  93.     switch( key ) {
  94.       case 's':
  95.         saveFrame("yourName-###.jpg");
  96.         println("Screenshot saved");
  97.         break;
  98.       case '+':
  99.         max = ((max+1)< (height/2)) ? max+1 : max;
  100.          println("Max dx/dy increased to: " + max);
  101.         break;
  102.       case '-':
  103.         max = ((max-1)>0) ? max-1 : max;
  104.         println("Max dx/dy reduced to: " + max);
  105.         break;
  106.       case 'R':
  107.         radiusScale = ((radiusScale+1)< height) ? radiusScale+1 : radiusScale;
  108.          println("Radius scaling increased to: " + radiusScale);
  109.         break;
  110.       case 'r':
  111.         radiusScale = ((radiusScale-1)>0) ? radiusScale-1 : radiusScale;
  112.         println("Radius scaling reduced to: " + radiusScale);
  113.         break;
  114.       case 'c':
  115.         for (int i=0; i<numLines; i++) {
  116.             col[i] =  (int) random(100);
  117.         }
  118.         println("Colours randomised.");
  119.         break;
  120.       case 'x':
  121.         background(0);
  122.         println("Background cleared.");
  123.         break;
  124.       case '?':
  125.         displayCommands();
  126.         break;
  127.       case 'B':
  128.         blendRate = ((blendRate+0.1)< 20) ? blendRate+0.1 : blendRate;
  129.          println("Blend rate increased to: " + blendRate);
  130.         break;
  131.       case 'b':
  132.         blendRate = ((blendRate-0.1)>=0) ? blendRate-0.1 : blendRate;
  133.         println("Blend rate reduced to: " + blendRate);
  134.         break;
  135.       case 'l':
  136.         numLines = (numLines>1) ? numLines-1 : numLines;
  137.         println("Line count reduced to: " + numLines);
  138.         setup();
  139.       break;
  140.       case 'L':
  141.         numLines = (numLines<100) ? numLines+1 : numLines;
  142.         println("Line count increased to: " + numLines);
  143.         setup();
  144.       break;
  145.     }
  146.    }
  147. }
  148.  
  149. void showCommands() {
  150.   float txSize = 20;
  151.  
  152.    textSize(txSize);
  153.     String helpLines[] = new String[9];
  154.     helpLines[0] = "--Commands--";
  155.     helpLines[1] = "s: Save screenshot";
  156.     helpLines[2] = "x: Clear background";
  157.     helpLines[3] = "?: Show this help";
  158.     helpLines[4] = "c: Randomise colours";
  159.     helpLines[5] = "+/-: +/- dx/dy max";
  160.     helpLines[6] = "R/r: +/- radius";
  161.     helpLines[7] = "L/l: +/- number of lines";
  162.     helpLines[8] = "B/b: +/- blend rate";
  163.    
  164.     fill(0,10);
  165.     noStroke();
  166.     rect(0,0,width/3,(txSize+2)*helpLines.length);
  167.    
  168.     fill(100);
  169.     for (int i=0; i< helpLines.length; i++){
  170.       text(helpLines[i], 5, txSize+2+((txSize+2)*i));
  171.     }
  172.  
  173. }
  174.  
  175. void displayCommands(){
  176.   frameCounter = (int) floor(5*frameRate); // Show help for this number of frames
  177.   showCommands();
  178. }
Advertisement
Add Comment
Please, Sign In to add comment