Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 22109

Quadrangle transformation?

$
0
0
I know that an affine transform will only turn a rectangle in to a parallelogram (allowing for shear, rotate, resize, reposition), but ONLY a parallelogram. It doesn't allow you to map a rectangle into ANY RANDOM quadrilateral (aka quadrangle), I figured out a general mathematical transformation, but the problem with it is it takes general parameters. I want to define a transformation based on the XY-coordinates of each point in a set of 4 known points A, B, C, and D. Then I could define these points just by clicking 4 times in an image, click a "transform" button and it would remap the pixels into the the new shape. Now I don't want to try for perspective (worrying about a z coordinate, such that things look more "squished" in the distance). I just want to simply map the points into a general grid in a new 2d-space.

Now my general equations I figured out are shown below:
Quote:

x2 = x*(y*a+b) + y*c + d
y2 = y*(x*e+f) + x*g + h


for calculating x2
x is the input x coordinate
y*a is variable slope based on input y coordinate and constant a
b is slope constant
y*c is variable offset based on input y coordinate and constant c
d is offset constant

for calculating y2
y is the input y coordinate
x*e is variable slope based on input x coordinate and constant e
f is slope constant
x*g is variable offset based on input x coordinate and constant g
h is offset constant


You may find it odd that y is being treated as part of defining slope when talking about x2 (and x is part of defining slope when talking about y2) when typically slope is m such that y=m*x+b (where b is the offset). However in this case it isn't about y/x = as would be when drawing a line, as although they are "linear" equations, they aren't happening in normal 2d space, but rather a special transforation space, where the coordinates themselves are parameters in the transformation. So in this case the slope for x2 is not y/x or m, but rather is x2/x, where y is used as part of the equation that calculates the slope (y*a+b). The same goes for y2, where the slope is y2/y and is calculated with (x*e+f).
And here is some VB6 code that implements this algorithm.

Code:

Private Function y2(ByVal x As Double, ByVal y As Double, _
ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
y2 = y * (x * a + b) + x * c + d
End Function

Private Function x2(ByVal x As Double, ByVal y As Double, _
ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal d As Double) As Double
x2 = x * (y * a + b) + y * c + d
End Function

But I still can't figure out how to extract these parameters from the XY-coordinates of 4 known points, that is to say:
PointA(X,Y)
PointB(X,Y)
PointC(X,Y)
PointD(X,Y)


If someone can help me with this, I will thank you greatly.

Viewing all articles
Browse latest Browse all 22109

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>