2

Given the following rectangles:

diagram

one can map any point $(x, y)$ from the rectangle to a point $P$ on the quadrilateral using the following steps:

  1. Define linear interpolation as $l(p_0, p_1, t) = p_0 + t(p_1 - p_0)$
  2. Get the horizontal and vertical ratios of $(x, y)$ to the dimensions of the rectangle: $r_x = \dfrac{x}{||\overline{AB}||}$ and $r_y = \dfrac{y}{||\overline{AD}||}$
  3. Linearly interpolate up the "vertical" segments of the quadrilateral using the previously calculated vertical ratio $r_y$: $L = l(A, E, r_x)$ and $R = l(G, F, r_x)$
  4. Linearly interpolate across the "horizontal" segment $\overline{LR}$ using the previously calculated horizontal ratio $r_x$: $P = l(L, R, r_y)$

Is it possible to find the inverse of this process—that being, is it possible to map a point from the quadrilateral to the rectangle instead of from the rectangle to the quadrilateral?

Note: I am looking to use such a process to draw an image file, which is a "rectangular" two-dimensional array of colors, inside a non-rectangular quadrilateral. If such an inverse process is less efficient than a different approach, then feel free to let me know, but I am still interested in whether or not it is possible to find the inverse of this process.

  • I edited the tags, since this is not a linear transformation. You might want to change the title of you question as well. – Christoph Sep 21 '18 at 06:31
  • @Christoph Thanks for the heads-up. Do you mind explaining why exactly this is not a linear transformation? My vocabulary is profoundly weak in this area of math. – NetherGranite Sep 21 '18 at 06:33
  • 2
    The $xy$ term makes it nonlinear, as does the constant term, although with only the latter it would still be an affine transformation. – amd Sep 21 '18 at 06:37

1 Answers1

3

The bilinear interpolation map that you describe is of the form $$x' = a+bx+cy+dxy \\ y' = e+fx+gy+hxy.$$ To solve this system for $x$ and $y$, you can start by multiplying the first equation by $h$ and the second by $d$ and subtracting. This gives you the linear equation $$(bh-df)x+(ch-dg)y+(ah-de)=hx'-dy'$$ which you can then solve for $x$ or $y$ and back-substitute into either of the original equations to get a quadratic in the other variable. Thus, there are up to two possible solutions corresponding to different choices of sign for the square root in the quadratic formula, but only one of them lies in the source rectangle. By continuity, this same choice of sign will give you the correct inverse image for any point in the destination quad. (Geometrically, you’re finding the intersections of two right hyperbolas with asymptotes parallel to the coordinate axes.)

An alternative to bilinear interpolation is to user a planar homography (perspective transformation) to map from the rectangle to the quad. Unlike the bilinear interpolation above, this map is one-to-one, so can be inverted without having to make any choices to get the correct inverse. Another advantage of this type of mapping is that many computer graphics libraries provide an API to construct this mapping for any pair of (convex) quadrilaterals. To build it yourself, you can use the method described in this answer, which allows you to generate the inverse map at the same time: it’s given by the matrix $AB^{-1}$.

amd
  • 53,693
  • Great terminology to know, thanks for your thorough answer. I actually did come across the form you described when trying to solve the problem myself, but I'm not sure how to go about solving it. That was actually going to be my next question. Do you have any resources you can point me to on how to solve it, or even just the name of that particular form of system of equations that I can look up? – NetherGranite Sep 21 '18 at 05:34
  • @NetherGranite Inverting the bilinear interpolation map is straightforward, but somewhat ugly. I’ve updated my answer with an outline of how you might go about this. – amd Sep 21 '18 at 06:25
  • Great, thanks for that. That is a surprisingly elegant solution to that problem. I'll definitely check out the SO answer you linked to. – NetherGranite Sep 21 '18 at 06:35