[ad_1]
In general, there is no parabola that goes through three given points. However, if you loosen the problem constraints and only define the Y coordinate of the parabola’s top and allow its X coordinate to be free, there is usually a solution.
Given \$A_x\$, \$A_y\$, \$B_x\$, \$B_y\$ (A and B’s coordinates) and \$C_y\$ (the parabola’s top), one can find a parabola formula of the form:
$$f(x) = C_y – u (x – v)^2$$
One needs to find \$u\$ and \$v\$ in this formula. The constraints give us, after some maths:
$$
\begin{align}
k &= \sqrt{\frac{C_y – A_y} {C_y – B_y}} \\
v &= \frac{k B_x – A_x}{k + 1} \\
u &= \frac{C_y – A_y} {(A.x – v)^2}
\end{align}
$$
For instance if \$A = (0, 0)\$, \$B = (10, 5)\$ and \$C_y = 15\$:
$$
\begin{align}
k &= \sqrt{\frac{15 – 0} {15 – 5}} &= 1.2247448714 \\
v &= \frac{(1.2247448714 \times 10 – 0)} {1.2247448714 + 1} &= 5.505102572 \\
u &= \frac{15 – 0}{(0 – 5.505102572)^2} &= 0.494948974
\end{align}
$$
Hence the formula:
$$
f(x) = 30 – 0.494948974 \times (x – 5.505102572)^2
$$
You can see this parabola going from \$(0,0)\$ to \$(10,5)\$ with a peak at \$Y = 15\$, as expected:
The following code should work:
pixels = []
k = sqrt((top_y - start_y) / (top_y - dest_y))
v = (k * dest_x - start_x) / (k + 1)
u = (top_y - start_y) / ((start_x - v) * (start_x - v))
for x in start_x to dest_x
y = top_y - u * (x - v) * (x - v)
pixels.push( [x,y] )
end
return pixels
[ad_2]