3D Transformation [Translation, Rotation and Scaling] in C/C++


In a three-dimensional homogeneous coordinates representation, a point is translated from position P = (x, y, z) to position P = (x’, y’, z’) with the following equations.
x’ = x + tx
y’ = y + ty
z’ = z + tz

Rotation about axes

To generate a rotation transformation for an object, we must designate an axis of rotation (about which the the object is to be rotated) and the amount of angular rotation. Unlike 2D applications, where all transformations are carried out in the xy plane, a three-dimensional rotation can be specified around any line in space. I will cover rotation about arbitrary axis in another post, here the discussion is restricted to rotation about coordinate axes.
About x – axis

If a point (x, y, z) is rotated through angle θ about x – axis to a new point (x’, y’, z’) then the new point is calculated as
y’ = y cosθ – z sinθ
z’ = y sinθ + z cosθ
x’ = x

About y – axis

z’ = z cosθ – x sinθ
x’ = z sinθ + x cosθ
y’ = y

About z – axis

x’ = x cosθ – y sinθ
y’ = x sinθ + y cosθ
z’ = z

Scaling with respect  a selected fixed position (xf, yf, zf) can be represented with the following transformation sequence:

1. Translate the fixed point to the origin
2. Scale the object relative to the coordinate origin
3. Translate the fixed point back to its original position

The equations for this sequence of transformation is (where s is scaling factor)

x’ = x * s + (1 – s) * xf
y’ = y *s + (1 – s) * yf
z’ = z * s + (1 – s) * zf

Source Code

#include <iostream>
#include <cmath> 
using namespace std; 
typedef struct {
    float x;
    float y;
    float z;
Point points; 
float temp = 0; 
void showPoint(){
void translate(float tx, float ty, float tz){
    points.x += tx;
    points.y += ty;
    points.z += tz;
    cout<<"After Translation, new point is :";
void rotatex(float angle){
    angle = angle * M_PI / 180.0;
    temp = points.y;
    points.y = points.y * cos(angle) - points.z * sin(angle);
    points.z = temp * sin(angle) + points.z * cos(angle);
    cout<<"After rotation about x, new point is: ";
void rotatey(float angle){
    angle = (angle * M_PI) / 180.0;
    temp = points.z;
    points.z = points.z * cos(angle) - points.x * sin(angle);
    points.x = temp * sin(angle) + points.x * cos(angle);
    cout<<"After rotation about y, new point is: ";
void rotatez(float angle){
    angle = angle * M_PI / 180.0;
    temp = points.x;
    points.x = points.x * cos(angle) - points.y * sin(angle);
    points.y = temp * sin(angle) + points.y *cos(angle);
    cout<<"After rotation about z, new point is: ";
void scale(float sf, float xf, float yf, float zf){
    points.x = points.x * sf + (1 - sf) * xf;
    points.y = points.y * sf + (1 - sf) * yf;
    points.z = points.z * sf + (1 - sf) * zf;
    cout<<"After scaling, new point is: ";
int main()
    float tx = 0, ty = 0, tz = 0;
    float sf = 0, xf = 0, yf = 0, zf = 0;
    int choose;
    float angle;
    cout<<"Enter the initial point you want to transform:";
    cout<<"Choose the following: "<<endl;
    cout<<"1. Translate"<<endl;
    cout<<"2. Rotate about X axis"<<endl;
    cout<<"3. Rotate about Y axis"<<endl;
    cout<<"4. Rotate about Z axis"<<endl;
    cout<<"5. Scale"<<endl;
        case 1:
            cout<<"Enter the value of tx, ty and tz: ";
            translate(tx, ty, tz);
        case 2:
            cout<<"Enter the angle: ";
        case 3:
            cout<<"Enter the angle: ";
        case 4:
            cout<<"Enter the angle: ";
        case 5:
            cout<<"Enter the value of sf, xf, yf and zf: ";
            scale(sf, xf, yf, zf);
    return 0;
SHARE 3D Transformation [Translation, Rotation and Scaling] in C/C++

You may also like...

4 Responses

  1. What an impressive source code, I was wondering if you have a C version of this same code?

    thank you so much for providing these amazing tutorials!.

  2. Simple and easy to understand!!

  3. Abuh Usman says:

    Please i don,t understand how it work, it does not display any shape or any actual rotation or scaling on the screen.it only request for value and display the putout in text format on the screen.
    In my school i have an assignment to write a c++ program to apply scaling and rotation to 3d dimensional shapes , please put me through.

Leave a Reply

Your email address will not be published.