openobject.org

A line of circles follows the mouse pointer

From Physical Programming

Image:A_line_of_circles_follows_the_mouse_pointer.jpg

Image:Source code with descriptions.pdf


/* A line of circles follows the mouse pointer */

/** global variables, x and y are the coordinates of
* each circle, i.e. position of circle 1 = x[1], y[2], with
* maximum amount of circles equal 10
*/

float[] x = new float[10];
float[] y = new float[10];

/** With this function the main window is constructed, size 
 *  determines the windows size and smooth controls the drawing 
 *  of new objects for a smooth animation
 */
 
void setup() {
  size(600, 600);
  smooth(); 
}

/** With function draw() the background is set to black. 
 *  Function circleline will draw each circle in a line after
 *  the mouse pointer. For this, it gets for circle 0 the 
 *  coordinates of the actual mousepointer position.
 *  In the for-loop circles 1-10 will be created with their 
 *  calculated position.
 */
 
void draw() {
  background(0);
  circleline(0, mouseX, mouseY);  // first circle
  
  // For-Loop for the next circles
  for(int i=0; i<x.length-1; i++) {
    circleline(i+1, x[i], y[i]);
  }
}
/** This function calculates the position for circle i.
 *  dx,dy ist the distance between circle i and its predecessor.
 *  With atan, cos, sin it calculates the angle between the 
 *  circles, so its not just a static line, but a snake.
 *  After that it starts function circle which will draw the 
 *  circle with given position, angle to predecessor and position
 */

void circleline(int i, float xin, float yin) {
  float dx = xin - x[i];
  float dy = yin - y[i];
  float angle = atan2(dy, dx);  
  x[i] = xin - cos(angle) * 25;
  y[i] = yin - sin(angle) * 25;
  circle(x[i], y[i], angle, i);
}

/** Function circle actually draws the circle with standart 
 *  methods
 *  Last fill sets color and the transparency by given position i
 */

void circle(float x, float y, float a, int i) {
  pushMatrix();           /**Pushes the current transformation matrix onto 
                            *the matrix stack. Understanding pushMatrix() 
                            *and popMatrix() requires understanding the 
                            *concept of a matrix stack. The pushMatrix()
                            *function saves the current coordinate system 
                            *to the stack and popMatrix() restores the 
                            *prior coordinate system. pushMatrix() and
                            *popMatrix() are used in conjuction with the 
                            *other transformation methods and may be 
                            *embedded to control the scope of the  
                            *transformations.
                            */

  translate(x, y);         /** 	Specifies an amount to displace objects 
                             *within the display window. The x parameter 
                             *specifies left/right translation, the y 
                             *parameter specifies up/down translation, and 
                             *the z parameter specifies translations
                             *toward/away from the screen.  Transformations
                             *apply to everything that happens after and
                             *subsequent calls to the function accumulates 
                             *the effect. For example, calling translate(50, 0)
                             *and then translate(20, 0) is the same as
                             *translate(70, 0). If translate() is called 
                             *within draw(), the transformation is reset 
                             *when the loop begins again. This function can 
                             *be further controlled by the pushMatrix() and     
                             *popMatrix().
                             */

  rotate(a);                /**Rotates a shape the amount specified by the
                              *angle parameter. Objects are always rotated
                              *around their relative position to the origin
                              *and positive numbers rotate objects in a 
                              *clockwise direction. Transformations apply 
                              *to everything that happens after and
                              *subsequent calls to the function accumulates
                              *the effect. 
                              *Technically, rotate() multiplies the current
                              *transformation matrix by a rotation matrix.
                              *This function can be further controlled by 
                              *the pushMatrix() and popMatrix().
                              */

  popMatrix();               /**Pops the current transformation matrix off 
                               *the matrix stack.
                               */

  ellipse(x, y, 15, 15);     // circles

  fill(255,0,0,100-i*10);    // color with transparenz
}