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
#include
#include
using namespace std;
typedef struct {
float x;
float y;
float z;
}Point;
Point points;
float temp = 0;
void showPoint(){
cout
void translate(float tx, float ty, float tz){
points.x += tx;
points.y += ty;
points.z += tz;
cout
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
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
}
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
}
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
int main()
{
float tx = 0, ty = 0, tz = 0;
float sf = 0, xf = 0, yf = 0, zf = 0;
int choose;
float angle;
cout>points.x>>points.y>>points.z;
cout>choose;
switch(choose){
case 1:
cout>tx>>ty>>tz;
translate(tx, ty, tz);
break;
case 2:
cout>angle;
rotatex(angle);
break;
case 3:
cout>angle;
rotatey(angle);
break;
case 4:
cout>angle;
rotatez(angle);
break;
case 5:
cout>sf>>xf>>yf>>zf;
scale(sf, xf, yf, zf);
break;
default:
break;
}
return 0;
}