调用 math.dist() 时出现递归错误

问题描述 投票:0回答:1

当调用 math.dist() 来查找两点之间的距离时,我得到一个 RecursionError。 这是错误:

Traceback (most recent call last):
  File "/Users/myName/Desktop/2D Renderer.py", line 76, in <module>
    mainLoop()
  File "/Users/myName/Desktop/2D Renderer.py", line 74, in mainLoop
    canvas.after(10,mainLoop())
  File "/Users/myName/Desktop/2D Renderer.py", line 74, in mainLoop
    canvas.after(10,mainLoop())
  File "/Users/myName/Desktop/2D Renderer.py", line 74, in mainLoop
    canvas.after(10,mainLoop())
  [Previous line repeated 1018 more times]
  File "/Users/myName/Desktop/2D Renderer.py", line 71, in mainLoop
    result, color, x = checkRay([0,5],100)
  File "/Users/myName/Desktop/2D Renderer.py", line 48, in checkRay
    result, color = checkPoint(position)
  File "/Users/myName/Desktop/2D Renderer.py", line 56, in checkPoint
    result = sdCircle(position,shape.radius,shape.position)
  File "/Users/myName/Desktop/2D Renderer.py", line 66, in sdCircle
    return math.dist(position,circlePosition) - circleRadius;
RecursionError: maximum recursion depth exceeded while calling a Python object

我尝试更改代码以在无限

canvas.after(10,mainLoop())
循环中运行
while
,但这导致了冻结。这是我的代码:

from tkinter import *
from tkinter import ttk
from enum import Enum
import math
root = Tk()
root.title("2D Renderer")
canvas = Canvas(root, width=500, height=50, background='gray75')
canvas.grid(column=0,row=0)

class Player:
    def __init__(self,position,rotation):
        self.position = position
        self.rotation = rotation

player = Player([0,0],[0,1])

def setPixel(x,y,color):
    canvas.create_rectangle(x+3, y+3, x+3, y+3, outline="",fill=color)

def setPixel1D(x,color):
    canvas.create_line(x+3, 0, x+3, 50,fill='red')

class Shapes(Enum):
    CIRCLE = 1
    SQUARE = 2

class Shape:
    def __init__(self,shape: Shapes,position,radius,color):
        self.shape = shape
        self.position = position
        self.radius = radius
        self.color = color

shapes = [Shape(Shapes.CIRCLE,[0,100],50,'ff0000')]

def normalizeVector(vector):
    length = math.sqrt(vector[0] * vector[0] + vector[1] * vector[1])
    vector[0] /= length
    vector[1] /= length
    return vector

def checkRay(vector,max_steps):
    vector = normalizeVector(vector)
    position = player.position
    for i in range(max_steps):
        position[0] += vector[0]
        position[1] += vector[1]
        result, color = checkPoint(position)
        if result:
            return True, color, position[0] / position[1] - player.position[1]
    return False, None, None

def checkPoint(position):
    for shape in shapes:
        if shape.shape == Shapes.CIRCLE:
            result = sdCircle(position,shape.radius,shape.position)
            if result <= 0:
                return True, shape.color
            else:
                return False, None
        else:
            return False, None
                

def sdCircle(position, circleRadius, circlePosition):
    return math.dist(position,circlePosition) - circleRadius;



def mainLoop():
    result, color, x = checkRay([0,5],100)
    if result:
        setPixel1D(x, color)
    canvas.after(10,mainLoop())

mainLoop()

我正在尝试让

mainLoop()
无限运行,同时给
canvas
足够的时间进行更新。

python python-3.x tkinter 2d
1个回答
0
投票

在这一行的代码中存在is无限递归:

canvas.after(10,mainLoop())
Canvas.after()
(或任何 tkinter 对象的
.after()
方法)应该采用 2 个参数:

  1. 执行可调用之前等待的时间(参数 2)
  2. 等待时间后要调用的可调用对象。

传递的第二个参数应该是一个 callable 对象,而不是可调用对象的 返回值,这就是您在

canvas.after(10,mainLoop())
行中所做的。考虑将其更改为:

canvas.after(10,mainLoop)

它会解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.