You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

给定焦点与共同准线,求两条抛物线指定高度的交点[Lua]

简化双抛物线指定高度交点的Lua实现

已知两条抛物线分别由焦点(fx1, fy1)、(fx2, fy2)和共同准线dirY定义,需找出它们在指定高度y处的交点。以下是简化后的Lua实现,完全去除了原代码中a1、b1、c1等中间变量,直接通过抛物线定义推导的公式计算:

原实现代码

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

简化后的实现代码

local function twoParabolasCrossByHeight(fx1, fy1, fx2, fy2, dirY, y)
    -- 焦点y坐标相同的特殊情况,直接返回x中点和指定y
    if fy1 == fy2 then
        return (fx1 + fx2) / 2, y
    end

    -- 推导后的公式直接计算x值
    local numerator = (fx2^2 - fx1^2) + (fy1 - fy2) * (2*y - fy1 - fy2)
    local denominator = 2 * (fx2 - fx1)

    -- 处理分母为0的情况(fx1等于fx2且fy1不等于fy2时,无交点或重合)
    if denominator == 0 then
        return
    end

    local x = numerator / denominator
    return x, y
end

示例验证

-- 预期输出为400 50:
print(twoParabolasCrossByHeight(250, 250, 550, 250, 300, 50))

-- 预期输出约为400 50:
print(twoParabolasCrossByHeight(250, 250, 550, 251, 300, 50))

示意图

两条抛物线与直线交点

内容的提问来源于stack exchange,提问作者darkfrei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 19:13:16