Re: Parabola functions in Lua
Posted: Sun Mar 24, 2024 11:09 am
The parabola is given by focus and directrix.
Finding the roots of parabola for given high y:
The parabola is given by focus and directrix.
Parabola curve is limited by points A (left) and B (right).
Finding control point C for quadratic Bezier curve for special case, where ay == by:
(based on the code of Mike 'Pomax' Kamermans)
or just
Finding the roots of parabola for given high y:
Code: Select all
function getFocusParabolaRoots (fx, fy, y) -- focus, horizontal line
-- (directrix dirY is global)
local h = fx -- x shift
local p = -(dirY-fy)/2 -- always negative for voronoi
local k = (fy - p) - y -- (fy-p) is vertex high
local leftX = h - math.sqrt (-k*4*p)
local rightX = h + math.sqrt (-k*4*p)
return leftX, rightX
end
Parabola curve is limited by points A (left) and B (right).
Finding control point C for quadratic Bezier curve for special case, where ay == by:
(based on the code of Mike 'Pomax' Kamermans)
Code: Select all
function getBezierControlPoint (fx, fy, ax, bx, dirY)
local f = function (x)
return (x*x-2*fx*x+fx*fx+fy*fy-dirY*dirY) / (2*(fy-dirY))
end
local function df(x)
return (x-fx) / (fy-dirY)
end
if (fy == dirY) then return end -- not parabola
local ay, by = f(ax), f(bx)
if (ay == by) then -- special case: horizontal AB
return fx, ay + 2*((fy + dirY) / 2-ay)
else
local ad = df(ax) -- tangent slope for A
local dx = (bx-ax)/2
return ax+dx, ay+ad*dx
end
end
Code: Select all
function getBezierControlPoint (fx, fy, ax, bx, dirY)
local f = function (x) return (x*x-2*fx*x+fx*fx+fy*fy-dirY*dirY) / (2*(fy-dirY)) end
local function df(x) return (x-fx) / (fy-dirY) end
if (fy == dirY) then return end -- not parabola
local ay, by = f(ax), f(bx)
local ad, dx = df(ax), (bx-ax)/2
return ax+dx, ay+ad*dx
end
Code: Select all
-- evaluate parabola for given focus and directrix, returns y-position for given x-position:
local function evaluateParabola(fx, fy, x, dirY)
local numerator = fx*fx + fy*fy - dirY*dirY - x*(2*fx - x)
local denominator = 2*(fy - dirY)
return numerator / denominator
end