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

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 | #include <iostream> #include <cmath> using namespace std; typedef struct { float x; float y; float z; }Point; Point points; float temp = 0; void showPoint(){ cout<<"("<<points.x<<","<<points.y<<","<<points.z<<")"<<endl; } void translate(float tx, float ty, float tz){ points.x += tx; points.y += ty; points.z += tz; cout<<"After Translation, new point is :"; showPoint(); } 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: "; showPoint(); } 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: "; showPoint(); } 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: "; showPoint(); } 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: "; showPoint(); } 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:"; cin>>points.x>>points.y>>points.z; 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; cin>>choose; switch(choose){ case 1: cout<<"Enter the value of tx, ty and tz: "; cin>>tx>>ty>>tz; translate(tx, ty, tz); break; case 2: cout<<"Enter the angle: "; cin>>angle; rotatex(angle); break; case 3: cout<<"Enter the angle: "; cin>>angle; rotatey(angle); break; case 4: cout<<"Enter the angle: "; cin>>angle; rotatez(angle); break; case 5: cout<<"Enter the value of sf, xf, yf and zf: "; cin>>sf>>xf>>yf>>zf; scale(sf, xf, yf, zf); break; default: break; } return 0; } |

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

Simple and easy to understand!!

Thank You

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.