Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用类生成分形树_Python_Svg - Fatal编程技术网

Python使用类生成分形树

Python使用类生成分形树,python,svg,Python,Svg,我想使用SVG路径对象生成一个分形树。树的一个分支应由一个分支对象表示。我的递归逻辑和收集路径有一些问题。对于depth=1代码应生成4个路径s,但我当前的代码只返回一个这样的路径。我怎样才能纠正这个问题 我的代码: import math class Branch: def __init__(self, pointxy1, pointxy2): self.pointXY1 = pointxy1 self.pointXY2 = pointxy2

我想使用SVG路径对象生成一个分形树。树的一个分支应由一个
分支
对象表示。我的递归逻辑和收集路径有一些问题。对于
depth=1
代码应生成4个
路径
s,但我当前的代码只返回一个这样的
路径
。我怎样才能纠正这个问题

我的代码:

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
导入数学
班支部:
定义初始化(self,pointxy1,pointxy2):
self.pointXY1=pointXY1
self.pointXY2=pointXY2
定义(自我):
返回(r“”)\
.format(self.pointXY1[0]、self.pointXY1[1]、self.pointXY2[0]、self.pointXY2[1])
def drawtree(自身、长度、角度、深度):
如果深度:
self.pointXY2[0]=self.pointXY1[0]+长度*(数学cos(数学弧度(角度)))
self.pointXY2[1]=self.pointXY1[1]+长度*(数学cos(数学弧度(角度)))
自抽式采油树(长度,角度-20,深度-1)
自绘制树(长度、角度、深度-1)
自绘制树(长度,角度+20,深度-1)
返回分支(self.pointXY1、self.pointXY2)
tree=[分支([400800],[400600])。drawtree(200,-90,1)]
对于树中的t:
打印t
下面是输出。它只有1条路径,而不是所需的4条路径

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

编辑:

这是我的非对象示例,它正在工作:

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)
导入数学
def牵引架(长度、角度、深度):
如果深度>=0:
x1=400
y1=800
x2=x1+长度*(数学余弦(数学弧度(角度)))
y2=y1+长度*(数学正弦(数学弧度(角度)))
打印(r'')。格式(x1、y1、x2、y2)
牵引架(长度,角度-20,深度-1)
绘图树(长度、角度、深度-1)
绘图树(长度,角度+20,深度-1)
抽油树(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"/>

结果:


构建一个平面列表,然后迭代以打印它:

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

构建一个平面列表,然后迭代以打印它:

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

您正在调用drawTree,但没有对返回值执行任何操作:

drawTree(lenght, angle - 20, depth - 1)
drawTree(lenght, angle, depth - 1)
drawTree(lenght, angle + 20, depth - 1)
所以返回的分支丢失了。它不会添加到您的树中


您的“非对象示例”之所以有效,是因为您正在drawTree函数中进行打印,所以每个分支都会打印一些内容。但是,您确实遇到了同样的问题,并且删除了返回值,只是您先打印了一些内容。

您正在调用drawTree,但没有对返回值执行任何操作:

drawTree(lenght, angle - 20, depth - 1)
drawTree(lenght, angle, depth - 1)
drawTree(lenght, angle + 20, depth - 1)
所以返回的分支丢失了。它不会添加到您的树中


您的“非对象示例”之所以有效,是因为您正在drawTree函数中进行打印,所以每个分支都会打印一些内容。尽管如此,您确实也遇到了同样的问题,并且删除了返回值,只是您先打印了一些内容。

您是要返回
self.drawtree
的结果吗?是的,所有递归结果。但是您不返回结果。
返回分支(self.pointXY1,self.pointXY2)
这不是它吗?@PeterWood我在我的问题中放了一个例子你想返回
self.drawtree
的结果吗?是的,所有的递归结果。但是你不返回结果。
返回分支(self.pointXY1,self.pointXY2)
这不是它吗?@PeterWood我在我的问题中放了一个例子好的,谢谢,但只有一个问题。值仍然是相同的
,这实际上是另一个问题。在新代码中,在创建分支之前不要更新
pointXY2
,但在打印之前要更新旧代码。@lukassz因为你有我的耳朵,并不意味着我要为你调试代码。写一个好的问题并贴出来。首先看看如何创建一个。在创建它的过程中,你可能会自己解决你的问题。好的,谢谢,它是有效的,但只有一个问题。值仍然是相同的
,这实际上是另一个问题。在新代码中,在创建分支之前不要更新
pointXY2
,但在打印之前要更新旧代码。@lukassz因为你有我的耳朵,并不意味着我要为你调试代码。写一个好的问题并贴出来。首先看看如何创建一个。在创建它的过程中,你可能会自己解决你的问题。