我想要做的是获得某个贝塞尔曲线与水平线(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
一些规格:
目前这段代码给了我这个:
(1-t)^2*x1+2*(1-t)*t*x2+t^2*x3
提前致谢,
创作者!
答案 0 :(得分:2)
贝塞尔曲线
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
您已在解决a
所需的二次方程式c
中交换了a*t^2+b*t+c=0
和y(t)=y
。