我有两条由各自焦点定义的抛物线((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))