What I am trying to do get the X coordinates at which a certain bezier curve crosses a horizontal line (a y coordinate). For the moment, I have this code:
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
A few specifications:
- This is Lua code.
- local means the variable is local to the chunk of code, so it does not affect the code's functionality.
- y1, y2, and y3 are the y coordinate of the 3 points. The same applies to x1, x2, x3.
- y is the y coordinate of the horizontal line I am computing.
- maxY is the biggest of the 3 y's.
- minY is the smallest.
For the moment this code gives me this:
- There are 8 bezier curves
- The green ones are generated using the normal method:
(1-t)^2*x1+2*(1-t)*t*x2+t^2*x3
- The red dots are the control points.
- The white lines are what is generated using the method described with the code above.
- The straight lines are lines, ignore them.
- There should be 8 curves, but only 4 are rendered.
Thanks in advance,
Creator!