Transformation (Translation, Rotation and Scaling) of a two dimensional objects in C/C++

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

Loading…
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,
  1. Translate the object so that the pivot-point position is moved to the coordinate origin.
  2. Rotate the object about the coordinate origin.
  3. 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,

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

The composite transformation matrix for this process is

Source code

#include 

#include 


/* 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 

Output (Modified)

Loading...