回溯。在python中从零开始再次迭代

2024-04-19 02:01:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python实现http://ahamnett.blogspot.com/2012/10/funnel-algorithm.html。 但问题是关于迭代过程。如何用I=0和新列表开始迭代?你知道吗

其思想是遍历两个列表,一次遍历一个右列表和一个左列表。然后测量两点之间的角度,并将其与先前计算的角度进行比较。如果在某个时候计算的角度变大(漏斗变宽),我想用I=0和新的右和左列表重新开始迭代过程。 目前我没有得到任何输出。我认为问题出在calcuo()语句中,我希望def calcuo()重新开始。你知道吗

import math

def getAngle(a, b, c):
    ang = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
    return ang

triangles = [
    [(7, 10), (5, 15), (0, 0)], 
    [(5, 15), (7, 10), (8, 15)], 
    [(12, 10), (8, 15), (7, 10)], 
    [(16, 12), (8, 15), (12, 10)], 
    [(16, 12), (16, 19), (8, 15)], 
    [(8, 24), (8, 15), (16, 19)], 
    [(17, 25), (8, 24), (16, 19)], 
    [(19, 19), (17, 25), (16, 19)], 
    [(19, 19), (40, 19), (17, 25)], 
    [(17, 25), (40, 19), (42, 25)]
]

start = (5, 12)
goal = (33, 22)


def calc(path): 

    rights = [
        (5.0, 15.0), (8.0, 15.0), (8.0, 15.0),
        (8.0, 15.0), (8.0, 15.0), (8.0, 24.0),
        (17.0, 25.0), (17.0, 25.0), (17.0, 25.0)
    ]

    lefts = [
        (7.0, 10.0), (7.0, 10.0), (12.0, 10.0),
        (16.0, 12.0), (16.0, 19.0), (16.0, 19.0),
        (16.0, 19.0), (19.0, 19.0), (40.0, 19.0)
    ]

    rights.append(goal)
    lefts.append(goal)
    rig = rights[0]
    lef = lefts[0]
    node = start
    ang = getAngle(lef, node, rig)
    nodes_list = [node]

    def calcu():
        nonlocal rights
        nonlocal lefts
        while nodes_list[-1] != goal:
            for i in range(0, len(rights)-1):
                rig = rights[i+1]
                nonlocal lef
                nonlocal ang
                lef = lef
                ang2 = getAngle(lef, nodes_list[-1], rig)
                if ang2 <= ang:
                    rig = rig
                    lef = lefts[i+1]
                    ang = getAngle(lef, nodes_list[-1], rig)
                    if ang > ang2:
                        nodes_list.append(lefts[i])
                        rights = rights[i+1:]
                        lefts = lefts[i+1:]
                        i = 0
                        calcu()
                    else:
                        continue
                        return nodes_list
                else:
                    nodes_list.append(rights[i])
                    rights = rights[i+1:]
                    lefts = lefts[i+1:]
                    i = 0
                    calcu()
        return nodes_list


abc = calc(triangles)
print(abc)

在上面的代码中,我想得到输出((5,12),(8,15),(16,19),(33,22))。你知道吗


Tags: 列表defmathlist角度nodesrigappend
1条回答
网友
1楼 · 发布于 2024-04-19 02:01:36

有几件事要看。你的代码结构有点混乱,但这是可以修复的!首先,您没有得到任何输出,因为函数calc没有return语句。您还有一个从未调用过的内部函数calcu。你知道吗

重新开始,避免任何内部函数和全局/非局部的东西。。。不需要。我将从一个函数开始,这个函数包含一个原点、一个左列表和一个右列表,看看是否可以让它来计算各个角度。这将为另一个函数提供基本的构建块,以使用这些输出并基于这些返回生成路径。你知道吗

伪代码中类似这样的内容:

lefts = [ ... ]
rights = [ ... ]
origin = (x0, y0)

def find_funnel(origin, lefts, rights):
  # use the origin and walk through ALL the lefts and rights 
  # to get all the angles as proof-of-concept
  loop through all the lefts/rights:
    print (left, right, angle)   # <  use this for troubleshooting, then remove
  return (last_left, last_right)

让它运行,这样它穿过你所有的左边和右边,并吐出可信的角度,然后你可以改变该函数的停止条件适合的漏斗算法,这样当它运行时,它会适当地停止,并返回一个有效的漏斗的“最后左边和最后右边”的原点你发送它。你知道吗

相关问题 更多 >