2017-03-21 3 views
0

私はSVGパスオブジェクトを使ってフラクタルツリーを生成したいと思います。ツリーの1つのブランチは、1つのBranchオブジェクトで表される必要があります。私は再帰ロジックに問題があり、pathを収集しています。 depth=1の場合、コードは4 pathを生成するはずですが、私の現在のコードはそのようなものを返すだけですpath。どうすれば修正できますか?Pythonはクラスを使ってフラクタルツリーを生成します

マイコード:

import math 


class Branch: 

    def __init__(self, pointxy1, pointxy2): 
     self.pointXY1 = pointxy1 
     self.pointXY2 = pointxy2 

    def __str__(self): 
     return (r'<path d="M {} {} L {} {}"'' ' 
       'stroke="rgb(100,60,0)" stroke-width="35"/>')\ 
      .format(self.pointXY1[0], self.pointXY1[1], self.pointXY2[0], self.pointXY2[1]) 

    def drawtree(self, lenght, angle, depth): 

     if depth: 
      self.pointXY2[0] = self.pointXY1[0] + lenght * (math.cos(math.radians(angle))) 
      self.pointXY2[1] = self.pointXY1[1] + lenght * (math.cos(math.radians(angle))) 

      self.drawtree(lenght, angle - 20, depth - 1) 
      self.drawtree(lenght, angle, depth - 1) 
      self.drawtree(lenght, angle + 20, depth - 1) 

     return Branch(self.pointXY1, self.pointXY2) 

tree = [Branch([400, 800], [400, 600]).drawtree(200, -90, 1)] 

for t in tree: 
    print t 

そして、次のように出力されます。

import math 


def drawTree(lenght, angle, depth): 

    if depth >= 0: 

     x1 = 400 
     y1 = 800 

     x2 = x1 + lenght * (math.cos(math.radians(angle))) 
     y2 = y1 + lenght * (math.sin(math.radians(angle))) 

     print (r'<path d="M {} {} L {} {}"'' stroke="rgb(100,60,0)" stroke-width="35"/>').format(x1, y1, x2, y2) 

     drawTree(lenght, angle - 20, depth - 1) 
     drawTree(lenght, angle, depth - 1) 
     drawTree(lenght, angle + 20, depth - 1) 


drawTree(200, -90, 1) 

出力:

<path d="M 400 800 L 400.0 600.0" stroke="rgb(100,60,0)" stroke-width="35"/> 
<path d="M 400 800 L 331.595971335 612.061475843" stroke="rgb(100,60,0)" stroke-width="35"/> 
<path d="M 400 800 L 400.0 600.0" stroke="rgb(100,60,0)" stroke-width="35"/> 
<path d="M 400 800 L 468.404028665 612.061475843" stroke="rgb(100,60,0)" stroke-width="35"/> 

結果をこれは、それが働いている私の非対象の一例である

:それは代わりに希望4.

<path d="M 400 800 L 400 600" stroke="rgb(100,60,0)" stroke-width="35"/> 

EDITの唯一の1パスです:

enter image description here

+0

あなたは 'self.drawtree'の結果を返すつもりですか? –

+0

はい、すべての再帰結果です。 – lukassz

+0

しかし結果は返されません。 –

答えて

1

フラットなリストを作成し、それを印刷し、それを繰り返す:

def drawtree(self, lenght, angle, depth): 
    result = [] 
    if depth: 
     self.pointXY2[0] = self.pointXY1[0] + lenght * (math.cos(math.radians(angle))) 
     self.pointXY2[1] = self.pointXY1[1] + lenght * (math.cos(math.radians(angle))) 

     result.extend(self.drawtree(lenght, angle - 20, depth - 1)) 
     result.extend(self.drawtree(lenght, angle, depth - 1)) 
     result.extend(self.drawtree(lenght, angle + 20, depth - 1)) 

    result.append(Branch(self.pointXY1, self.pointXY2)) 
    return result 
+0

ありがとう、それは動作しますが、唯一の問題です。値は依然として同じです。 ' – lukassz

+0

これは本当に別のものです別の時間の質問。あなたの新しいコードでは 'Branch'を作成する前に' pointXY2'を更新しませんが、印刷前に古いコードで行います。 –

+0

これを修正するには? – lukassz

1

あなたはdrawTreeにこれらの呼び出しを作っているが、あなたは、戻り値で何もしていません。

drawTree(lenght, angle - 20, depth - 1) 
drawTree(lenght, angle, depth - 1) 
drawTree(lenght, angle + 20, depth - 1) 

したがって、返された分岐はちょうど失われます。あなたのツリーには追加されません。

「オブジェクト以外の例」が機能するように見える理由は、drawTree関数内で印刷を実行しているため、すべてのブランチに対して印刷されたものが得られるからです。あなたは本当にそこに同じ問題を抱えていて、戻り値を落としています。それは最初に何かを印刷することです。

関連する問題