我假设您了解线交点测试的工作原理,但您不了解如何将其应用于您的代码,因为您目前无法获得线的端点。
您应该修改您的lines
方法,以便它返回它绘制的线的端点。
def lines(xcoord, ycoord):
penup()
goto(xcoord, ycoord)
startPoint = pos()
pensize(3)
pendown()
color("blue")
forward(10)
right(randint(0,360))
endPoint = pos()
penup()
return (startPoint, endPoint)
然后,当您绘制线条时,请跟踪点:
myLines = []
for i in range(10):
#not pictured: generate xcoord and ycoord however you want
myLines.append(lines(xcoord,ycoord))
稍后,您可以使用之前查看的线-线相交测试来检测哪些重叠。
def intersects(line1, line2):
#todo: implement line-line intersection test that you read about
#iterate through all combinations of lines,
#testing whether the two intersect
for i, line1 in enumerate(myLines):
for j, line2 in enumerate(myLines):
#we don't care if a line intersects with itself
if i == j:
continue
if intersects(line1, line2):
print "Line #{} intersects with Line #{}".format(i,j)