J2ME中实现lindermayer分形绘制

J2ME中实现lindermayer分形绘制

没什么好说的,直接看代码吧,如果有疑问,可以参考下面这个网站。
参考网站:集智俱乐部(Clustering Intelligence) http://www.swarmagents.com/
package lindermayersystem;
import javax.microedition.lcdui.Graphics;
import java.util.Stack;
/**
 *
 * <p>Title: Lindermayer System</p>
 * <p>Description: Lindermayer System demo </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: </p>
 * <p>转载请说明出处,http://royelee.spaces.live.com,Email:royelee_007#hotmail.com(change # into @)</p>
 * <p>负责分形系统的逻辑工作和绘制工作</p>
 *
 *  分形中绘制符号表示的含义
 *  F:表示在当前的位置画一条长为l的直线段。l是由用户事先任意设定的数值,表示基本线段的长度。
 *  +:表示逆时针旋转一个角θ,θ的数值由用户事先确定;
 *  -:表示顺时针旋转一个角θ;
 *  [:表示暂时保存当前的画图状态
 *  ]:表示提取出保存的画图状态。 “[”和“]”要成对的出现。
 *
 * @author roye
 * @version 1.0
 */
public class Lindermayer {
  public static final char DRAW_STRATEGY_F = ‘F’;
  public static final char DRAW_STRATEGY_PLUS = ‘+’;
  public static final char DRAW_STRATEGY_MINUS = ‘-‘;
  public static final char DRAW_STRATEGY_SQUARE_BRACKET_L = ‘[‘;
  public static final char DRAW_STRATEGY_SQUARE_BRACKET_R = ‘]’;
  public static final int DEFAULT_PATTERN = 19;//总的默认图案数
 
 
  private double perRotation;//绘制的单位旋转度
  private double perLength;//绘制的单位长度
  private PointData curPoint;//当前点
  private int startDepth;//绘制的深度
 
  private String startRule;//开始公理
  private String [][] drawRule;//绘制公式
  private int ruleNum;//公式长度
 
 
  /**
   * 点数据
   * <p>Title: Lindermayer System</p>
   * <p>Description: Lindermayer System demo </p>
   * <p>Copyright: Copyright (c) 2007</p>
   * <p>Company: </p>
   * @author roye
   * @version 1.0
   */
  class PointData{
    public int x; //绘制点坐标x
    public int y; //绘制点坐标y
    public int rotation; //当前绘制点旋转度
  }
 
  public Lindermayer() {
  }
 
  /**
   * 设置分形绘制的初始参数
   * @param drawRule String[][] 绘制策略
   * @param startRule String 绘制的公理(绘制第一步公式)
   * @param ruleNum int 策略数组有几行(在drawRule[][]中)
   * @param perRotation double 每一笔的单位旋转量
   * @param perLength double 每一笔的单位长度
   * @param x int 起点x坐标
   * @param y int 起点y坐标
   * @param r int 起点的旋转度
   */
  public void setParam(String [][] drawRule,String startRule,int ruleNum,double perRotation, double perLength, int x, int y, int r){
    this.drawRule = drawRule;
    this.startRule = startRule;
    this.ruleNum = ruleNum;
    this.perRotation = perRotation;
    this.perLength = perLength;
    curPoint = new PointData();
    curPoint.x = x;
    curPoint.y = y;
    curPoint.rotation = r;
  }
 
  /**
   * 设置绘制的起始点x坐标,y坐标
   * @param x int
   * @param y int
   */
  public void setStartPosition(int x, int y){
    if(curPoint != null){
      curPoint.x = x;
      curPoint.y = y;
    }
  }
 
  /**
   * 获取开始绘制的公理(绘制第一步公式)
   * @return String
   */
  public String getStartingInstruction(){
    return startRule;
  }
 
  /**
   * 设置绘制的深度
   * @param d int
   */
  public void setDepth(int d){
    startDepth = d;
  }
 
  /**
   * 返回绘制的深度
   * @return int
   */
  public int getDepth(){
    return startDepth;
  }
 
 
  /**
   * 按照表达式说明的方式绘制分形图形
   * @param g Graphics
   * @param instruction String 绘制图形的表达式
   * @param depth int 递归调用的层次
   */
  public void draw(Graphics g, String instruction, int depth ){
    //如果是递归到了第0层,则退出
    if(depth == 0){
      return ;
    }
   
    Stack statusRecord = new Stack();
    int i,j;
   
     //深入到表达式中,如果有递归的符号出现,则递归调用
    for (j = 0; j < instruction.length(); j++) {
      //当前绘制图形的表达式中的字符
      int curRuleChar = instruction.charAt(j);
      for (i = 0; i < ruleNum; i++) {       
        //等式左端出现相同符号
        if (drawRule[i][0].charAt(0) == curRuleChar) {
          //递归调用
          draw(g, drawRule[i][1], depth – 1);
          break;
        } 
      }
     
      switch (curRuleChar) {
        case DRAW_STRATEGY_F:
          //绘制直线
          double rad = 2 * Math.PI * curPoint.rotation / 360; // 角度转换
          double p = perLength * Math.cos(rad);
          double q = perLength * Math.sin(rad);
          double bx = curPoint.x + p;
          double by = curPoint.y + q;
          g.drawLine(curPoint.x, curPoint.y, (int) bx, (int) by);
          curPoint.x = (int) bx;
          curPoint.y = (int) by;
          break;
        case DRAW_STRATEGY_PLUS:
          curPoint.rotation += perRotation;
          break;
        case DRAW_STRATEGY_MINUS:
          curPoint.rotation -= perRotation;
          break;
        case DRAW_STRATEGY_SQUARE_BRACKET_L:
          //记录当前绘制点的数据
          PointData pd = new PointData();
          pd.x = curPoint.x;
          pd.y = curPoint.y;
          pd.rotation = curPoint.rotation;
          statusRecord.push(pd);
          break;
        case DRAW_STRATEGY_SQUARE_BRACKET_R:
          //恢复之前绘制点的数据
          curPoint = (PointData) statusRecord.pop();
          break;
      }
    }   
  }
 
  /**
   * 装载内置的参数
   * @param k int 参数编号
   * @return true 装载成功 false 装载失败
   */
  public boolean loadDeafultParam(int k){
    int pStartX,pStartY;
    int direction_init;
    double lengthF;
    double rotation;
    int StartDepth;
    int ruleNumber;
    String sStart;
    String [][] sRule = new String[10][2];       
    if (k == 0) { //斜草
      pStartX = 340;
      pStartY = 20;
      direction_init = 60;
      lengthF = 3.3;
      rotation = -3;
      StartDepth = 7;
      ruleNumber = 2;
      sStart = "G";
      sRule[0][0] = "G";
      sRule[0][1] = "GFX[+++++GFG][—–GFG]";
      sRule[1][0] = "X";
      sRule[1][1] = "F-XF";
    }
    else if (k == 1) {
      pStartX = 200;
      pStartY = 50;
      direction_init = 0;
      lengthF = 6;
      rotation = -60;
      StartDepth = 6;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "–FXF++FXF++FXF–";
      sRule[1][0] = "F";
      sRule[1][1] = "FF";
      //reinit();
    }
    else if (k == 2) {
      pStartX = 200;
      pStartY = 20;
      direction_init = 90;
      lengthF = 4.5;
      rotation = 30;
      StartDepth = 5;
      ruleNumber = 1;
      sStart = "F";
      sRule[0][0] = "F";
      sRule[0][1] = "F[+F[+F][-F]F][-F[+F][-F]F]F[+F][-F]F";
      //reinit();
    }
    else if (k == 3) {
      pStartX = 200;
      pStartY = 210;
      direction_init = -90;
      lengthF = 9.5;
      rotation = -18;
      StartDepth = 13;
      ruleNumber = 4;
      sStart = "K";
      sRule[0][0] = "S";
      sRule[0][1] = "[+++G][—H]FFS";
      sRule[1][0] = "G";
      sRule[1][1] = "+G[-FH]F";
      sRule[2][0] = "H";
      sRule[2][1] = "-H[+FG]F";
      sRule[3][0] = "K";
      sRule[3][1] = "FSF";
      //reinit();
    }
    else if (k == 4) { //枝
      pStartX = 200;
      pStartY = 5;
      direction_init = 90;
      lengthF = 1.5;
      rotation = -25.7341;
      StartDepth = 6;
      ruleNumber = 1;
      sStart = "F";
      sRule[0][0] = "F";
      sRule[0][1] = "F[+F]F[-F]F";
      //reinit();
    }
    else if (k == 5) { //星
      pStartX = 15;
      pStartY = 200;
      direction_init = 0;
      lengthF = 4.5;
      rotation = -60;
      StartDepth = 5;
      ruleNumber = 1;
      sStart = "F";
      sRule[0][0] = "F";
      sRule[0][1] = "F-F++F-F";
      //reinit();
    }
    else if (k == 6) { //蒲公英
      pStartX = 200;
      pStartY = 10;
      direction_init = 90;
      lengthF = 0.37;
      rotation = 30;
      StartDepth = 10;
      ruleNumber = 2;
      sStart = "Y";
      sRule[0][0] = "X";
      sRule[0][1] = "X[-FFF][+FFF]FX";
      sRule[1][0] = "Y";
      sRule[1][1] = "YFX[+Y][-Y]";
      //reinit();
    }
    else if (k == 7) { //灌木丛
      pStartX = 250;
      pStartY = 20;
      direction_init = 90;
      lengthF = 3.5;
      rotation = -30;
      StartDepth = 6;
      ruleNumber = 1;
      sStart = "F";
      sRule[0][0] = "F";
      sRule[0][1] = "FF-[-F+F+F]+[+F-F-F]";
      //reinit();
    }
    else if (k == 8) { //棕榈
      pStartX = 200;
      pStartY = 20;
      direction_init = 90;
      lengthF = 5.6;
      rotation = -18;
      StartDepth = 12;
      ruleNumber = 5;
      sStart = "SLFFF";
      sRule[0][0] = "S";
      sRule[0][1] = "[+++H][—G]TS";
      sRule[1][0] = "G";
      sRule[1][1] = "+H[-G]L";
      sRule[2][0] = "H";
      sRule[2][1] = "-G[+H]L";
      sRule[3][0] = "T";
      sRule[3][1] = "TL";
      sRule[4][0] = "L";
      sRule[4][1] = "[-FFF][+FFF]F";
      //reinit();
    }
    else if (k == 9) { //开花的草
      pStartX = 200;
      pStartY = 10;
      direction_init = 90;
      lengthF = 3;
      rotation = -30;
      StartDepth = 8;
      ruleNumber = 2;
      sStart = "G";
      sRule[0][0] = "G";
      sRule[0][1] = "[+FGF][-FGF]XG";
      sRule[1][0] = "X";
      sRule[1][1] = "XFX";
      //reinit();
    }
    else if (k == 10) { //灌木丛
      pStartX = 370;
      pStartY = 30;
      direction_init = 90;
      lengthF = 2;
      rotation = -1.2;
      StartDepth = 6;
      ruleNumber = 1;
      sStart = "F";
      sRule[0][0] = "F";
      sRule[0][1] =
          "F[+++++++++++++++++++++++++F]-F[————————-F]F";
      //reinit();
    }
    else if (k == 11) { //杨柳
      pStartX = 170;
      pStartY = 0;
      direction_init = 90;
      lengthF = 7;
      rotation = -22.5;
      StartDepth = 5;
      ruleNumber = 1;
      sStart = "F";
      sRule[0][0] = "F";
      sRule[0][1] = "FF+[+F-F-F]-[-F+F+F]";
      //reinit();
    }
    else if (k == 12) { //Juliet集
      pStartX = 95;
      pStartY = 250;
      direction_init = 0;
      lengthF = 1;
      rotation = 90;
      StartDepth = 17;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "X+YF+";
      sRule[1][0] = "Y";
      sRule[1][1] = "-FX-Y";
      //reinit();
    }
    else if (k == 13) { //砖墙
      pStartX = 30;
      pStartY = 40;
      direction_init = 0;
      lengthF = 13;
      rotation = 90;
      StartDepth = 4;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "XFYFX+F+YFXFY-F-XFYFX";
      sRule[1][0] = "Y";
      sRule[1]

[1] = "YFXFY-F-XFYFX+F+YFXFY";
      //reinit();
    }
    else if (k == 14) { //砖砌X形
      pStartX = 80;
      pStartY = 90;
      direction_init = 0;
      lengthF = 3.5;
      rotation = 90;
      StartDepth = 4;
      ruleNumber = 1;
      sStart = "F+F+F+F";
      sRule[0][0] = "F";
      sRule[0][1] = "F+F-F-FF+F+F-F";
      //reinit();
    }
    else if (k == 15) { //三角绕三角
      pStartX = 200;
      pStartY = 10;
      direction_init = 0;
      lengthF = 1.7;
      rotation = -60;
      StartDepth = 6;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "–FXF++FXF++FXF–";
      sRule[1][0] = "F";
      sRule[1][1] = "FFF";
      //reinit();
    }
    else if (k == 16) { //一笔迷宫
      pStartX = 50;
      pStartY = 30;
      direction_init = 0;
      lengthF = 10;
      rotation = -90;
      StartDepth = 6;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "-YF+XFX+FY-";
      sRule[1][0] = "Y";
      sRule[1][1] = "+XF-YFY-FX+";
      //reinit();
    }
    else if (k == 17) { //树
      pStartX = 200;
      pStartY = 10;
      direction_init = 90;
      lengthF = 0.35;
      rotation = 30;
      StartDepth = 10;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "F[+X]F[-X]+X";
      sRule[1][0] = "F";
      sRule[1][1] = "FF";
      //reinit();
    }
    else if (k == 18) { //对称的树
      pStartX = 200;
      pStartY = 10;
      direction_init = 90;
      lengthF = 0.35;
      rotation = 30;
      StartDepth = 10;
      ruleNumber = 2;
      sStart = "X";
      sRule[0][0] = "X";
      sRule[0][1] = "F[+X][-X]FX";
      sRule[1][0] = "F";
      sRule[1][1] = "FF";
      //reinit();
    }else{
      return false;
    }
    String[][] s = new String[ruleNumber][2];
    for (int i = 0; i < ruleNumber; i++) {
      s[i][0] = sRule[i][0];
      s[i][1] = sRule[i][1];
    }
    setParam(s, sStart, ruleNumber, rotation, lengthF, pStartX, pStartY,
             direction_init);
    setDepth(StartDepth);
   
    return true;
  }

}

3 thoughts on “J2ME中实现lindermayer分形绘制

  1. 想在google上查查lindermayer分形绘制是什么概念,结果第一条结果就是你这个页面,世界真小啊。

  2. Hi,Do you need advertising displays, digital signages, advertising player and LCD displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.
    amberdigital Contact Us
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[i

  3. wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling -229287055965233

Leave a Reply

Your email address will not be published. Required fields are marked *