Advertisement
xeromino

danielExample

Aug 18th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 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 num = 5;  // the number of spinning tops
  13. float[] x = new float [num];
  14. float[] y = new float [num];      // current drawing position
  15. float[] dx = new float [num];  
  16. float[] dy = new float [num];  
  17. float rad;       // radius for the moving hand
  18.  
  19. void setup() {
  20.   size(500, 500);
  21.   background(0);
  22.   for (int i=0; i<num; i++) {
  23.     float val = 2; //  value for the max/min value of the increments
  24.     x[i]= random(width); // initial horizontal starting point
  25.     y[i]= random(height); // initial vertical starting point
  26.     dx[i] = random(-val, val); // x-increments
  27.     dy[i] = random(-val, val); // y-increments
  28.   }
  29. }
  30.  
  31.  
  32. void draw() {
  33.   // blendMode(BLEND); .-> not needed, it's the default
  34.   fill(0, 20); // I would increase the alpha to 15 in order not to have 'smears', except if that's what you want ;)
  35.   rect(0, 0, width, height);
  36.   rad = radians(frameCount);
  37.   for (int i=0; i<num; i++) {
  38.     spinningTop(i, x[i], y[i], dx[i], dx[i]);
  39.   }
  40. }
  41.  
  42. void spinningTop(int i, float _x, float _y, float _dx, float _dy) {
  43.   // calculate new position
  44.   x[i] += dx[i];
  45.   y[i] += dy[i];
  46.   float max = 1.5;
  47.   float min = 0.5;
  48.  
  49.   //When the shape hits the edge of the window, it reverses its direction and changes velocity
  50.   if (x[i] > width-100 || x[i] < 100) {
  51.     dx[i] = dx[i] > 0 ? -random(min, max) : random(min, max);
  52.   }
  53.  
  54.   if (y[i] > height-100 || y[i] < 100) {
  55.     dy[i] = dy[i] > 0 ? -random(min, max) : random(min, max);
  56.   }
  57.  
  58.   float bx = x[i] + 100 * sin(rad);
  59.   float by = y[i] + 100 * cos(rad);
  60.   fill(180);
  61.  
  62.   float radius = 100 * sin(rad*0.1);
  63.   float handX = bx + radius * sin(rad*3);
  64.   float handY = by + radius * cos(rad*3);
  65.  
  66.   stroke(255, 50);
  67.   line(bx, by, handX, handY);
  68.   ellipse(handX, handY, 2, 2);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement