根据焦点和共同准线找到两个抛物线的交点 [Lua]

问题描述 投票:0回答:1

我有两条由各自焦点定义的抛物线((fx1,fy1)和(fx2,fy2)),以及一个公共准线dirY。

在已知交点高度 y 的情况下,我需要找到这两个抛物线的交点。

我有很长的解决方案,但是这里有办法让它更短,没有a,b,c吗?

local function twoParabolasCrossByHeight (fx1, fy1, fx2, fy2, dirY, y)
    local f1 = math.abs(dirY-fy1)/2
    local f2 = math.abs(dirY-fy2)/2

    if fy1 == fy2 then
        return (fx1+fx2)/2, y
    end
    local a1 = -1/(4*f1)
    local a2 = -1/(4*f2)
    local b1 = -2*fx1*a1
    local b2 = -2*fx2*a2
    local c1 = fx1*fx1*a1 + fy1 + f1
    local c2 = fx2*fx2*a2 + fy2 + f2
    local a = a1-a2
    local b = b1-b2
    local c = c1-c2

    local d = b*b-4*a*c
    if d < 0 then return end
    local x1 = (-b-math.sqrt (d))/(2*a)
    local y1 = a1*x1*x1 + b1*x1 + c1
    return x1, y1
end

两个例子:

 -- must be 400 50:
print (twoParabolasCrossByHeight (250, 250, 550, 250, 300, 50))

 -- must be aprox. 400  50:
print (twoParabolasCrossByHeight (250, 250, 550, 251, 300, 50))

math lua trigonometry
1个回答
0
投票

这相对容易:给定焦点的两条抛物线作为线交叉。

结果线是两点之间的垂直平分线:

function focusFocusLineYCrossing (p1x, p1y, p2x, p2y, y)
    local cx = (p1x+p2x)/2
    local cy = (p1y+p2y)/2
    local m = (p2y-p1y)/(p2x-p1x) -- slope
    local x = cx - m*(y-p1y) -- perpendicular to slope
    return x
end

© www.soinside.com 2019 - 2024. All rights reserved.