# User:Andrew Nikitin/Projective transformation

This calculator determines parameters of projective transformation that maps four given points into 4 given points.

## Introduction

### Projective plane

Let's consider all triplets of real numbers ${\displaystyle (x,y,z)}$. We will call triplets ${\displaystyle (x_{1},y_{1},z_{1})}$ and ${\displaystyle (x_{2},y_{2},z_{2})}$ equivalent when ${\displaystyle \exists c:x_{1}=cx_{2},y_{1}=cy_{2},z_{1}=cz_{2}}$. We will call the set of equivalence classes the projective plane and each equivalence class a point on this plane.

Point ${\displaystyle (x,y)}$ of the regular plane can be identified wit point ${\displaystyle (x,y,1)}$ of the projective plane and point ${\displaystyle (x,y,z),z\neq 0}$ can be identified with point ${\displaystyle (x/z,y/z)}$ of the regular plane.

Points ${\displaystyle (x,y,0),x\neq 0\,\mathrm {or} \,y\neq 0}$ of the projective plane do not have corresponding points on the regular plane and are called inifinite points.

Point ${\displaystyle (0,0,0)}$ constitutes a class in itself.

For any given set of coefficients ${\displaystyle (a,b,c)}$ the set of points defined by ${\displaystyle ax+by+cz=0}$ is called a projective straight line.

### Projective transformation

Linear transformation in the projective plane
{\displaystyle {\begin{aligned}x'=a_{1}x+b_{1}y+c_{1}z\\y'=a_{2}x+b_{2}y+c_{2}z\\z'=a_{3}x+b_{3}y+c_{3}z\end{aligned}}}

corresponds to projective transformation of the regular plane
{\displaystyle {\begin{aligned}x'={\frac {a_{1}x+b_{1}y+c_{1}}{a_{3}x+b_{3}y+c_{3}}}\,\\y'={\frac {a_{2}x+b_{2}y+c_{2}}{a_{3}x+b_{3}y+c_{3}}}.\end{aligned}}}

The important property of this transformation is that it transforms straight lines into straight lines.

The following J verb implements a projective transformation. It accepts a 3-element vector, which represents an arbitrary point on the projective plane. Alternatively, as a shortcut, 2-element vector (x,y) corresponds to finite point (x,y,1). [{{#file: "projective transformation"}} Download script: projective transformation ]

NB. M is in transposed form compared to natural
amv=:(}:%{:)@(+/ . * 3&({.!.1))"2 1


To visualise the transformation, we will use grid G0 as defined below and transform it to a projected grid. Then we will plot both grids. We will define a variant of the amv verb, called amc, which treats complex numbers as finite points of the projective plane and transforms them appropriately. [{{#file: "projective transformation"}} Download script: projective transformation ]

amc=: (j./"1@amv +.)
G0=:(<: %~ [: j./~ i.)11


### Choosing a projective transformation

A projective transformation is defined by its 9 coefficients, but since projective transformation remains the same if all coefficients are multiplied by a non-zero number, only 8 of them are independent.

If we want point ${\displaystyle (x_{0},y_{0},1)}$ to become ${\displaystyle (x_{1},y_{1},1)}$ under some projective transformation, then coefficients of this transformation must satisfy
{\displaystyle {\begin{aligned}x_{1}=(a_{1}x_{0}+b_{1}y_{0}+c_{1})/(a_{3}x_{0}+b_{3}y_{0}+c_{3})\\y_{1}=(a_{2}x_{0}+b_{2}y_{0}+c_{2})/(a_{3}x_{0}+b_{3}y_{0}+c_{3})\end{aligned}}}

or
{\displaystyle {\begin{aligned}x_{1}(a_{3}x_{0}+b_{3}y_{0}+c_{3})=(a_{1}x_{0}+b_{1}y_{0}+c_{1})\,\\y_{1}(a_{3}x_{0}+b_{3}y_{0}+c_{3})=(a_{2}x_{0}+b_{2}y_{0}+c_{2}).\end{aligned}}}

0 : 0
x0 y0 1 0  0  0 -x1x0 -x1y0 -x1
0  0  0 x0 y0 1 -y1x0 -y1y0 -y1

straight lines are set of points (x,y,z) so that ax+by+cz=0, denoted (a,b,c)
lx0=.1 0 0 NB. vertical line x=0
lx1=.1 0 _1 NB. vertical line x=1
lx2=.1 0 _2 NB. vertical x=2
cleanup (1 0 _2 mp %. M) mp |:1,.~ M amv t

)


If M is a transformation, then straight line L=(a,b,c) transforms into L·inv(M) Indeed, (L·inv(M))·(M p)=L p=0.

(α,β,γ)=M·(α',β',γ')