Click to See Complete Forum and Search --> : 2d transformations algorithms


Maverika
June 9th, 2004, 05:55 PM
Does anyone know the algorithms for 2d transformations? Specifically the algorithms for translation,rotation and scaling.

If anyone knows them or knows a url which has them please post them.

Electroman
June 9th, 2004, 08:12 PM
How do you mean? Like matrices? if so Dx has some functions that create the Matrices for you if you give it the rotation/movement you want ;).

themoffster
July 2nd, 2004, 06:59 PM
If it's java matrices you're after, I can help.

Bornish
August 11th, 2004, 05:26 AM
I think using 2 functions will be enough for all your 2d transformations (since you didn't mentioned warping or other sophisticated transformations). I use this functions a lot in my projects when rotation or scaling must be done from a different origin, lets say, my cursor location.

void prj2dMatrix(double pm[9],double bX,double bY,double k,double sX,double sY,double aX,double aY)
{
pm[0] = sX*cos(k);pm[3] = 0.0-sX*sin(k);pm[6] = aX-bX*sX*cos(k)+bY*sX*sin(k);
pm[1] = sY*sin(k);pm[4] = sY*cos(k);pm[7] = aY-bX*sY*sin(k)-bY*sY*cos(k);
pm[2] = 0;pm[5] = 0;pm[8] = 1;
}
void prj2dTransform(double pm[9],dPoint2d& ch2)
{
double w,tempX;
w = ch2.X*pm[2]+ch2.Y*pm[5]+pm[8];
tempX = (ch2.X*pm[0]+ch2.Y*pm[3]+pm[6])/w;
ch2.Y = (ch2.X*pm[1]+ch2.Y*pm[4]+pm[7])/w;
ch2.X = tempX;
}

A small legend is required to fully understand this code:
(bX,bY) - origin used to scale/rotate; values are substracted from the input ch2
k - counter clockwise rotation angle
(sX,sY) - multiplication scale factor on each axis; (0,1) shrinks & (1,...) enlarges
(aX,aY) - final translation applied to the new origin; pass same as bX,bY if no additional translation is required!

You can even flip an object using -1 for sX or sY. Use your imagination when passing the parameters and you'll see the use of these functions in many user interface tools.

Sample code (rotates "myPoint" with "kappa" around "cursor"):
...
struct dPoint2d
{
double X;
double Y;
};
...
double trans[9];
double kappa;
dPoint2d cursor,myPoint;
...
prj2dMatrix(trans,cursor.X,cursor.Y,kappa,1,1,cursor.X,cursor.Y);
prj2dTransform(trans,myPoint);
...