0

我尝试查看线-线相交测试,但我不太了解它,因为它使用的是线的端点。

def lines(xcoord, ycoord):  
    penup()  
    goto(xcoord, ycoord)  
    pensize(3)  
    pendown()  
    color("blue")  
    forward(10)  
    right(randint(0,360))  
    penup()

因此,如果我随机绘制这些线 10 次,我如何检测它们是否重叠?

4

1 回答 1

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)
于 2013-04-12T14:07:01.543 回答