Math 两条线段的相交或重叠

Math 两条线段的相交或重叠,math,graphics,lua,2d,coronasdk,Math,Graphics,Lua,2d,Coronasdk,给定两条线段,每条线段由两个点定义,如何最有效地确定它们的交点或重叠 交叉点定义为实际接触中相互交叉的两段。 重叠定义为两个端点具有相同的x值或相同的y值,但至少一端位于另一条线的点之间 我问这个问题是因为我有兴趣找到一个例程,该例程计算两者并将交点作为线段返回,即使它的两个点位于同一位置(交点而不是重叠) 使用Lua,我计算重叠的函数是: local function getParallelLineOverlap( ax, ay, bx, by, cx, cy, dx, dy ) lo

给定两条线段,每条线段由两个点定义,如何最有效地确定它们的交点或重叠

交叉点定义为实际接触中相互交叉的两段。 重叠定义为两个端点具有相同的x值或相同的y值,但至少一端位于另一条线的点之间

我问这个问题是因为我有兴趣找到一个例程,该例程计算两者并将交点作为线段返回,即使它的两个点位于同一位置(交点而不是重叠)

使用Lua,我计算重叠的函数是:

local function getParallelLineOverlap( ax, ay, bx, by, cx, cy, dx, dy )
    local function sortAB( a, b )
        return math.min( a, b ), math.max( a, b )
    end

    ax, bx = sortAB( ax, bx )
    cx, dx = sortAB( cx, dx )

    local OverlapInterval = nil
    if (bx - cx >= 0 and dx - ax >=0 ) then
        OverlapInterval = { math.max(ax, cx), math.min(bx, dx) }
    end

    return OverlapInterval
end
同样使用Lua,我用于计算交点的函数是:

local function doLinesIntersect( a, b, c, d )
    -- parameter conversion
    local L1 = {X1=a.x,Y1=a.y,X2=b.x,Y2=b.y}
    local L2 = {X1=c.x,Y1=c.y,X2=d.x,Y2=d.y}

    -- Denominator for ua and ub are the same, so store this calculation
    local _d = (L2.Y2 - L2.Y1) * (L1.X2 - L1.X1) - (L2.X2 - L2.X1) * (L1.Y2 - L1.Y1)

    -- Make sure there is not a division by zero - this also indicates that the lines are parallel.
    -- If n_a and n_b were both equal to zero the lines would be on top of each
    -- other (coincidental).  This check is not done because it is not
    -- necessary for this implementation (the parallel check accounts for this).
    if (_d == 0) then
        return false
    end

    -- n_a and n_b are calculated as seperate values for readability
    local n_a = (L2.X2 - L2.X1) * (L1.Y1 - L2.Y1) - (L2.Y2 - L2.Y1) * (L1.X1 - L2.X1)
    local n_b = (L1.X2 - L1.X1) * (L1.Y1 - L2.Y1) - (L1.Y2 - L1.Y1) * (L1.X1 - L2.X1)

    -- Calculate the intermediate fractional point that the lines potentially intersect.
    local ua = n_a / _d
    local ub = n_b / _d

    -- The fractional point will be between 0 and 1 inclusive if the lines
    -- intersect.  If the fractional calculation is larger than 1 or smaller
    -- than 0 the lines would need to be longer to intersect.
    if (ua >= 0 and ua <= 1 and ub >= 0 and ub <= 1) then
        local x = L1.X1 + (ua * (L1.X2 - L1.X1))
        local y = L1.Y1 + (ua * (L1.Y2 - L1.Y1))
        return {x=x, y=y}
    end

    return false
end
局部函数doLinesIntersect(a、b、c、d)
--参数转换
局部L1={X1=a.x,Y1=a.y,X2=b.x,Y2=b.y}
局部L2={X1=c.x,Y1=c.y,X2=d.x,Y2=d.y}
--ua和ub的分母相同,因此存储此计算
局部d=(L2.Y2-L2.Y1)*(L1.X2-L1.X1)-(L2.X2-L2.X1)*(L1.Y2-L1.Y1)
--确保没有被零除-这也表示线是平行的。
--如果n_a和n_b都等于零,那么这些线将位于每一条线的顶部
--其他(巧合)。此检查未完成,因为它不是
--此实现所必需的(并行检查对此进行了说明)。
如果(_d==0),则
返回错误
结束
--n_a和n_b作为独立的可读性值进行计算
局部n_a=(L2.X2-L2.X1)*(L1.Y1-L2.Y1)-(L2.Y2-L2.Y1)*(L1.X1-L2.X1)
局部n_b=(L1.X2-L1.X1)*(L1.Y1-L2.Y1)-(L1.Y2-L1.Y1)*(L1.X1-L2.X1)
--计算直线可能相交的中间分数点。
本地ua=n_a/\u d
本地ub=n_b/\u d
--如果这些线
--相交。如果分数计算大于1或更小
--如果大于0,则需要更长的直线才能相交。

如果(ua>=0,ua=0,ub)您所说的是线段,而不是直线……直线的长度是无限长的,可能会重复