Transformation (Translation, Rotation and Scaling) of a two dimensional objects in C/C++
destination source:https://www.programming-techniques.com/?p=121
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

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.
The composite transformation matrix for this process is
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.
The composite transformation matrix for this process is
Source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | #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)

Related Article
destination source:https://www.programming-techniques.com/?p=121