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

Translation

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

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. Required fields are marked *

Share