알고리즘 2008/03/18 01:10

brenches

사용자 삽입 이미지


package
{
 import flash.display.*;
 import flash.utils.*;
 
 public class branches extends MovieClip {
   // Stem Generator by Lifaros
   var branchNum:Number = 20;//number of branches or stems
   var xcenter:Number = 275;//horizontal center
   var xoffset:Number = 150;//max distance from horizontal center
  public function branches():void{
   
   // branch duplication
   for (var i=0; i<branchNum; i++) {
    var mybranch:MovieClip = new MovieClip();
    mybranch.x = xcenter + Math.random() * 2 * xoffset - xoffset;
    mybranch.y = 400;
    addChild(mybranch);
    // in leafs
    var points:Number = 30 + Math.floor(Math.random() * 40);
    // number of point on each branch or stem
    var leaves:Number = 13;
    // number of leaves on each branch
    var py:Array = new Array();
    // data array
    var px:Array = new Array();
    var dy:Number = 5;
    // vertical distance between each branch point
    var offset:Number = 0.007;
    // random factor
    py[0] = 0;
    px[0] = 0;
    // data calculation
    for (var j=1; j <= points; j++) {
     py[j] = -dy * j;
     px[j] = px[j - 1] + j * offset * (Math.random() * 21 - 10);
    }
    // branch points duplication and drawing
    for (j=0; j < points; j++) {
         
     var sx = px[j + 1] - px[j];
     var sy = py[j + 1] - py[j];
     var rotate:Number;
     if(j==0){
      rotate = 90;
     } else {
      rotate = Math.atan2(py[j-1] - py[j],px[j-1] - px[j])*180;
     }
     var myline:Shape = new line(0x00eeaa, 1.0, sx, sy, rotate);
     myline.x = px[j];
     myline.y = py[j];
     mybranch.addChild(myline);
    }
    // leaf duplication and drawing
    for (j=0; j < leaves; j++) {

     var tx = px[points - 2 * j];
     var ty = py[points - 2 * j];
     var xscale = 30 + 1 * j;
     var yscale = 10 + 1 * j;
     rotate = Math.random() * 180 - 180;
 
     var myleaf:Shape =  new leaf(0x00eeaa, 1.0, xscale, yscale, rotate);
     myleaf.x = tx;
     myleaf.y = ty;
     mybranch.addChild(myleaf);
 
    }
   }
  }
 }
}
import flash.display.Shape;
import flash.display.LineScaleMode;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.Shape;

class leaf extends Shape {
 public function leaf(color:uint,ah:Number, w:Number, h:Number,rot:Number) {
  rot = rot*Math.PI/180;
  var tX:Number = Math.cos(rot)*h;
  var tY:Number = Math.sin(rot)*h;
  var controlX:Number = tX/2 + Math.cos(rot-Math.PI/4)*w/2;
  var controlY:Number = tY/2 + Math.sin(rot-Math.PI/4)*w/2;
  graphics.beginFill(color,ah);
  graphics.moveTo(0,0);
  graphics.curveTo(controlX , controlY, tX, tY);
  controlX = tX/2 + Math.cos(rot+Math.PI/4)*w/2;
  controlY = tY/2 + Math.sin(rot+Math.PI/4)*w/2;
  graphics.curveTo(controlX , controlY, 1, 1);
  graphics.endFill();

  //return this;
 }
}

class line extends Shape {
 public function line(color:uint,ah:Number, w:Number, h:Number,rotate:Number) {
  rotate = rotate*Math.PI/180;
  var tX:Number = Math.cos(rotate)*h;
  var tY:Number = Math.sin(rotate)*h;
  var controlX:Number = tX/2 + Math.cos(rotate-Math.PI/4)*w/2;
  var controlY:Number = tY/2 + Math.sin(rotate-Math.PI/4)*w/2;
  graphics.lineStyle(2, 0xFFD700, 1, false, LineScaleMode.VERTICAL,CapsStyle.NONE, JointStyle.MITER, 10);
  graphics.moveTo(0,0);
  graphics.lineTo(tX, tY);
  //return this;
 }
}

//lifaros Code conversion