# How to solve barrel problem
## One approach
You need to translate old (distorted) point coordinates into new (undistorted) point coordinates.
The equation, which manages translation is:
$$
r_u = r_d(1+kr_d^2) \quad (1)
$$
Physical meaning of equation preserves angles, thus any changes are managed propotionally for $X$ and $Y$.
Let $r_{ux} = |center_x - p_{ux}|$ and so on. Following original equation and physical meaning,
$$
r_{ux}^2+r_{uy}^2 = (r_{dx}^2+r_{dy}^2)(1+k(r_{dx}^2+r_{dy}^2))^2 \quad (2) \\
\frac{r_{ux}}{r_{uy}} = \frac{r_{dx}}{r_{dy}} = ctg\alpha=t \quad (3)
$$
RHS of $(2)$ is a constant with respect to $k$ is given. LHS of $(2)$ is
$$
r_{ux}^2 + \frac{r_{ux}^2}{ctg^2\alpha}
$$
If you solve this for $r_{ux}$, you will get a solution.
NB: In this case there is a chance that there will be "missing points" in the generated image.
## Another approach
Other approach is to restore old coordinates for each point in new image. Thus you will have a chance to use any interpolation method.
Let $r_{dx}^2=x$. Solving (2) for $x$:
$$
r_{ux}^2(1+\frac{1}{t^2})=(x(1+\frac{1}{t^2}))(1+kx(1+\frac{1}{t^2}))^2
$$
To simplify, $(1+\frac{1}{t^2})=a$,
$$
r_{ux}^2=x*(1+akx)^2\\
r_{ux}^2=x*(1+2akx+a^2k^2x^2)\\
r_{ux}^2=x+2akx^2+a^2k^2x^3
$$
Find $t, a$. Solve this for $x$. Restore $r_{dx}$ and then $r_{dy}$.