2015-06-04 39 views
-1

我想在處理中繪製分形樹,但我不知道如何製作它。我的想法是繪製一棵二叉樹。但樹必須有更多的分支。我不知道如何編碼。請幫助我,謝謝。現在我畫了一棵2D樹,但是我的老闆要求我將它改爲3D。這是我的代碼,我不知道如何製作它。而且我是一個新人處理程序。請幫助我。通過處理繪製分形樹

void tree(float treeLength, float strokeWeight, int currentTreeStep) 
{ 
    if (currentTreeStep < maxTreeSteps) { 

     if(currentTreeStep >= 8 && currentTreeStep <=10) 
      stroke(#558351); //set color light green 
     else{ 
      if(currentTreeStep >10 && currentTreeStep <=20) 
       stroke(#199B0E); //set hard green   
      else{ 
       stroke(#311919);//set branch color 
      } 
     } 
     strokeCap(PROJECT); 
     strokeWeight(strokeWeight); 
     line(0, 0, 0, -treeLength); 
     translate(0, -treeLength); 

     strokeWeight *= 0.5; 
     treeLength *= 0.75; 

     if (treeLength > 1) { 
      pushMatrix(); 
      rotate(radians(branchAngle)); 
      tree(treeLength, strokeWeight, currentTreeStep + 1); 
      popMatrix(); 

      pushMatrix(); 
      rotate(-radians(branchAngle)); 
      tree(treeLength, strokeWeight, currentTreeStep + 1); 
      popMatrix(); 
     } 
    } 
} 
+1

其中的一部分是給你的麻煩?你的[MCVE](http://stackoverflow.com/help/mcve)在哪裏?請注意,Processing編輯器附帶了大量的例子,其中包括幾個關於分形樹的例子。 –

+0

感謝您的幫助,我通過Google修復了二維樹。但是如何將其更改爲3D還有一個問題。 – WangYao

回答

0

通常它不是直觀的人們可能從2D(三角大部分)移動到3D(線性代數:向量和矩陣乘法)。

看看這example從我做的課程之一。

enter image description here

希望你還可以看到在行動的3D矩陣乘法部分以及

+0

我在我的處理中運行你的代碼,但它不能運行,我修正了一些問題,並且無法運行。我必須將Vector更改爲PVector,但編譯器會說「方法get(float []) Pvector不適用於參數(float)「(我在Processing 3.0a5中運行它)。並且我的代碼被更改了。 for(int i = 0; i WangYao

+0

@WangYao Vector大致指可調整大小/動態數組的[java.util.Vector](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html)。請不要用PVector替換它。只需添加下面這行:''import java.util.Vector;'''。處理2.0和更新版本刪除導入到'''java.util。*'''這就是爲什麼你會得到錯誤 –

+0

非常感謝,我通過你的指導修復它,並再次感謝你。對不起我的聲望沒有達到15.我不能投票給你的答案。對不起。 – WangYao