Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Recursive Tree in Sine Motion // Gerhard Funk www.gerhard-funk.de
- PVector startPos = new PVector ();
- float startDistance = 100;
- int trunkAngle=-90;
- float branchesAngle=0;
- float angleAdd=0;
- float startLinThick=20;
- int iterNum=0;
- int iterLimit=10;
- void setup() {
- size (800,800);
- smooth();
- frameRate(24);
- startPos.set(width/2,height/2,0.0);
- }
- void draw () {
- background (235);
- int sunAngle = (frameCount%360)*4;
- int sunRadius = 300;
- float sunx = (cos(radians(sunAngle+180)) * sunRadius) + startPos.x;
- float suny = (sin(radians(sunAngle+180)) * sunRadius) + startPos.y;
- ellipseMode(CENTER);
- fill(0);
- ellipse(startPos.x, startPos.y, 80, 80); //drawing black Circle at center
- fill(255,0,0);
- ellipse (sunx, suny, 50, 50); //drawing red Circle (Sun)
- startDistance = round(suny-height/2)*-0.6;
- tree (startPos,trunkAngle,startDistance,startLinThick,iterNum);
- treeBezier(startPos,trunkAngle,startDistance,startLinThick,iterNum,false);
- branchesAngle = 75 * sin(radians(sunAngle));
- fill(0,255,0);
- ellipse(startPos.x, startPos.y, 50, 50); //drawing green circle
- }
- void tree (PVector startP, float angle, float distance, float linesThick, int _iterNum) {
- PVector endP = new PVector();
- endP.x=cos(radians(angle))*distance+startP.x;
- endP.y=sin(radians(angle))*distance+startP.y;
- //DRAW
- strokeWeight (linesThick);
- stroke(0);
- line(startP.x,startP.y,endP.x,endP.y);
- //RECURSION
- linesThick=linesThick*0.625;
- if (linesThick<=0) {linesThick=1;}
- angleAdd +=0.1;
- distance-=1;
- _iterNum+=1;
- if (_iterNum>=iterLimit) {return;}
- tree (endP,angle+branchesAngle,distance*0.625,linesThick, _iterNum);
- tree (endP,angle-branchesAngle,distance*0.625,linesThick, _iterNum);
- }
- void treeBezier (PVector startP, float angle, float distance, float linesThick, int _iterNum, boolean mirror) {
- PVector endP = new PVector();
- PVector startT = new PVector();
- PVector endT = new PVector();
- float angleT = 30;
- float lengthT = 3; //attention, never set to zero, otherways causes “division by zero”
- if(mirror) {angleT = angleT*-1;}
- endP.x=cos(radians(angle))*distance+startP.x;
- endP.y=sin(radians(angle))*distance+startP.y;
- startT.x=cos(radians(angle+angleT))*distance/lengthT+startP.x;
- startT.y=sin(radians(angle+angleT))*distance/lengthT+startP.y;
- endT.x=cos(radians(angle+angleT))*distance/lengthT*-1+endP.x;
- endT.y=sin(radians(angle+angleT))*distance/lengthT*-1+endP.y;
- //DRAW
- strokeWeight (linesThick/3);
- stroke(0,255,0);
- noFill();
- bezier(startP.x, startP.y, startT.x, startT.y, endT.x, endT.y, endP.x, endP.y);
- //RECURSION
- linesThick=linesThick*0.625;
- if (linesThick<=0) {linesThick=1;}
- angleAdd +=0.1;
- distance-=1;
- _iterNum+=1;
- if (_iterNum>=iterLimit) {return;}
- treeBezier (endP,angle+branchesAngle,distance*0.625,linesThick, _iterNum, true);
- treeBezier (endP,angle-branchesAngle,distance*0.625,linesThick, _iterNum, false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement