我试着查看线-线相交测试,但我不太理解它,因为它使用的是线的端点。
def lines(xcoord, ycoord):
penup()
goto(xcoord, ycoord)
pensize(3)
pendown()
color("blue")
forward(10)
right(randint(0,360))
penup()那么,如果我随机绘制这些线10次,我如何检测它们中是否有重叠?
发布于 2013-04-12 22:07:02
我假设您了解线相交测试是如何工作的,但是您不知道如何将其应用于您的代码,因为您目前没有办法获得线的终点。
您应该修改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)https://stackoverflow.com/questions/15969447
复制相似问题