Python乌龟防止线相交?

Python turtle prevent line intersection?

我有一个简单的乌龟程序,它以一种模糊的随机模式画线;我将其用于我自己的艺术目的(我喜欢这种模式)。

但是,我想防止线重叠,即我想防止乌龟绘制一条已经存在的线,以防止它制作盒子。但是,我没有看到任何方法来提取乌龟在文档中绘制的线条,以及诸如此类的问题:

Turtle line intersection, coordinates

Turtle graphics drawing over itself

不是很有帮助。

它与这个不同:

因为我没有使用网格,并且使用网格不会以我想要的方式生成线条,或者网格必须非常精细。

import turtle
import random

turtle_actor = turtle.Turtle()

rect = turtle.Screen()
dims = rect.screensize()
x_max = dims[0]
y_max = dims[1]
x_min = x_max*-1
y_min = y_max*-1
turtle_actor.speed(0)

def position_check():
     global turtle_actor
     global y_max
     global x_max
     global y_min
     global x_min
     if turtle_actor.xcor() < x_min or turtle_actor.xcor() > x_max or turtle_actor.ycor() < y_min or turtle_actor.ycor() > y_max:
          turtle_actor.penup()
          turtle_actor.goto((random.randrange(x_min,x_max),random.randrange(y_min,y_max)))
          turtle_actor.pendown()

def recurse(length,n):
     global turtle_actor
     global y_max
     global x_max
     global y_min
     global x_min
     if n < 1:
          return
     l_use = length/random.choice([2,2,2,3,4,5,7,1])
     turtle_actor.forward(l_use)
     position_check()
     turtle_actor.left(random.choice([0,90,-90,180]))
     position_check()
     turtle_actor.left(random.choice([0,90,-90,180]))
     position_check()
     turtle_actor.backward(l_use)
     position_check()
     recurse(length,n-1)
     return  


recurse(50,1000)

乌龟图形没有记忆。您必须跟踪在自己的 python 数据结构中绘制的所有线条,然后在绘制每条新线条时查看它是否与之前的线条相交。不幸的是那是 O(n^2);但是有一些方法可以通过快速拒绝许多远距离线来使其更快,例如参见 [​​=10=].