Given 2d vectors $A$, $B$, $C$, $D$, $P$, find $x$ such that the line through $Ax+B(1-x)$ and $Cx+D(1-x)$ passes through $P$

97 Views Asked by At

I'm trying to parametrize the quad with an x and y coordinate (from 0 to 1) so that I can properly apply a texture to it for a computer graphics application. I came up with the idea for this myself, but am having a hard time getting to a solution.

Given the 2d vectors $A$, $B$, $C$, $D$, $P$,

construct points $R$ = $A$x+$B$(1-x) and $Q$ = $C$x+$D$(1-x)

then $P$ = $R$y+$Q$(1-y)

Check my explanation image

substitute R and Q so $P$ = ($A$x+$B$(1-x))y + ($C$x+$D$(1-x))(1-y)

since coords are 2d, this is actually two equations (lowercase=x uppercase=y)

$p$ = ($a$x+$b$(1-x))y + ($c$x+$d$(1-x))(1-y)

$P$ = ($A$x+$B$(1-x))y + ($C$x+$D$(1-x))(1-y)

take first equation and solve for y

$y = \frac{-cx+d(x-1)+p}{x(a-c)+b(1-x)+d(x-1)}$

substitute y in second equation

$P = (Ax+B(1-x))\frac{-cx+d(x-1)+p}{(x(a-c)+b(1-x)+d(x-1)}+(Cx+D(1-x))(1-(\frac{-cx+d(x-1)+p}{x(a-c)+b(1-x)+d(x-1)}))$

solve for x? I think that would be it, I'm just stuck on how to do this. Its too large and unwieldy. Is there another way?

3

There are 3 best solutions below

7
On BEST ANSWER

Give all vectors a third coordinate of $0$ so that, as members of $\Bbb{R}^3$, they're compatible with cross products. Also, translate all points by $-P$ to move $P$ to the origin. Then, for $P$, $Q$, $R$ to be collinear, "all we have to do" is force $$R\times Q = O \tag{0}$$ where $O:=(0,0,0)$. To simplify some calculations, let's re-parameterize relative to the midpoints of $\overline{AB}$ and $\overline{CD}$ by defining $s := 2x-1$. Then we have $$R = \frac12(A+B)+ \frac12(A-B)s \qquad\qquad Q = \frac12(C+D)+\frac12(C-D)s$$ and $(0)$ becomes (after multiplying-through by $4$), $$s^2 ((A-B)\times(C-D)) \;+\; 2 s (A\times C-B\times D) \;+\;((A+B)\times(C+D)) \;=\; O \tag1$$

Since $A$, $B$, $C$, $D$ are all in $\Bbb{R}^2$, the first two components of any cross product are $0$. Thus, $(1)$ amounts to a quadratic in $s$ whose coefficients are the third components of those cross-products. Solving via the Quadratic Formula, we find $$s = -\frac{(A\times C -B\times D)_3\pm \sqrt{\Delta}}{((A-B)\times(C-D))_3} \qquad\qquad\left(\;x = \frac12(1+s)\;\right)\tag2$$ where $$\begin{align} \Delta &:=(A\times C - B\times D)_3^2 - ((A-B)\times(C-D))_3 ((A+B)\times(C+D))_3 \\[4pt] &\;=(A\times D)_3^2 + (B\times C)_3^2 - 2 \left(\, (A\times B)_3\,(C\times D)_3 + (A\times C)_3\,(B\times D)_3 \,\right) \end{align}$$ (The second form will show invariance under a substitution below.)

Now, to find $y$ first observe that $$\begin{align} Ry+Q(1-y) &= (Ax+B(1-x)) y + (C x+D(1-x))(1-y) \\ &=(Ay + C(1-y))x+ (B y+D(1-y))(1-x) \\ &=R' x + Q'(1-x) \end{align}$$ where $R' := Ay+C(1-y)$ and $Q':=By+D(1-y)$. Thus, we can find $y$ by the process above, which gives a counterpart of $(2)$ with the substitutions $A\to A$, $B\leftrightarrow C$, $D\to D$ (leaving $\Delta$ unchanged, as promised): $$t = -\frac{(A\times B -C\times D)_3\mp \sqrt{\Delta}}{((A-C)\times(B-D))_3} \qquad\qquad\left(\;y = \frac12(1+t)\;\right)\tag3$$ Note that the sign on $\sqrt{\Delta}$ flips from its state in $(2)$ to get the proper correspondence between the solutions for $s$ and $t$. $\square$


For an example, consider $$A = (1,2,0) \qquad B = (3,4,0) \qquad C = (-1,-1,0) \qquad D = (-2,-3,0) \qquad P=(0,0,0)$$ Then, writing $\lambda$ for $\pm 1$, we (and by "we", I mean "Mathematica") can calculate $$\Delta = 8 \qquad s = 1+\lambda\sqrt{2} \qquad t = -3-2\lambda\sqrt{2} $$ $$x = \frac12 (2 +\lambda \sqrt{2}) \qquad y = -1-\lambda\sqrt{2}$$ so that $$\begin{align} R &= Ax+B(1-x) = (1 - \lambda \sqrt{2}, 2 - \lambda \sqrt{2}, 0) \\ Q &= Cx+D(1-x) = \left(\frac12 (-2 + \lambda \sqrt{2}), -1 + \lambda \sqrt{2}, 0\right) \end{align}$$ and $$Ry+Q(1-y) = (3 (\lambda^2-1), 4 (\lambda^2-1), 0) = (0,0,0) = P$$ as desired.

2
On

Let us take a time of reflection.

  • What you are using is called a biquadratic mapping which induces distorsions in a subtle manner illustrated by the diagram below:

enter image description here

  • What I advise you to use instead for texture mapping is projective transformations ; they generate less distorsions (in particular for strongly deformed quadrilaterals):

enter image description here

(with perspective points). The projective transform mapping $ABCD$ onto quadrangle $A'B'C'D'$ (or the reverse) where the texture is "located" with

$$A'(0,0),B'(1,0),D'(1,1),C'(0,1)$$

(see remark below) is given by formulas:

$$x'=f(x,y)=(ax+by+c)/(gx+hy+i),$$

$$y'=g(x,y)=(dx+ey+f)/(gx+hy+i)$$

You can assume in general that you have $i=1$ (up to a common multiplication of the numerators and denominators).

Therefore, you have 8 unknowns coefficients $a,b,c,d,e,f,g,h$ and 8 linear constraints by expressing that:

$$\text{Image of point (0,0):} \ 0=f(x_A,y_A),0=g(x_A,y_A)$$

$$\text{Image of point (1,0):} \ 1=f(x_B,y_B),0=g(x_B,y_B)$$

$$\text{Image of point (1,1):} \ 1=f(x_D,y_D),1=g(x_D,y_D)$$

$$\text{Image of point (0,1):} \ 0=f(x_C,y_C),1=g(x_C,y_C)$$

or the inverse formulas depending on what you desire.

This system of equations can be solved once for all (I can do it on MatLab CAS).

Remark; instead of a square, I could have chosen a more general rectangle $A'(0,0),B'(L,0),D'(L,W),C'(0,W)$ for the reference texture but the principle is the same.

0
On

Here is an approach -

Given positions vectors $ \small A, B, C, D$ and $ \small P$, we easily know direction vectors $\small \vec{PB}, \vec{BA}, \vec{PD}$ and $ \small \vec{DC}$. Also note that $ \small \vec{BR} = x \vec{BA}, \vec {DQ} = x \vec {DC}$

So, $\small \vec{PR} = \vec {PB} + \vec{BR} = \vec {PB} + x \vec{BA} \ , \ \vec{PQ} = \vec {PD} + \vec{DQ} = \vec {PD} + x \vec{DC}$

As $ \small QR$ must pass through point $ \small P$, $ \small \vec {PQ} $ and $ \small \vec{PR}$ are in opposite direction. Hence their cross product will give zero vector.

$\small \vec{PR} \times \vec{PQ} = (\vec {PB} + x \vec{BA}) \times (\vec {PD} + x \vec{DC}) = (0, 0)$

$ \implies \small x^2 (\vec{BA} \times \vec{DC}) + x (\vec{PB} \times \vec{DC} + \vec{BA} \times \vec{PD}) + (\vec{PB} \times \vec{PD}) = (0, 0)$

Equating both $i$ and $j$ components of LHS to zero, you can find value of $x$