We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Class: Trying to combine two sketches together
Page Index Toggle Pages: 1
Class: Trying to combine two sketches together (Read 862 times)
Class: Trying to combine two sketches together
May 2nd, 2010, 7:47pm
 
I'm trying to combine these sketches together.  Is this even possible?  Im trying to use Classes but im having trouble.

Limbs
main
int width = 800;  
int height = 800;

int nTentacles = 10;

int nSegs = 30;

float totalLength = width/2;  
float downScaling = .9;
float baseLength;
float baseThickness = 10;
float maxdTheta = 1.5*(2*PI)/nSegs;
float maxdThetaVelPerStep = maxdTheta/40;
float maxThetaVel = maxdThetaVelPerStep*2;
float trickleVelScaling = nSegs*.003;

boolean centeredTentacles = true;
boolean drawSuckers = true;

color backgroundColor = color(255,255,255);
color tentacleColor = 0;
color suckerColor = #D1F6FA;
color suckerOutlineColor = 0;
float suckerScale = .8;


Tentacle[] tentacles;


void setup(){
frameRate(30);
smooth();
size(width,height);

int nLeft = 5;
int nRight = 5;
int nTop = 6;
int nBottom = 6;
nTentacles = nLeft+nRight+nTop+nBottom;
tentacles = new Tentacle[nTentacles];

float angle = 0;
for(int i=0; i<nLeft; i++)
 tentacles[i] = new Tentacle(0, height*i/nLeft, angle);

angle = PI;
for(int i=0; i<nRight; i++)
 tentacles[i+nLeft] = new Tentacle(width, height*i/nRight, angle);

angle = PI/2;
for(int i=0; i<nTop; i++)
 tentacles[i+nLeft+nRight] = new Tentacle(width*i/nTop, 0, angle);

angle = 3*PI/2;
for(int i=0; i<nBottom; i++)
 tentacles[i+nLeft+nRight+nTop] = new Tentacle(width*i/nBottom, height, angle);
}


void draw(){
 blankScreen();  
 for(int i=0; i<nTentacles; i++){  
   tentacles[i].drawTentacle();
   tentacles[i].wiggleTentacle();
 }
}

void blankScreen(){  
 fill(backgroundColor);
 noStroke();
 rect(0,0,width,height);
}

tentacle


class Tentacle{    
 float x0,y0,theta0;
 float[] segLengths;
 float[] segThetas;
 
 Tentacle(float x0i, float y0i, float theta0i){
   x0 = x0i;
   y0 = y0i;
   theta0 = theta0i;
 
   baseLength = totalLength*(downScaling-1)/(pow(downScaling,nSegs)-1);
   
   segLengths = new float[nSegs];
   segThetas = new float[nSegs];
   for(int i=0; i<nSegs; i++){
     if(i==0)
       segLengths[i] = baseLength;
     else
       segLengths[i] = segLengths[i-1]*downScaling;
   }
 }

 void drawTentacle(){
   float x1 = x0;  
   float y1 = y0;  
   float x2 = x0;  
   float y2 = y0;  
   float totalTheta = theta0;
   for(int i=0; i<nSegs; i++){
     stroke(tentacleColor);
     strokeWeight(baseThickness*segLengths[i]/segLengths[0]);
     totalTheta+=segThetas[i];
     x2 = x1+segLengths[i]*cos(totalTheta);
     y2 = y1+segLengths[i]*sin(totalTheta);
     line(x1,y1,x2,y2);
   
     if(drawSuckers){
       fill(suckerColor);
       stroke(suckerOutlineColor);
       strokeWeight(0);
       ellipse((x1+x2)/2,(y1+y2)/2,
              baseThickness*segLengths[i]/segLengths[0]*suckerScale,
              baseThickness*segLengths[i]/segLengths[0]*suckerScale);

     }
     x1 = x2;
     y1 = y2;
   }
 }
 
 
 float[] segVelocities = new float[nSegs];
 
 void wiggleTentacle(){
   for(int i = 0; i<nSegs; i++){
     

     if(segThetas[i] <= -maxdTheta || segThetas[i] >= maxdTheta)
       segVelocities[i] = 0;
   }
 
   for(int i = nSegs-2; i>=0; i--){
     segVelocities[i] += segVelocities[i+1]*trickleVelScaling;
   }
   

   segVelocities[nSegs-10] += random(-maxdThetaVelPerStep,maxdThetaVelPerStep);
   

   int sign = 1;
   for(int i = 0; i<nSegs; i++){
     if(segThetas[i]>0 && segVelocities[i]>0){
       segVelocities[i] = min(segVelocities[i],
                              maxThetaVel*(1-segThetas[i]/maxdTheta));
     }
   if(segThetas[i]<0 && segVelocities[i]<0){
       segVelocities[i] = min(abs(segVelocities[i]),
                              maxThetaVel*(1-abs(segThetas[i])/maxdTheta));
       segVelocities[i] = -segVelocities[i];
     }
   }

   for(int i = 0; i<nSegs; i++){
     segThetas[i] = constrain(segThetas[i]+segVelocities[i],
                              -maxdTheta*(1-i/nSegs),
                              maxdTheta*(1-i/nSegs));
   }
 }
}

Re: Class: Trying to combine two sketches together
Reply #1 - May 2nd, 2010, 7:48pm
 
molecules
int numParticleGroups = 300;
int spreadRadius = 12;
int radius = 10;
int shimmer = 5;

ParticleGroup[] particles = new ParticleGroup[numParticleGroups];
final float Pi180 = PI / 180;

void setup() {

 size(600, 600);
 background(255);
 colorMode(HSB);
 noStroke();
 smooth();

 for (int i = 0; i < numParticleGroups; i++) {
   particles[i] = new ParticleGroup();
 }
}

void draw() {

 background(255);

 for (int i = 0; i < numParticleGroups; i++) {
   particles[i].move();
 }
}

class ParticleGroup {

 float xspeed, yspeed, xradius, yradius, xangle, yangle, spread1, spread2, spread3, spread4, xp, yp, r;
 color col;

 ParticleGroup() {

   this.xspeed = random(1, 2);
   this.yspeed = random(1, 2);
   this.xradius = random(0, 200);
   this.yradius = random(0, 200);
   this.r = radius;
   this.xangle = random(5,12);
   this.yangle = random(1,2);
   this.col = color(random(160, 170), random(100, 255), random(150, 230), random(150, 255));
   this.spread1 = random(-spreadRadius, spreadRadius);
   this.spread2 = random(-spreadRadius, spreadRadius);
   this.spread3 = random(-spreadRadius, spreadRadius);
   this.spread4 = random(-spreadRadius, spreadRadius);
 }

 void move() {

   this.xangle += this.xspeed;
   this.yangle += this.yspeed;
   this.xp = (float) Math.sin(this.xangle * Pi180) * this.xradius + 300;
   this.yp = (float) Math.cos(this.yangle * Pi180) * this.yradius + 300;

   fill(this.col);

   ellipse(xp, yp, this.r, this.r); // main point
   ellipse(xp + (this.spread1 + random(shimmer)), yp + (this.spread2 + random(shimmer)), this.r, this.r);
   ellipse(xp - (this.spread3 + random(shimmer)), yp + (this.spread4 + random(shimmer)), this.r, this.r);
   ellipse(xp - (this.spread4 + random(shimmer)), yp - (this.spread3 + random(shimmer)), this.r, this.r);
   ellipse(xp + (this.spread2 + random(shimmer)), yp - (this.spread1 + random(shimmer)), this.r, this.r);
 }
}
Re: Class: Trying to combine two sketches together
Reply #2 - May 2nd, 2010, 11:23pm
 
What do you mean by "combine"? Run them at the same time? Toggle between them? Merge them somehow?
Re: Class: Trying to combine two sketches together
Reply #3 - May 4th, 2010, 9:06pm
 
...  
Sketch Browser should help.
http://openprocessing.org/visuals/?visualID=8251
Bit.craft came up with this awesome piece of code. It already allows you to select any sketch in sequence.

With minimal modification you can adjust it to select randomly or even simultaneously.

The current template version will be used for the Processing Combined Effort posted here
http://processing.org/discourse/yabb2/num_1266418665.html
Page Index Toggle Pages: 1