Jump to content
Posted 25 February 2011 - 12:02 PM
Posted 25 February 2011 - 12:15 PM
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.
You don't forget how to play when you grow old; you grow old when you forget how to play.
Posted 25 February 2011 - 12:25 PM
Posted 25 February 2011 - 01:10 PM
Posted 25 February 2011 - 01:16 PM
( u ) ( 1 a b c )( uv ) = ( 0 ) ( 0 1 d e )( v ) ( 0 ) ( 1 )
( u ) ( bx-ax cx-bx dx-ax ax-px )( uv ) = ( 0 ) ( by-ay cy-by dy-ay ay-py )( v ) ( 0 ) ( 1 )
Posted 25 February 2011 - 01:32 PM
Posted 25 February 2011 - 01:45 PM
Posted 25 February 2011 - 08:31 PM
Yeah, I though of looking at it the following way:
The approach of the bilinear interpolation
p1 := a + ( b - a ) * u
p2 := d + ( c - d ) * u
p := p1 + ( p2 - p1 ) * v
means that there is a line between p1 and p2 that passes through p. Expressing this line the other way, namely
p + l = p1
p + k * l = p2
so that l + k * l (where k is obviously need to be a negative number) is the said line. Considering that this is done in 2D, we have 4 equations with 4 unkowns (k, lx, ly, and u) where k * lx and k * ly are the problems. Fortunately, from the lower of the both equations, we can isolate
k * lx = dx - px + ( cx - dx ) * u
k * ly = dy - py + ( cy - dy ) * u
and divide both of these, so that
lx / ly = [ dx - px + ( cx - dx ) * u ] / [ dy - py + ( cy - dy ) * u ]
Now, using the upper of the equations to get
lx = ax - px + ( bx - ax ) * u
ly = ay - py + ( by - ay ) * u
and setting these into the above lx / ly, we get a quadratic equation solely in u which can be solved using the famous p,q-formula.
This gives, as luca-deltodesco has mentioned, 0 or 1 real solutions in general. But due to the fact that p always lies inside the quad, I expect 1 real solution. With that, v can be determined easily.
double C = (double)(a.Y - p.Y) * (d.X - p.X) - (double)(a.X - p.X) * (d.Y - p.Y); double B = (double)(a.Y - p.Y) * (c.X - d.X) + (double)(b.Y - a.Y) * (d.X - p.X) - (double)(a.X - p.X) * (c.Y - d.Y) - (double)(b.X - a.X) * (d.Y - p.Y); double A = (double)(b.Y - a.Y) * (c.X - d.X) - (double)(b.X - a.X) * (c.Y - d.Y); double D = B * B - 4 * A * C; double u = (-B - Math.Sqrt(D)) / (2 * A); double p1x = a.X + (b.X - a.X) * u; double p2x = d.X + (c.X - d.X) * u; double px = p.X; double v = (px - p1x) / (p2x - p1x);u and v are then [0,1] coordinates you can sample a texture from, etc.