**1. Translation**

A translation is applied to an object by repositioning it along a straight-line path from one coordinate location to another. We translate a two-dimensional point by adding translation distances,

*tx*and*ty*, to the original coordinate position (x,y) to move the point to a new position*(x’, y’)**x’ = x + tx, y’ = y + ty*

The translation distance pair

*(tx, ty)*is called a translation vector or shift vector. In matrix form the translation of a two dimensional object can be written as**2. Rotation**

A two dimensional rotation is applied to an object by repositioning it along a circular path in the

*xy*plane. To generate a rotation, we specify a rotation angle*θ*and the position*(xr, yr)*of the pivot point about which the object is to be rotated. In general Pivot – Point rotation, there are 3 steps to rotate an object about pivot point,- Translate the object so that the pivot-point position is moved to the coordinate origin.
- Rotate the object about the coordinate origin.
- Translate the object so that the pivot point is returned to its original position.

**3. Scaling**

A scaling transformation alters the size of an object. This operation can be carried out for polygons by multiplying the coordinate values

*(x, y)*of each vertex by scaling factors

*sx*and

*sy*to produce the transformed coordinates

*(x’, y’).*In General Fixed – Point Scaling, there are 3 steps to scale an object about the fixed point,

- Translate object so that the fixed point coincides with the coordinate origin.
- Scale the object with respect to the coordinate origin.
- Use the inverse translation of step 1 to return object to its original position.

**Source code**

#include <windows.h>#include <cmath>`/* Drawing Parameters */`

`int xa = 10;`

`int ya = 10;`

`int xb = 100;`

`int yb = 10;`

`int xc = 10;`

`int yc = 100;`

`int tx = 50;`

`int ty = 100;`

`float angle = 90;`

`/* Matrix Parameters */`

typedef float Matrix3x3[3][3];Matrix3x3 theMatrix;`/* set window handle */`

`static HWND sHwnd;`

`static COLORREF redColor=RGB(255,0,0);`

`static COLORREF blueColor=RGB(0,0,255);`

`static COLORREF greenColor=RGB(0,255,0);`

`void SetWindowHandle(HWND hwnd){`

sHwnd=hwnd;}`/* SetPixel */`

void setPixel(int x,int y,COLORREF& color=redColor){`if(sHwnd==NULL){`

MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);`exit(0);`

}HDC hdc=GetDC(sHwnd);SetPixel(hdc,x,y,color);ReleaseDC(sHwnd,hdc);`return;`

`// NEVERREACH //`

}`void matrix3x3setIdentity(Matrix3x3 m){`

`int i, j;`

`for(i = 0; i < 3; i++){`

`for(j = 0; j < 3; j++){`

m[i][j] = (i == j);}}}`void matrixMultiply(Matrix3x3 a, Matrix3x3 b){`

Matrix3x3 temp;for(int i = 0; i < 3; i++)for(int j = 0; j < 3; j++)temp[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j];for(int i = 0; i < 3; i++)for(int j = 0; j < 3; j++)b[i][j] = temp[i][j];}void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3){POINT Pt[3];HDC hDC=GetDC(sHwnd);Pt[0].x = x1; Pt[0].y = y1;Pt[1].x = x2; Pt[1].y = y2;Pt[2].x = x3; Pt[2].y = y3;Polygon(hDC, Pt, 3);}`void initializeTheMatrix(Matrix3x3 m){`

m[0][0] = xa; m[1][0] = ya; m[2][0] = 1;m[0][1] = xb; m[1][1] = yb; m[2][1] = 1;m[0][2] = xc; m[1][2] = yc; m[2][2] = 1;}void assignPoints(int &xa, int &ya, int &xb, int &yb, int &xc, int &yc, Matrix3x3 theMatrix){xa = theMatrix[0][0]; ya = theMatrix[1][0];xb = theMatrix[0][1]; yb = theMatrix[1][1];xc = theMatrix[0][2]; yc = theMatrix[1][2];}void translateTriangle(int tx, int ty){Matrix3x3 tVector;matrix3x3setIdentity(tVector);tVector[0][2] = tx;tVector[1][2] = ty;initializeTheMatrix(theMatrix);matrixMultiply(tVector, theMatrix);assignPoints(xa, ya, xb, yb, xc, yc, theMatrix);drawTriangle(xa,ya,xb,yb,xc,yc);}void rotateTriangle(float angle, int rx, int ry){Matrix3x3 rVector;matrix3x3setIdentity(rVector);angle = angle*M_PI/180;rVector[0][0] = cosf(angle);rVector[0][1] = -sinf(angle);rVector[0][2] = rx*(1-cosf(angle)) + ry*sinf(angle);rVector[1][0] = sinf(angle);rVector[1][1] = cosf(angle);rVector[0][2] = ry*(1-cosf(angle)) - rx*sinf(angle);initializeTheMatrix(theMatrix);matrixMultiply(rVector, theMatrix);assignPoints(xa, ya, xb, yb, xc, yc, theMatrix);drawTriangle(xa, ya, xb, yb, xc, yc);}void scaleTriangle(float xf, float yf, float sx, float sy){Matrix3x3 sVector;matrix3x3setIdentity(sVector);sVector[0][0] = sx;sVector[0][2] = (1 - sx)*xf;sVector[1][1] = sy;sVector[1][2] = (1 - sy)*yf;initializeTheMatrix(theMatrix);matrixMultiply(sVector, theMatrix);assignPoints(xa, ya, xb, yb, xc, yc, theMatrix);drawTriangle(xa, ya, xb, yb, xc, yc);}`/* Window Procedure WndProc */`

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){`switch(message){`

`case WM_PAINT:`

SetWindowHandle(hwnd);drawTriangle(xa, ya, xb, yb, xc, yc);translateTriangle(tx, ty);rotateTriangle(angle, 30, 500);scaleTriangle(600, 200, 0.5, 0.5);`break;`

case WM_CLOSE: // FAIL THROUGH to call DefWindowProc`break;`

`case WM_DESTROY:`

PostQuitMessage(0);`return 0;`

`default:`

break; // FAIL to call DefWindowProc //}`return DefWindowProc(hwnd,message,wParam,lParam);`

}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){static TCHAR szAppName[] = TEXT("Transformations");WNDCLASS wndclass;wndclass.style = CS_HREDRAW|CS_VREDRAW ;wndclass.lpfnWndProc = WndProc ;wndclass.cbClsExtra = 0 ;wndclass.cbWndExtra = 0 ;wndclass.hInstance = hInstance ;wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;wndclass.lpszMenuName = NULL ;wndclass.lpszClassName = szAppName ;`// Register the window //`

`if(!RegisterClass(&wndclass)){`

MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);`exit(0);`

}`// CreateWindow //`

`HWND hwnd=CreateWindow(szAppName,"Translation, Rotation and Scaling - Programming Techniques",`

WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);`if(!hwnd){`

MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);`exit(0);`

}`// ShowWindow and UpdateWindow //`

ShowWindow(hwnd,iCmdShow);UpdateWindow(hwnd);`// Message Loop //`

MSG msg;`while(GetMessage(&msg,NULL,0,0)){`

TranslateMessage(&msg);DispatchMessage(&msg);}`/* return no error to the operating system */`

`return 0;`

}

Output (Modified)

Hello Bibek, you're codin skills are top notch, you are truly passionate about programming, and I admire that of you.

ReplyDeleteI was hoping if you can do a C version of this program, cause in class I have to present this topics but the annoying teacher that I have wants it to be compiled on C, so I was wondering if you wouldn't mind doing a C version of this same program.

Again, thank you so much for providing this tutorials :)

What is the matrix for 3D Rotation with Pivot point and 3D Scaling with Pivot point?

ReplyDeletewell thanks for sharing the program is running successfully.

ReplyDeleteNice share bro.

thanks for sharing

ReplyDeletecan this run on ubantu?

ReplyDeleteNo you can't. This program runs only on Windows platform.

DeletePlease can you explain why we taken Xf(1-Sx) and Yf(1-Sy) in composite transformation matrix? what does that calculations means?

ReplyDelete