我尝试查看线 - 线交叉点测试,但我不太了解它,因为它使用了线路的端点。
def lines(xcoord, ycoord):
penup()
goto(xcoord, ycoord)
pensize(3)
pendown()
color("blue")
forward(10)
right(randint(0,360))
penup()
因此,如果我将这些线随机抽取10次,我如何检测它们是否重叠?
答案 0 :(得分:1)
我假设您了解线路相交测试的工作原理,但您不明白如何将其应用于您的代码,因为您目前无法获得线路的终点。
您应修改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)