Transformations

Transformations in Computer Graphics

Transformation means changing the position, size, or orientation of an object. In computer graphics, the three basic 2D transformations are translation, rotation, and scaling.

Published May 28, 2026 - 12 min read

What are transformations in computer graphics?

Transformations are operations applied to object coordinates. Instead of redrawing a completely new object from scratch, we transform the original points and draw the updated shape.

In the programs below, a triangle is used because it has only three points, making the coordinate changes easy to see. The same formulas can be applied to rectangles, polygons, and other 2D objects.

Translation in Computer Graphics

Translation moves an object from one position to another. If a point is (x, y) and the translation distances are tx and ty, the new point is:

x' = x + tx
y' = y + ty

Translation program in C using graphics.h

C / Translation
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>

int main()
{
    int gd = DETECT, gm;
    int x1 = 120, y1 = 120;
    int x2 = 220, y2 = 120;
    int x3 = 170, y3 = 220;
    int tx = 180, ty = 80;

    initgraph(&gd, &gm, "");

    setcolor(WHITE);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
    outtextxy(x1, y1 - 20, "Original Triangle");

    setcolor(GREEN);
    line(x1 + tx, y1 + ty, x2 + tx, y2 + ty);
    line(x2 + tx, y2 + ty, x3 + tx, y3 + ty);
    line(x3 + tx, y3 + ty, x1 + tx, y1 + ty);
    outtextxy(x1 + tx, y1 + ty - 20, "Translated Triangle");

    getch();
    closegraph();
    return 0;
}

Rotation Transformation

Rotation turns an object by an angle. For rotation about the origin, the formula is:

x' = x cos(theta) - y sin(theta)
y' = x sin(theta) + y cos(theta)

The program below rotates a triangle around its first point. It asks for the rotation angle, then draws the original triangle and the rotated triangle in different colors.

Rotation program in C using graphics.h

C / Rotation
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>

#define PI 3.14159

int main()
{
    int gd = DETECT, gm;
    int x1 = 220, y1 = 180;
    int x2 = 320, y2 = 180;
    int x3 = 270, y3 = 280;
    int rx2, ry2, rx3, ry3;
    float angle, radian;

    printf("Enter rotation angle: ");
    scanf("%f", &angle);

    initgraph(&gd, &gm, "");

    setcolor(WHITE);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
    outtextxy(x1, y1 - 20, "Original Triangle");

    radian = angle * PI / 180;

    rx2 = x1 + (x2 - x1) * cos(radian) - (y2 - y1) * sin(radian);
    ry2 = y1 + (x2 - x1) * sin(radian) + (y2 - y1) * cos(radian);

    rx3 = x1 + (x3 - x1) * cos(radian) - (y3 - y1) * sin(radian);
    ry3 = y1 + (x3 - x1) * sin(radian) + (y3 - y1) * cos(radian);

    setcolor(GREEN);
    line(x1, y1, rx2, ry2);
    line(rx2, ry2, rx3, ry3);
    line(rx3, ry3, x1, y1);
    outtextxy(x1, y1 + 15, "Rotated Triangle");

    getch();
    closegraph();
    return 0;
}

Try it online: open the graphics.h online compiler, paste the program, then use sample angle 45.

Scaling Transformation

Scaling changes the size of an object. If sx and sy are scaling factors, the new point is:

x' = x * sx
y' = y * sy

If both scale factors are greater than 1, the object becomes larger. If they are between 0 and 1, the object becomes smaller.

Scaling program in C using graphics.h

C / Scaling
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdio.h>

int main()
{
    int gd = DETECT, gm;
    int x1 = 80, y1 = 80;
    int x2 = 140, y2 = 80;
    int x3 = 110, y3 = 140;
    int sx = 2, sy = 2;

    initgraph(&gd, &gm, "");

    setcolor(WHITE);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
    outtextxy(x1, y1 - 20, "Original Triangle");

    setcolor(GREEN);
    line(x1 * sx, y1 * sy, x2 * sx, y2 * sy);
    line(x2 * sx, y2 * sy, x3 * sx, y3 * sy);
    line(x3 * sx, y3 * sy, x1 * sx, y1 * sy);
    outtextxy(x1 * sx, y1 * sy - 20, "Scaled Triangle");

    getch();
    closegraph();
    return 0;
}

Difference between translation, rotation, and scaling

  • Translation changes only the position of an object.
  • Rotation changes the orientation of an object by turning it through an angle.
  • Scaling changes the size of an object by multiplying its coordinates.

Common mistakes

Forgetting that screen y increases downward

In graphics.h, the top-left corner is (0, 0). The y-coordinate increases downward, so rotation examples often use helper functions to map mathematical coordinates to screen coordinates.

Rotating around the wrong point

The basic rotation formula rotates around the origin. To rotate around another point, translate the object to the origin, rotate it, then translate it back.

Scaling moves the object away from the origin

Basic scaling multiplies coordinates from the origin. If you want to scale around the object's own center, you must use translation before and after scaling.