如何计算二次贝塞尔曲线和水平线之间的交点?

How to compute the intersection between a quadratic bezier curve and a horizontal line?

我想要做的是获取某个贝塞尔曲线与水平线(y 坐标)相交的 X 坐标。目前,我有这个代码:

function self.getX(y)
    if y > maxY or y < minY then
        return
    end
    local a = y1 - y
    if a == 0 then
        return
    end
    local b = 2*(y2 - y1)
    local c = (y3 - 2*y2 + y1)

    local discriminant = (b^2 - 4*a*c )

    if discriminant < 0 then
        return
    else
        local aByTwo = 2*a
        if discriminant == 0 then
            local index1 = -b/aByTwo
            if 0 < index1 and index1 < 1 then
                return (1-index1)^2*x1+2*(1-index1)*index1*x2+index1^2*x3
            end
        else
            local theSQRT = math.sqrt(discriminant)
            local index1, index2 = (-b -theSQRT)/aByTwo, (-b +theSQRT)/aByTwo
            if 0 < index1 and index1 < 1 then
                if 0 < index2 and index2 < 1 then
                    return (1-index1)^2*x1+2*(1-index1)*index1*x2+index1^2*x3, (1-index2)^2*x1+2*(1-index2)*index2*x2+index2^2*x3
                else
                    return (1-index1)^2*x1+2*(1-index1)*index1*x2+index1^2*x3
                end
            elseif 0 < index2 and index2 < 1 then
                return (1-index2)^2*x1+2*(1-index2)*index2*x2+index2^2*x3
            end
        end
    end     
end

几个规格:

目前这段代码给我的是:

提前致谢,

创作者!

贝塞尔曲线有

y(t)=(1-t)^2*y1+2(1-t)*t*y2+t^2*y

扩展为

(y1-2*y2+y3)*t^2+2(y2-y1)*t+y1

您已在求解y(t)=y.

所需的二次方程a*t^2+b*t+c=0中交换了ac