Docsity

Geometric Transformations in Computer Graphics: Translation, Scaling, and Rotation, Lecture notes of Computer Science

Toggle sidebar
bg1
Computer Graphics
1
Chapter Three
Transformations and Clipping
3.1. Basic transformation
Changes in orientation, size, and shape are accomplished with geometric transformations that
alter the coordinate descriptions of objects. The basic geometric transformations are translation,
rotation, and scaling.
3.1.1. Translation
A translation is applied to an object by repositioning it along a straight-line path from one
coordinate location to another. We translate a two-dimensional point by adding translation
distances tx and ty to the original coordinate position (x, y) to move the point to a new position
(x',y').
x' = x + tx y' = y + ty…………………… equation 3-1
The translation distance pair (tx, ty) is called a translation vector or shift vector. We can express
the translation equations 3-1 as a single matrix equation by using column vectors to represent
coordinate positions and the translation vector:
P=[𝑥1
𝑥2] P’= [𝑥1
′
𝑥2
′] T=[𝑡𝑥
𝑡𝑦]………………. Equation 3-2
This allows us to write the two-dimensional translation equations in the matrix form:
P’=P+T…………….. equation 3-3
Translation is a rigid-body transformation that moves objects without deformation. That is, every
point on the object is translated by the same amount.
Example: translating a star made up of line strips
#include <iostream.h>
#include<GL/glut.h>
#include<math.h>
float tranX(float x,float i)
{ float nx;
nx=x+i;
return nx; }
float tranY(float y,float i)
{ float ny;
ny=y+i;
return ny; }
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Geometric Transformations in Computer Graphics: Translation, Scaling, and Rotation and more Lecture notes Computer Science in PDF only on Docsity!

Chapter Three

Transformations and Clipping

3.1. Basic transformation

Changes in orientation, size, and shape are accomplished with geometric transformations that alter the coordinate descriptions of objects. The basic geometric transformations are translation, rotation, and scaling. 3.1.1. Translation A translation is applied to an object by repositioning it along a straight-line path from one coordinate location to another. We translate a two-dimensional point by adding translation distances tx and ty to the original coordinate position (x, y) to move the point to a new position (x',y'). x' = x + tx y' = y + ty…………………… equation 3 - 1 The translation distance pair (tx, ty) is called a translation vector or shift vector. We can express the translation equations 3 - 1 as a single matrix equation by using column vectors to represent coordinate positions and the translation vector: P=[

𝑥 2 ]^ P

’ = [𝑥^1

′ 𝑥 2 ′^

] T=[

𝑡𝑦]………………. Equation 3-^2 This allows us to write the two-dimensional translation equations in the matrix form: P’=P+T…………….. equation 3- 3 Translation is a rigid-body transformation that moves objects without deformation. That is, every point on the object is translated by the same amount. Example: translating a star made up of line strips #include <iostream.h> #include<GL/glut.h> #include<math.h> float tranX(float x,float i) { float nx; nx=x+i; return nx; } float tranY(float y,float i) { float ny; ny=y+i; return ny; }

void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,0.0); glLineWidth(2.0); //original star made up of line strip glBegin(GL_LINE_STRIP); glVertex2f(0.37,0); glVertex2f(-0.37,0); glVertex2f(0.19,-0.37); glVertex2f(0.0,0.37); glVertex2f(-0.19,-0.37); glVertex2f(0.37,0); glEnd(); float i=0.6; glColor3f(1.0,1.0,0.0); glLineWidth(2.0); //translated star by 0.6 in the x and y direction glBegin(GL_LINE_STRIP); glVertex2f((tranX(0.37,i)),(tranY(0,i))); glVertex2f((tranX(-0.37,i)),(tranY(0,i))); glVertex2f((tranX(0.19,i)),(tranY(-0.37,i))); glVertex2f((tranX(0.0,i)),(tranY(0.37,i))); glVertex2f((tranX(-0.19,i)),(tranY(-0.37,i))); glVertex2f((tranX(0.37,i)),(tranY(0,i))); glEnd(); glFlush();} void init() { glClearColor(0.2,0.4,0.7,0.0);} int main(int argc, char **argv) { glutInit(&argc,argv); glutCreateWindow("Star translation"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }

glVertex2f(0.5,0.5); glVertex2f(0.5,-0.5); glEnd(); glBegin(GL_LINES); glVertex2f(0.0,1.0); glVertex2f(0.0,-1.0); glVertex2f(-1.0,0.0); glVertex2f(1.0,0.0); glEnd(); glColor3f(1.0,1.0,1.0); glBegin(GL_POLYGON); glVertex2f((scaleX(-0.5,0.5)),(scaleY(-0.5,-0.5))); glVertex2f((scaleX(-0.5,0.5)),(scaleY(0.5,-0.5))); glVertex2f((scaleX(0.5,0.5)),(scaleY(0.5,-0.5))); glVertex2f((scaleX(0.5,0.5)),(scaleY(-0.5,-0.5))); glEnd(); glFlush();} void init() {glClearColor(0.2,0.4,0.7,0.0); } int main(int argc, char **argv) { glutInit(&argc,argv); glutCreateWindow("Scaling a square"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; } 3.1.3. Rotation A two-dimensional rotation is applied to an object by repositioning it along a circular path in the xy plane. To generate a rotation, we specify a rotation angle θ and the position (xr, yr) of the rotation point (or pivot point) about which the object is to be rotated. Positive values for the rotation angle define counterclockwise rotations about the pivot point, as in Fig. 3 - 1 , and negative values rotate objects in the clockwise direction.

Figure 3-1: Rotation of an object through angle θ about the pivot point (xr, yr) We first determine the transformation equations for rotation of a point position P when the pivot point is at the coordinate origin. The angular and coordinate relationships of the original and transformed point positions are shown in Fig. 3 - 2. In this figure, r is the constant distance of the pint from the origin, angle ∅ is the original angular position of the point from the horizontal, and θ is the rotation angle. Using standard trigonometric identities, we can express the transformed coordinates in terms of angles θ and ∅ as: Figure 3- 2 ------------ Equation 3 - 6 The original coordinates of the point in polar coordinates are: ……………………… Equation 3- 7

glVertex2f(0.0,0.0); glVertex2f(0.5,0.5); glVertex2f(0.9,0.0); glEnd(); glBegin(GL_TRIANGLES); glColor3f(0.3,0.3,0.5); glVertex2f((traX(0.0,0.0,90.0)),(traY(0.0,0.0,90.0))); glVertex2f((traX(0.5,0.5,90.0)),(traY(0.5,0.5,90.0))); glVertex2f((traX(0.9,0.0,90.0)),(traY(0.9,0.0,90.0))); glEnd(); glFlush(); } void init() { glClearColor(0.2,0.4,0.7,0.0);} int main(int argc, char **argv) { glutInit(&argc,argv); glutCreateWindow("Rotating triangle"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }

3.2. Matrix Representation

Translation We expressed the translation equation as single matrix equation by using column vectors to represent coordinate positions and the translation vector in section 3.1 like the following: p’ = p + t=[

]= [

𝑦]^ +[

]

Rotation Matrix representation of rotation for a point p(x,y) is: [

]=[

cos 𝜃 − sin 𝜃 sin 𝜃 cos 𝜃

]* [

𝑦]

Scaling Matrix representation of scaling for a point p(x,y) is: [

]=[

] ∗ [

𝑦]

OpenGL manages two 4 × 4 transformation matrices: the modelview matrix (Model related operations: glBegin, glEnd,glTranslate, glRotate, glScale, gluLookAt) , and the projection

matrix(glViewport,gluPerspective/glOrtho/glFrustum…). Whenever you specify geometry (using glVertex), the vertices are transformed by the current modelview matrix and then the current projection matrix. Hence, you don’t have to perform these transformations yourself. You can modify the entries of these matrices at any time. OpenGL provides several utilities for modifying these matrices. The modelview matrix is normally used to represent geometric transformations of objects; the projection matrix is normally used to store the camera transformation. To modify the current matrix, first specify which matrix is going to be manipulated: use glMatrixMode(GL MODELVIEW) to modify the modelview matrix. The modelview matrix can then be initialized to the identity with glLoadIdentity(). The matrix can be manipulated by directly filling its values, multiplying it by an arbitrary matrix, or using the functions OpenGL provides to multiply the matrix by specific transformation matrices (glRotate, glTranslate, and glScale). OpenGL provides a stack to assist with hierarchical transformations. There is one stack for the modelview matrix and one for the projection matrix. OpenGL provides routines for pushing and popping matrices on the stack. Eg. The following example demonstrates using openGL transformation functions #include<iostream.h> #include <GL/glut.h> #include<math.h> float i=0.1; float j=-0.1; float angle=30; float scale =0.1; void update(int value) { i += 0.2; j-=0.2; if(i>1) i=0; if(j<-1) j=0; angle+=50; if(angle>360) angle-=360; scale+=0.1; if (scale>0.6) scale=0.1; glutPostRedisplay(); glutTimerFunc(1000, update, 0);}

glEnd(); glPopMatrix(); glFlush();} void init() {glClearColor(0.0, 0.0, 0.0, 0.0);} int main(int argc, char** argv) { glutInit(&argc,argv); glutInitWindowSize(500, 500); glutCreateWindow("simple"); glutDisplayFunc(display); init(); glutTimerFunc(1000, update, 0); glutMainLoop();}

3.3. Homogenous co-ordinates

Translation

The translation matrix for translating a vector by (X,Y,Z) is : Without the fourth column and the bottom 1 value a translation wouldn't have been possible.

Scaling

The scaling matrix that scales a given vector by (SX,SY,SZ)is given by: If you think about it for a moment, you can see that scaling would also be possible with a mere 3 - by-3 matrix.

Rotation

The matrices for rotating around the three axes are specified here. The rotation angle is indicated

by the theta (θ).

Rotation around X-axis: Don't worry about understanding the actual geometry behind this, explaining that is beyond the scope of this guide. What matters is that you have a solid idea of how a rotation is described by a rotation axis and an angle and that you've at least seen what a rotation matrix looks like.

Matrix-Matrix product

In the previous section you've seen how transformation matrices can be used to apply transformations to vectors, but this by itself is not very useful. It clearly takes far less effort to do a translation and scaling by hand without all those pesky matrices!