void BranchingExperiment_2App::update()
{    
    if (mouseIsPressed) {
        Branch subBranch;
        subBranch.setup( mMouseLoc, mMouseVel);
        mBranches.push_back( subBranch );
    }
    
    for (list<Branch>::iterator p = mBranches.begin(); p != mBranches.end(); ++p){
        p->update();
        
    }
    
    if( mSaveFrames ){
		writeImage( getHomeDirectory() + "branching_" + toString( getElapsedFrames() ) + ".png", copyWindowSurface() );
        
        mSaveFrames = false;
	}
}
Exemple #2
0
void Branch::setup(float _xPos, float _yPos,
           float _length, float _width,
           float _splitPercentage,
           float _maxSplitAngle,
           float _maxSubdivisions,
           float _maxSegments,
           float _segmentsSinceStart,
           float _angle,
           float _curvature,
           ofColor _color
//           int _counter
                   ){
    
//Set incoming values to the last branch values
    xPos = _xPos;
    yPos = _yPos;
    length = _length;
    width = _width;
    splitPercentage = _splitPercentage;
    maxSplitAngle = _maxSplitAngle;
    maxSubdivisions = _maxSubdivisions;
    maxSegments = _maxSegments;
    segmentsSinceStart = _segmentsSinceStart;
    angle = _angle;
    curvature = _curvature;
    color = _color;

//Check if the branch splits
    float r = ofRandomuf(); //gives avlue btwn 0 & 1
    
    if(r > splitPercentage && segmentsSinceStart != maxSegments){
        split = true;
    } else {
        split = false;
    }
    
//draw current branch
    ofSetColor(color);
    ofSetLineWidth(int(width));
    ofLine(xPos, yPos, xPos + length*cos(angle), yPos + length*sin(angle));
    
    
    
//update the values of existing branch
    xPos += length*cos(angle);
    yPos += length*sin(angle);
    width = ofRandom(0.4, 0.8)*width;
    length = ofRandom(0.6,0.8)*length;
    angle += curvature;
    curvature = ofRandom(0,2*PI/360);
    segmentsSinceStart++;
    
//recursion
    if(segmentsSinceStart <= maxSegments){
        
        if(!split){
            //do not split
            Branch subBranch;
            subBranch.setup(xPos, yPos, length, width, splitPercentage, maxSplitAngle, maxSubdivisions, maxSegments, segmentsSinceStart, angle, curvature, color);
        }else{
            //split occurs
            Branch subBranch1;
            float angle1 = angle + ofRandom(0,maxSplitAngle);
            subBranch1.setup(xPos, yPos, length, width, splitPercentage, maxSplitAngle, maxSubdivisions, maxSegments, segmentsSinceStart, angle1, curvature, color);
            
            Branch subBranch2;
            float angle2 = angle -ofRandom(0, maxSplitAngle);
            subBranch2.setup(xPos, yPos, length, width, splitPercentage, maxSplitAngle, maxSubdivisions, maxSegments, segmentsSinceStart, angle2, curvature, color);
            
        }
    }
    
    
}
//--------------------------------------------------------------
void Branch::setup(float _xPos,
                   float _yPos,
                   float _width,
                   float _length,
                   float _splitPercentage,
                   float _maxSplitAngle,
                   int _maxSubdivisions,
                   int _maxSegments,
                   int _segmentsSinceStart,
                   float _angle,
                   float _curvature,
                   ofColor _color,
                   int numberOfBobs){
    
    //===========================================
    // set incoming values based on what was passed in from previous instance of branch
    //===========================================
    
    xPos = _xPos;
    yPos = _yPos;
    width = _width;
    length = _length;
    splitPercentage = _splitPercentage;
    maxSplitAngle = _maxSplitAngle;
    maxSubdivsions = _maxSubdivisions;
    maxSegments = _maxSegments;
    segmentsSinceStart = _segmentsSinceStart;
    angle = _angle;
    curvature = _curvature;
    color = _color;
    
    //===========================================
    //does the branch split??
    //===========================================
    if(ofRandom(0, 1) > splitPercentage && segmentsSinceStart != maxSegments){ //if random number between 0-1 is greater than split %, then split ... and never split on last branch
        split = true;
    } else{
        split = false;
    }
    
    //===========================================
    // create some leaves!!!
    //===========================================
    
    numLeaves = (int)ofRandom(0,4);
    if(segmentsSinceStart == maxSegments){ // make sure there are leaves at the end of the final branch
        numLeaves = (int)ofRandom(3,5);
    }
    if(segmentsSinceStart == 0){ // no leaves at the end of the first branch (aka the Trunk!)
        numLeaves = 0;
    }
    
    for(int i = 0; i < numLeaves; i++){
        Leaf tempLeaf;
        tempLeaf.setup(angle);
        leaves.push_back(tempLeaf);
    }
    
    //===========================================
    // SOME FEEDBACK
    //===========================================
    
    //    cout << "New Branch" << endl;
    //    cout << "Segment Count = ";
    //    cout << segmentsSinceStart << endl;
    
    //===========================================
    //draw current branch instance
    //===========================================
    
    ofSetColor(color);
    ofSetLineWidth((int)width);
    ofLine(xPos, yPos, xPos + length*cos(angle), yPos + length*sin(angle)); //draw the branch segnment
    
    //  ATTEMPT TO USE RECTANGLES INSTEAD!!!
    //    ofPushMatrix();
    //        ofTranslate(xPos + length*cos(angle), yPos + length*sin(angle));
    //        ofRotateZ(angle * (180/PI));
    //        ofRect(0, 0 - length/2, length, width);
    //    ofPopMatrix();
    
    //===========================================
    //draw leaves on current branch
    //===========================================
    
    for(int i = 0; i < numLeaves; i++){
        leaves[i].draw(xPos + length*cos(angle), yPos + length*sin(angle));
    }
    
    //===========================================
    //update variables to pass to next (sub) branch
    //===========================================
    
    xPos += length*cos(angle);
    yPos += length*sin(angle);
    width = width*(ofRandom(0.5,0.8));
    length = length*(ofRandom(0.7, 0.9));
    segmentsSinceStart += 1;
    angle += curvature;
    curvature += ofRandom(0, (float)(2*PI)/360.0);
    
    //===========================================
    //RECURSION!
    //===========================================
    
    if (segmentsSinceStart <= maxSegments) {
        if(!split){ //if the branch does not split... just create one more branch to keep going (in roughly the same direction)
            Branch subBranch;
            subBranch.setup(xPos, yPos, width, length, splitPercentage, maxSplitAngle, maxSubdivsions, maxSegments, segmentsSinceStart, angle, curvature, color, 5);
        } else { //if the branch does split!! create two sub-branches
            cout << "split" << endl; //some feedback
            Branch subBranch1;
            float angle1 = angle + ofRandom(0, maxSplitAngle);
            Branch subBranch2;
            float angle2 = angle - ofRandom(0, maxSplitAngle);
            
            subBranch1.setup(xPos, yPos, width, length, splitPercentage, maxSplitAngle, maxSubdivsions, maxSegments, segmentsSinceStart, angle1, curvature, color, 5);
            
            subBranch2.setup(xPos, yPos, width, length, splitPercentage, maxSplitAngle, maxSubdivsions, maxSegments, segmentsSinceStart, angle2, curvature, color, 5);
        }
    }
}
//--------------------------------------------------------------
void Branch::setup(float _xPos,
                   float _yPos,
                   float _width,
                   float _length,
                   float _splitPercentage,
                   int _maxSubdivisions,
                   int _maxSegments,
                   int _segmentsSinceStart,
                   float _angle,
                   float _curvature,
                   ofColor _color,
                   int numberOfBobs){
    
    // set incoming values based on what was passed in from previous instance of branch
    xPos = _xPos;
    yPos = _yPos;
    width = _width;
    length = _length;
    splitPercentage = _splitPercentage;
    maxSubdivsions = _maxSubdivisions;
    maxSegments = _maxSegments;
    segmentsSinceStart = _segmentsSinceStart;
    angle = _angle;
    curvature = _curvature;
    color = _color;
    
    myLeaf.setup();
    
    // store branch in MAIN vector<> of branches...
    
    cout << "New Branch" << endl;
    cout << "Segment Count = ";
    cout << segmentsSinceStart << endl;
    
    //    //draw current branch instance
    ofSetColor(color);
    ofSetLineWidth((int)width);
    ofLine(xPos, yPos, xPos + length*cos(angle), yPos + length*sin(angle)); //draw the branch segnment
    
    //update variables to pass to next sub branch
    xPos += length*cos(angle);
    yPos += length*sin(angle);
    width = width*(ofRandom(0.5,0.8));
    segmentsSinceStart += 1;
    angle += curvature;
    curvature += ofRandom(0, (float)(2*PI)/360.0);
    
    myLeaf.draw();
    
    //RECURSION!
    if (segmentsSinceStart <= maxSegments) {
        //roll the dice... will the branch split into sub-branches???
        if(ofRandom(0, 1) > splitPercentage){ //NO
            Branch subBranch;
            subBranch.setup(xPos, yPos, width, length, splitPercentage, maxSubdivsions, maxSegments, segmentsSinceStart, angle, curvature, color, 5);
        } else { //YES!!!
            cout << "split" << endl;
            Branch subBranch1;
            float angle1 = angle + ofRandom(0, 3*(PI/8));
            Branch subBranch2;
            float angle2 = angle - ofRandom(0, 3*(PI/8));
            
            subBranch1.setup(xPos, yPos, width, length, splitPercentage, maxSubdivsions, maxSegments, segmentsSinceStart, angle1, curvature, color, 5);
            
            subBranch2.setup(xPos, yPos, width, length, splitPercentage, maxSubdivsions, maxSegments, segmentsSinceStart, angle2, curvature, color, 5);
        }
    }
}
//--------------------------------------------------------------
void Branch::setup(
                   float _xPos,
                  float _yPos,
                  float _width,
                  float _length,
                  float _splitPercentage,
                  int _maxSubdivisions,
                  int _maxSegments,
                  int _segmentsSinceStart,
                  float _angle,
                  float _curvature,
                   ofColor _color){
    
    // set incoming values based on what was passed in from previous instance of branch:
    xPos = _xPos;
    yPos = _yPos;
    width = _width;
    length = _length;
    splitPercentage = _splitPercentage;
    maxSubdivsions = _maxSubdivisions;
    maxSegments = _maxSegments;
    segmentsSinceStart = _segmentsSinceStart;
    angle = _angle;
    curvature = _curvature;
    color = _color;
    
    myLeaf.setup();
    
    //print line - counsol print:
    cout << "New Branch" << endl;
    cout << "Segment Count = ";
    cout << "segmentSinceStart" << endl;
    
    //draw current branch:
    ofSetColor(color);
    ofSetLineWidth((int)width);

    //o que é isso mesmo? posição e angulo dos branches? ofLine é a função para criar lines?
    ofLine(xPos, yPos, xPos + length * cos(angle), yPos + length * sin(angle));
    
    //definições:
    //pq q isso não está no draw?
    xPos += length * cos(angle); //aqui o angle é angle pq pode ser qualquer um? se eu quisesse que fosse um angle especifico eu colocaria numero?
    yPos += length * sin(angle);
    width = width * (ofRandom(0.5, 0.8)); // variação do tamanho dos branches
    segmentsSinceStart += 1; // esse é para acrescentar mais um branch?
    angle += curvature;
    curvature += ofRandom (0, (float)(2* PI)/360);
    
    myLeaf.draw(xPos, yPos);

    //recursion:
    if ( segmentsSinceStart <= maxSegments) {
        if (ofRandom (0,1) > splitPercentage) {
            Branch subBranch;
            subBranch.setup(xPos, yPos, width, length, splitPercentage, maxSubdivsions, maxSegments, segmentsSinceStart, angle, curvature, color);
        } else {
            cout << "split" << endl;
            Branch subBranch1;
            float angle1 = angle + ofRandom(0, PI/2);
            Branch subBranch2;
            float angle2 = angle - ofRandom(0, PI/2);
            subBranch1.setup(xPos, yPos, width,length,splitPercentage, maxSubdivsions, maxSegments, segmentsSinceStart, angle1,curvature,color);
            subBranch2.setup(xPos, yPos, width,length,splitPercentage, maxSubdivsions, maxSegments,segmentsSinceStart, angle2,curvature,color);
        }
    }
}
//--------------------------------------------------------------
void Branch::setup(float _xPos,
                   float _yPos,
                   float _width,
                   float _length,
                   float _split,
                   int _maxSubdivisons,
                   int _maxSegments,
                   int _segmentSinceStart,
                   float _angle,
                   float _curvature,
                   ofColor _color){
    
    
    xPos = _xPos;
    yPos = _yPos;
    width = _width;
    length = _length;
    split = _split;
    maxSubdivisons = _maxSubdivisons;
    maxSegments = _maxSegments;
    segmentSinceStart = _segmentSinceStart;
    angle = _angle;
    curvature = _curvature;
    color = _color;
    
    //store brac=k in main vector<> of Branches ...
    
    cout << "new Branch" <<endl;
    cout << "Segment Count=" ;
    cout << segmentSinceStart<< endl;
    //draw current branch instance
    ofSetColor(color);
    ofSetLineWidth((int)width);
    ofLine(xPos, yPos, xPos + length*cos(angle),yPos+length*sin(angle));
    
    //update varibles to pass to next sub branch
    xPos += length * cos(angle);
    yPos += length * sin(angle);
    width = width * (ofRandom(0.5,0.8));
    segmentSinceStart += 1;
    angle += curvature;
    curvature += ofRandom(0,(float)(2*PI)/360.0);
    
   
    
    
    //Recurison
    if (segmentSinceStart <= maxSegments) {
        // split if chances
        if (ofRandom(0,1) > split){
            Branch subBranch;
            subBranch.setup(xPos, yPos, width, length, split, maxSubdivisons, maxSegments, segmentSinceStart, angle , curvature, color);
        }else{
            cout << "split" << endl;
            Branch subBranch1;
            float angle1 = angle + ofRandom(0, 3*(PI/8));
            Branch subBranch2;
            float angle2 = angle + ofRandom(0, 3*(PI/8));
            
            subBranch1.setup(xPos, yPos,width,length,split,maxSubdivisons,maxSegments,segmentSinceStart, angle1, curvature,color);
            subBranch2.setup(xPos, yPos, width, length, split, maxSubdivisons, maxSegments,segmentSinceStart, angle2 , curvature,color);
        
        }
    }
    
    
    
    
    

}