# Function to map 2D coordinates from one quadrilateral to another

What would be a clean way to define a function $F : R^2 \rightarrow R^2$ that maps coordinates from one quadrilateral domain to another like the below image? (Assuming we know the coordinates of the 4 points of each quadrilateral)

Does it matter if one of them is non-convex or is it possible to get a single nice function that handles all cases? If non-convex cases complicate the mapping, what would be a nice function assuming that both are convex?

• If both are convex, map the corners of square A to the corners of square B. Every point in A is a convex combination of the vertices, so you map it to the corresponding combination of the vertices of B. If A is not convex, but B is convex, you can choose 3 of the vertices of A so that the triangle they determine covers all of A. Map these three vertices to 3 vertices of B and proceed as above. Some convex combinations of the 3 vertices don't lie in A, but you don't map them. The image of the map will not be all of B. I A isn't convex, I don't have a good idea. – saulspatz Feb 8 '18 at 22:33
• It depends on what other properties you want the map to have. Two common choices are a planar perspective transformation (homography) and bilinear interpolation. – amd Feb 8 '18 at 23:22
• @saulspatz: That doesn't fix the result uniquely, because each internal point has a one-dimensional range of convex combinations, which will not all correspond to the same point in B. – hmakholm left over Monica Feb 9 '18 at 2:06

This can be done using range-split operation, $A,B \in (\mathbb{R},\mathbb{R})$, $x \in [0..1]$:
• RangeSplit($A$,$B$,$x$) = $(1.0-x) \cdot A+x\cdot B$.
If your original rectangle coordinates are $(x,y), x \in [0..w], y \in [0..h]$, and corners of new quad is $(A..B),(C..D)$, then the final solution would be:
• $(x_0,y_0) = (x/w,y/h)$
• $(x_r,y_r)$ = RangeSplit(RangeSplit($A$,$B$,$x_0$),RangeSplit($C$,$D$,$x_0$),$y_0$)