August 14, 2012

mathncode:

I was requested to make tutorials and/or publish my code. So here’s a piece of work with the details explained!

So, the idea of these sketches is that we have a number of pendulums. These pendulums move at a placid pace, forming a perfect circle, although their movement could be different (and in that case the shape that is formed would be different). The first pendulum circles a fixed point (in this case, the center point of the sketch); the second pendulum’s center point is the moving tip of the first pendulum; and so on. In this sketch, we have three pendulums.

Now we have to decide the pendulums’ lengths (or the circles’ radiuses) and the pace at which the pendulums circle their center points. For that purpose we have the variables pendx_len (the length/radius of the pendulum), and alfax_diff (the pace at which the pendulum turns around its center point per frame). These values can be different for each pendulum.

Then we have to decide what to draw. We can choose to draw just the tip of the last pendulum or we can choose to draw all the pendulums with their whole bodies. We also have to decide whether we want the drawing to leave a trace of the pendulums’ movement or whether we just want to see an animation of the pendulums’ movement, in which case we have to command the background to be white (or the colour of our choice) after each frame. And we have to decide the stroke’s weight and transparency.

Then we can run the code and take pictures of our evolving formation by embedding a function for saving frames.

The Processing code is here:

/*
* these values can be changed, should you want to see a different shape formed
*
*/
// width and height of screen
int W = 600;
int H = 600;
// center coordinates
float CX = .5*W;
float CY = .5*H;
// pendulum lengths
float pend1_r = min(W,H)*.1;
float pend2_r = min(W,H)*.2;
float pend3_r = min(W,H)*.1;
// pendulum angle start values
float startval1 = 0;
float startval2 = HALF_PI;
float startval3 = 0;
// pendulum angle differences per frame
float alfa1_diff = .04;
float alfa2_diff = -.05;
float alfa3_diff = -.03;


// these variables and values should be left like this
float pend1_x;
float pend1_y;
float pend2_x;
float pend2_y;
float pend3_x;
float pend3_y;
float alfa1 = startval1;
float alfa2 = startval2;
float alfa3 = startval3;


// setting up values
void setup(){
// screen size
size(W,H);
// this makes the drawing look smoother
smooth();
// background colour
background(255);
// determining stroke colour and transparency
stroke(0, 50);
// determining strokeweight
strokeWeight(0);
}


// this code is executed after each frame
void draw(){
// the coordinates of the tip of the first pendulum
pend1_x = CX + pend1_r*sin(alfa1);
pend1_y = CY + pend1_r*cos(alfa1);
// drawing the first pendulum (optional)
line(CX,CY,pend1_x,pend1_y);
// the coordinates of the tip of the second pendulum
pend2_x = pend1_x + pend2_r*sin(alfa2);
pend2_y = pend1_y + pend2_r*cos(alfa2);
// drawing the second pendulum (optional)
line(pend1_x,pend1_y,pend2_x,pend2_y);
// the coordinates of the tip of the third pendulum
pend3_x = pend2_x + pend3_r*sin(alfa3);
pend3_y = pend2_y + pend3_r*cos(alfa3);
// drawing the third pendulum (optional)
line(pend2_x,pend2_y,pend3_x,pend3_y);
// altering the angles so the pendulums keep moving
alfa1 += alfa1_diff;
alfa2 += alfa2_diff;
alfa3 += alfa3_diff;
}


// a function to save frames on pressing enter
void keyPressed(){
if(keyCode==ENTER){
saveFrame("screen-####.png");
}
}

(Source: my-pink-code)