Jump to Content

RSS Feeds:

Posts | Comments

http://ivanwlam.com/experiments/programming

Leave a Reply


Example

You need Java to view these. You can click on the Java applet to start and stop the animations.

Processing Code in Example

 //set the number of frames  int frameR=2;  //set the number of sides (or points) to draw from  int sides=8;    //the rest is the same as the other files.    void setup(){   size(345,345);   frameRate(frameR);  }    //start the next iteration (connection) at 1.  int nextIt=1;    //switches for animation purposes  boolean itUp=true;  boolean looping=true;    void draw(){   //mark the centers to measure the distances from   int x=width/2;   int y=height/2;     //start angle at 0   float angle=0;     background(204);   smooth();     //radius is distance between center to side of the screen minus 10 to give some padding   int rad=x-10;     //define old and new values to be connected later   float oldx=0,oldy=0,newx=0,newy=0;     //get arrays to hold the point values   float[] xValues=new float[sides];   float[] yValues=new float[sides];     //set rotation angle for each turn.   float rot=360.0/sides;     //store the values of the points in an array   for(int i=0;i<sides;i++){   oldx=x+rad*(cos(radians(angle)));   oldy=y+rad*(sin(radians(angle)));   xValues[i]=oldx;   yValues[i]=oldy;            //rotate the angle and then get new values from the same formulas as above   angle+=rot;   newx=x+rad*(cos(radians(angle)));   newy=y+rad*(sin(radians(angle)));   }     //get the points in the array and connect them a particular way depending on the iteration   for(int i=0;i<sides;i++){   if(i<sides-nextIt){ //to prevent referencing a value in an array that goes beyond the number of sides as it tries to connect       stroke(0);   line(xValues[i],yValues[i],xValues[i+nextIt],yValues[i+nextIt]);   }   else { // if i gets into the territory where i+nextIt is greater than the number of sides   //just adding a color to the last stroke to see how it connects   if(i==sides-1){   stroke(255,0,0);   }   else {   stroke(0);   }   //when the lines return to the start of the array values   line(xValues[i],yValues[i],xValues[i-(sides-nextIt)],yValues[i-(sides-nextIt)]);   }   }     //setting up the connection iteration direction (++ or --) depending on the current nextIt value   if(itUp==true && nextIt<sides){   nextIt+=1;   }   else if(itUp==true && nextIt>=sides){   itUp=false;   nextIt-=1;   }   else if(itUp==false && nextIt<sides && nextIt>0){   nextIt-=1;   }   else if(itUp==false && nextIt==0){   itUp=true;   nextIt+=1;   }     //for local coding only. spits out a screenshot named by the sides value and the framerate value   if(nextIt==round(sides/3)){   saveFrame("output-"+sides+"at"+frameR+".png");   }    }    //clicking starts and stops the animation, controlled by a switch  void mouseClicked(){   if(looping==true){   noLoop();   looping=false;   }   else{   loop();   looping=true;   }

Processing Code in Example

 //set the number of frames  int frameR=10;  //set the number of sides (or points) to draw from  int sides=50;    //the rest is the same as the other files.    void setup(){   size(345,345);   frameRate(frameR);  }    //start the next iteration (connection) at 1.  int nextIt=1;    //switches for animation purposes  boolean itUp=true;  boolean looping=true;    void draw(){   //mark the centers to measure the distances from   int x=width/2;   int y=height/2;     //start angle at 0   float angle=0;     background(204);   smooth();     //radius is distance between center to side of the screen minus 10 to give some padding   int rad=x-10;     //define old and new values to be connected later   float oldx=0,oldy=0,newx=0,newy=0;     //get arrays to hold the point values   float[] xValues=new float[sides];   float[] yValues=new float[sides];     //set rotation angle for each turn.   float rot=360.0/sides;     //store the values of the points in an array   for(int i=0;i<sides;i++){   oldx=x+rad*(cos(radians(angle)));   oldy=y+rad*(sin(radians(angle)));   xValues[i]=oldx;   yValues[i]=oldy;            //rotate the angle and then get new values from the same formulas as above   angle+=rot;   newx=x+rad*(cos(radians(angle)));   newy=y+rad*(sin(radians(angle)));   }     //get the points in the array and connect them a particular way depending on the iteration   for(int i=0;i<sides;i++){   if(i<sides-nextIt){ //to prevent referencing a value in an array that goes beyond the number of sides as it tries to connect       stroke(0);   line(xValues[i],yValues[i],xValues[i+nextIt],yValues[i+nextIt]);   }   else { // if i gets into the territory where i+nextIt is greater than the number of sides   //just adding a color to the last stroke to see how it connects   if(i==sides-1){   stroke(255,0,0);   }   else {   stroke(0);   }   //when the lines return to the start of the array values   line(xValues[i],yValues[i],xValues[i-(sides-nextIt)],yValues[i-(sides-nextIt)]);   }   }     //setting up the connection iteration direction (++ or --) depending on the current nextIt value   if(itUp==true && nextIt<sides){   nextIt+=1;   }   else if(itUp==true && nextIt>=sides){   itUp=false;   nextIt-=1;   }   else if(itUp==false && nextIt<sides && nextIt>0){   nextIt-=1;   }   else if(itUp==false && nextIt==0){   itUp=true;   nextIt+=1;   }     //for local coding only. spits out a screenshot named by the sides value and the framerate value   if(nextIt==round(sides/3)){   saveFrame("output-"+sides+"at"+frameR+".png");   }    }    //clicking starts and stops the animation, controlled by a switch  void mouseClicked(){   if(looping==true){   noLoop();   looping=false;   }   else{   loop();   looping=true;   }

Processing Code in Example

 //set the number of frames  int frameR=30;  //set the number of sides (or points) to draw from  int sides=200;    //the rest is the same as the other files.    void setup(){   size(345,345);   frameRate(frameR);  }    //start the next iteration (connection) at 1.  int nextIt=1;    //switches for animation purposes  boolean itUp=true;  boolean looping=true;    void draw(){   //mark the centers to measure the distances from   int x=width/2;   int y=height/2;     //start angle at 0   float angle=0;     background(204);   smooth();     //radius is distance between center to side of the screen minus 10 to give some padding   int rad=x-10;     //define old and new values to be connected later   float oldx=0,oldy=0,newx=0,newy=0;     //get arrays to hold the point values   float[] xValues=new float[sides];   float[] yValues=new float[sides];     //set rotation angle for each turn.   float rot=360.0/sides;     //store the values of the points in an array   for(int i=0;i<sides;i++){   oldx=x+rad*(cos(radians(angle)));   oldy=y+rad*(sin(radians(angle)));   xValues[i]=oldx;   yValues[i]=oldy;            //rotate the angle and then get new values from the same formulas as above   angle+=rot;   newx=x+rad*(cos(radians(angle)));   newy=y+rad*(sin(radians(angle)));   }     //get the points in the array and connect them a particular way depending on the iteration   for(int i=0;i<sides;i++){   if(i<sides-nextIt){ //to prevent referencing a value in an array that goes beyond the number of sides as it tries to connect       stroke(0);   line(xValues[i],yValues[i],xValues[i+nextIt],yValues[i+nextIt]);   }   else { // if i gets into the territory where i+nextIt is greater than the number of sides   //just adding a color to the last stroke to see how it connects   if(i==sides-1){   stroke(255,0,0);   }   else {   stroke(0);   }   //when the lines return to the start of the array values   line(xValues[i],yValues[i],xValues[i-(sides-nextIt)],yValues[i-(sides-nextIt)]);   }   }     //setting up the connection iteration direction (++ or --) depending on the current nextIt value   if(itUp==true && nextIt<sides){   nextIt+=1;   }   else if(itUp==true && nextIt>=sides){   itUp=false;   nextIt-=1;   }   else if(itUp==false && nextIt<sides && nextIt>0){   nextIt-=1;   }   else if(itUp==false && nextIt==0){   itUp=true;   nextIt+=1;   }     //for local coding only. spits out a screenshot named by the sides value and the framerate value   if(nextIt==round(sides/3)){   saveFrame("output-"+sides+"at"+frameR+".png");   }    }    //clicking starts and stops the animation, controlled by a switch  void mouseClicked(){   if(looping==true){   noLoop();   looping=false;   }   else{   loop();   looping=true;   }  }