首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检查两条绘制的线条是否重叠?

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

Stack Overflow用户
提问于 2013-04-12 18:51:24
回答 1查看 1K关注 0票数 0

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

代码语言:javascript
复制
def lines(xcoord, ycoord):  
    penup()  
    goto(xcoord, ycoord)  
    pensize(3)  
    pendown()  
    color("blue")  
    forward(10)  
    right(randint(0,360))  
    penup()

那么,如果我随机绘制这些线10次,我如何检测它们中是否有重叠?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-12 22:07:02

我假设您了解线相交测试是如何工作的,但是您不知道如何将其应用于您的代码,因为您目前没有办法获得线的终点。

您应该修改lines方法,使其返回所绘制直线的端点。

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
myLines = []
for i in range(10):
    #not pictured: generate xcoord and ycoord however you want
    myLines.append(lines(xcoord,ycoord))

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15969447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档