【问题标题】:Trying to find if a line intersects with another, but it is failing even when the lines do intersect试图找出一条线是否与另一条线相交,但即使线确实相交它也会失败
【发布时间】:2015-11-04 02:00:08
【问题描述】:

在我的程序开始时,我绘制了六个垂直的直线段,然后我随机绘制了用户想要的任意数量的线段。每次绘制它时,我都会检查它是否与这六个段之一相交。问题是,即使它们相交,它也永远不会返回 True。

我正在使用 Python,并对两条线使用 y = mx + b 来找到它们相交的公共点,并检查它是否位于两条线段上。 代码如下:

import random
import turtle


wn = turtle.Screen()
wn.setworldcoordinates(-50, -50, 50.5, 50)

intersectCount = 0
stickCount = 0


def drawLines():
    draw = turtle.Turtle()
    for x in range(-5, 6):
        if x % 2 != 0:
            draw.penup()
            draw.goto(x * 10, 50)
            draw.pendown()
            draw.goto(x * 10, -50)


def drawStick():
    draw = turtle.Turtle()
    draw.color("Brown")
    rand = random.Random()

    stickLength = 10  # sticks are 10 units long
    x1 = rand.randint(-50, 50)
    y1 = rand.randint(-50, 50)
    x2 = 0
    y2 = 0

    direction = rand.randint(1, 4)

    if(direction == 1):
        x2 = x1 + stickLength
        y2 = y1 - stickLength
    elif(direction == 2):
        x2 = x1 - stickLength
        y2 = y1 + stickLength
    elif(direction == 3):
        x2 = x1 + stickLength
        y2 = y1 + stickLength
    else:
        x2 = x1 - stickLength
        y2 = y1 - stickLength

    draw.penup()
    draw.goto(x1, y1)
    draw.pendown()
    draw.goto(x2, y2)

    global stickCount
    stickCount += 1
    for x in range(-5, 6):
        if x % 2 != 0:
            if (checkStick(x * 10, 50, x * 10, -50, x1, y1, x2, y2)):
                global intersectCount
                intersectCount += 1
                break


def drop():
    sticks = input("Enter how many sticks you would like to drop: ")
    sticks = int(sticks)
    for x in range(0, sticks):
            drawStick()

    print(str(stickCount) + " sticks were dropped")
    print("There were " + str(intersectCount) + " sticks that intersected")


def checkStick(x1, y1, x2, y2, sX1, sY1, sX2, sY2):
    #greatest and least x coordinates from the line
    greatestX = 0
    leastX = 0
    if(x1 == x2 or x1 > x2):
        greatestX = x1
        leastX = x2
    else:
        greatestX = x2
        leastX = x1
    #same thing but with y's
    greatestY = 0
    leastY = 0
    if(y1 == y2 or y1 > y2):
        greatestY = y1
        leastY = y2
    else:
        greatestY = y2
        leastY = y1
    #same thing but with stick x's
    gStickX = 0
    lStickX = 0
    if(sX1 == sX2 or sX1 > sX2):
        greatestX = sX1
        leastX = sX2
    else:
        greatestX = sX2
        leastX = sX1
    #same thing but with stick x's
    gStickY = 0
    lStickY = 0
    if(sY1 == sY2 or sY1 > sY2):
        greatestY = sY1
        leastY = sY2
    else:
        greatestY = sY2
        leastY = sY1

    #y = mx + b
    #the stick
    stickSlope = ((sY2 - sY1) / (sX2 - sX1))  # m, or the slope
    stickIntercept = sY1 - (stickSlope * sX1)  # b = y - (mx)
    #the line
    lineSlope = 0
    if(x2 - x1 != 0):  # m, or the slope
        lineSlope = (y2 - y1) / (x2 - x1)

    lineIntercept = y1 - (lineSlope * x1)  # b = y - (mx)
    #set the two formulas equal to each other, find x and then y, that is where they intersect#this will be reset as the x of intersection
    x = (lineIntercept - stickIntercept) / (stickSlope - lineSlope)  # solving for x by getting the x's on one side, and other numbers on one side, then dividing out whatever number x is being multiplied by to get what x is
    y = ((stickSlope * x) + stickIntercept)  # back to y = mx + b now that we have all the variable to find y
#points intersect at x, y

    if(stickSlope == lineSlope):
        return False  # parallel
    else:
        #checking if it is within the line segment
        if(x <= greatestX and x >= leastX):
            if(y <= greatestY and y >= leastY):
                #checking if it is within the stick segment
                if(x <= gStickX and x >= lStickX):
                    if(y <= gStickY and x >= lStickY):
                        return True
                    else:
                        return False
                else:
                    return False
            else:
                return False
        else:
            return False


drawLines()
drop()

raw_input()  # waits for the user to click a key to exit

【问题讨论】:

  • 请修复代码块的缩进。
  • 这是python 2还是python3?
  • @saulspatz python3.5
  • 请举例说明它没有达到您的预期。让它尽可能小。
  • @saulspatz 在我检查交叉点 checkStick() 的地方不起作用。我包含了所有代码,因为有必要理解。它没有正确检测线何时穿过另一条线(从不返回 True)。

标签: python line intersection turtle-graphics line-intersection


【解决方案1】:

如果你只是想找到交点,这很容易:

def intersects(m1, b1, m2, b2, xlims=[0, 1], ylims=[0, 1]):
    if m1 == m2 and b1 != b2:
        return None, None

    intersect_x = (b2 - b1) / (m1 - m2)
    if intersect_x < xlims[0] or intersect_x > xlims[1]:
        return None, None

    intersect_y = m1 * intersect_x + b1
    if intersect_y < ylims[0] or intersect_y > ylims[1]:
        return None, None

    return intersect_x, intersect_y


if __name__ == '__main__':
    a, b = intersects(1, 0, -1, 1)
    print(a, b)
    a, b = intersects(1, 0, 1, 0.5)
    print(a, b)

【讨论】:

    【解决方案2】:

    将检查段冲突 (checkStick) 的函数替换为

    def checkStick(x1, y1, x2, y2, x3, y3, x4, y4):
        dx1 = x2 - x1
        dy1 = y2 - y1
        dx2 = x4 - x3
        dy2 = y4 - y3
    
        if dx1 * dy2 - dx2 * dy1 == 0:
            # Segments are on parallel lines. There will be intersection if segments
            # are collinear and they give intersecting projection to both axis
            segments_collinear = (y3 - y1) * dx1 == dy1 * (x3 - x1)
            collision_ox = max(x1, x2) >= min(x3, x4) and min(x1, x2) <= max(x3, x4)
            collision_oy = max(y1, y2) >= min(y3, y4) and min(y1, y2) <= max(y3, y4)
            return segments_collinear and collision_ox and collision_oy
    
        s = (dx1 * (y1 - y3) - dy1 * (x1 - x3)) / (dx1 * dy2 - dx2 * dy1)
        t = (dx2 * (y1 - y3) - dy2 * (x1 - x3)) / (dx1 * dy2 - dx2 * dy1)
    
        return 0 <= s <= 1 and 0 <= t <= 1
    

    如果您想了解参数s 和t 公式背后的思想,请查看this answer(那里有参数u ant t)

    基本上,如果0 &lt;= s &lt;= 1 and 0 &lt;= t &lt;= 1,那么两条线的交点(它们是我们线段的延伸)在我们的两个线段上。否则它在外面。

    特殊情况。如果dx1 * dy2 - dx2 * dy1 == 0 那么我们的线是平行的。只有当两个线段都在同一条线上 (3rd point is collinear with first two) 并且它们实际上在这条线上相交(线段投影到两个轴相交)时,我们才会有交集。

    【讨论】:

    • 啊,我明白了,谢谢你的帮助。现在效果很好!我将继续阅读该帖子,以便更好地了解发生了什么。
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 2018-04-06
    • 2011-09-28
    相关资源
    最近更新 更多