Advertisement
Guest User

Untitled

a guest
May 27th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. // import le libraries
  2. import ddf.minim.*;
  3.  
  4. Minim minim;
  5. AudioPlayer bmusic;
  6.  
  7. int w = 1000; // width of window
  8. int h = 500; // hight of window
  9. int diagonale = (int) Math.sqrt((w * w)  + (h * h));
  10. int r = 25; // r>4 velikost malega ฤrnega krogca
  11. int bgRed = 255, bgGreen = 255, bgBlue = 255;
  12.  
  13. int circlesVertically = h / (2 * r);
  14. int circlesHorizontally = w / (2 * r); // variables, number of lattice points needed to fill the drawing area horizontally and vertically
  15.  
  16. int mousePressX, mousePressY;
  17.  
  18. float k = 0; // counter variable for the animation
  19. float kIncrement = 0.15f; // how slowly are the black dots turning (i.e. how many waves are there) SPEED
  20. float dotSize = 25; // size of dots
  21.  
  22. // limits of enlarging and shrinking the dots
  23. int dotSizeMin = 5, dotSizeMax = 2*r;
  24. float maxRad = PI*2, thisRadiant, thisAngleToTop, thisBackgroundVariant;
  25. PVector mousePos = new PVector(0, 0);
  26. float pull = 0;
  27.  
  28. void setup(){
  29.     minim = new Minim(this);
  30.     //bmusic = minim.loadFile("rainforest_ambience.wav");
  31.     //bmusic.play();
  32.     size(w, h);
  33.     smooth();
  34.     frameRate(24);
  35. }
  36.  
  37. void draw() {
  38.     background(255);
  39.  
  40.     for (int i = 0; i < circlesHorizontally; i++) { // i and j are the phases shift of each point
  41.         for (int j = 0; j < circlesVertically; j++) {
  42.                  
  43.             int circleX = (2 * i + 1) * r; // circleX and circleY coordinates of the each circle
  44.             int circleY = (2 * j + 1) * r;
  45.          
  46.             // white background circles
  47.             fill(bgRed, bgGreen, bgBlue);
  48.             stroke(128, 128, 128);
  49.             ellipse(circleX, circleY, 2*r - 4, 2*r - 4); // you draw these circles
  50.          
  51.             // calculate this black circle's deviation from it's white background circle
  52.             thisRadiant = radians((i+j+k) * 15);
  53.             if (thisRadiant > maxRad) {
  54.                 thisRadiant = thisRadiant % maxRad; // bring the radians back to a maximum of 2*PI
  55.             }
  56.             thisAngleToTop = abs(thisRadiant - PI);
  57.             thisBackgroundVariant = (thisAngleToTop % PI) / PI;
  58.             if (round(thisAngleToTop * 100) == 314) { // check if the deviation is exacly PI (3.14)
  59.                 thisBackgroundVariant = 1;
  60.             }
  61.          
  62.          
  63.             int blackCircleX = circleX + (int) ((r-2) * sin(thisRadiant));
  64.             int blackCircleY = circleY + (int) ((r-2) * cos(thisRadiant));
  65.          
  66.             PVector circle = new PVector(blackCircleX, blackCircleY);
  67.             if (mouseX != 0) {
  68.                 mousePos = new PVector(mouseX, mouseY);
  69.                 // substract the position of the circle from the position of the mouse
  70.                 mousePos.sub(circle);
  71.                 pull = diagonale / mousePos.mag(); // make the pull dependend on the size of the window
  72.             }
  73.            
  74.             // the pull might shoot the circle off beyond the cursor position
  75.             if (pull > mousePos.mag()) {
  76.                 pull = mousePos.mag();
  77.             }
  78.          
  79.             mousePos.normalize(); // now make the vector have a length of 1
  80.             blackCircleX += mousePos.x * pull; // multiply the direction from the circle's position to he mouse with the pull
  81.             blackCircleY += mousePos.y * pull;
  82.             fill(255 * thisBackgroundVariant);
  83.             ellipse(blackCircleX, blackCircleY, dotSize, dotSize);
  84.            
  85.             // debug shizzle
  86.             //fill(255, 0, 0);
  87.             //textSize(20);
  88.             //text(pull, circleX - 15, circleY + 5);
  89.             //println(mousePos.x, mousePos.y);
  90.             //strokeWeight(2);
  91.             //line(blackCircleX + mousePos.x * pull, blackCircleY + mousePos.y * pull, circleX, circleY);
  92.         }
  93.     }
  94.     k+=kIncrement;
  95.    
  96. }
  97.  
  98. void keyPressed () {
  99.     println("KEY PRESS:", keyCode);
  100.    
  101.     switch (keyCode) {
  102.        
  103.         case 521: // + key
  104.             if (dotSize < dotSizeMax && kIncrement > 0.015f) {
  105.                 println("bigger");
  106.                 dotSize++;
  107.                 kIncrement -= 0.01f;
  108.             }
  109.             break;
  110.    
  111.         case 45: // - key
  112.             if (dotSize > dotSizeMin) {
  113.                 println("smaller");
  114.                 dotSize--;
  115.                 kIncrement += 0.01f;
  116.             }
  117.             break;
  118.        
  119.         case 32: // space bar
  120.             stop();
  121.             break;
  122.            
  123.         case LEFT:
  124.             bgRed = 206;
  125.             bgGreen = 175;
  126.             bgBlue = 175;
  127.             break;
  128.            
  129.         case RIGHT:
  130.             bgRed = 237;
  131.             bgGreen = 225;
  132.             bgBlue = 236;
  133.             break;
  134.            
  135.         default:
  136.             break;
  137.     }
  138. }
  139.  
  140. void stop() {
  141.     //bmusic.close();
  142.     //minim.stop();
  143.     super.stop();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement