如何检查两条绘制的线是否重叠?

2024-04-18 14:01:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着看直线-直线相交测试,但我不太理解它,因为它使用的是直线的端点。在

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

如果我随机画了10次这些线,我怎么能检测出它们是否重叠?在


Tags: rightdefblue端点直线colorforwardlines
1条回答
网友
1楼 · 发布于 2024-04-18 14:01:04

假设你不能理解你的底线,但是我不能理解你的代码。在

您应该修改您的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)

然后,在绘制直线时,请跟踪这些点:

^{pr2}$

稍后,您可以使用前面看到的线-线相交测试来检测哪些重叠。在

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)

相关问题 更多 >