2d Geometric Transformation
2d Geometric Transformation
• Two dimensional
transformations
• Matrix representations
• Inverse transformations
• Three dimensional
transformations
Vertices
• We have always represented vertices as
(x,y)
• An alternate method is:
• Exampl
e: x
( x, y)
y
2.1
(2.1,4.8)
4.8
Matrix * Vector
x' a b x x' a b c x
y' c y
d f
y
z'y' gd
x' ax x' ax by i z
cz y' dxe
by y' cx
ey fz z'
dy
gx hy izh
0 1 0
I
1
00
0 1 0 1
I 0 1
Matrix * Matrix
a b x
What does the identity
A ,B do?
c d z w
ax bz ay bw
A* B
cx dz cy dw
a b 1 0
? 0
c d 1
Geometric transformation functions
• Translatio
n
• Rotation
• Scaling
• Reflection
• Shearing
Transformations
• Rigid Body Transformations - transformations that do not
change the object.
• Translate
– If you translate a rectangle, it is still a rectangle
• Scale
– If you scale a rectangle, it is still a rectangle
• Rotate
– If you rotate a rectangle, it is still a rectangle
Translation
• Translation - repositioning an object along a straight-line
path (the
translation distances) from one coordinate location to
another.
(x’,y’)
(tx,ty)
(x,y)
2D Translations
x x d x y y d y
Define x vectors
x the column d
P , P ,T x P’
y y d y
P
Now
P P
T
Translation
• Given P (x, y) P (3.7,4.1)
: T T (7.1,8.2)
(t x ,t y )
x' x x' 3.7
• We
ty'x y 7.1
want: y
t y' 4.1
8.2
• Matrix x' 3.7 7.1
x'
x t
y' t y' 4.1 8.2
x
form:
y y x'
P' P
3.4
T
y'
4.1
Translation Examples
• P=(2,4), T=(-1,14), P’=(?,?)
• P=(8.6,-1), T=(0.4,-0.2),
P’=(?,?)
• P=(0,0), T=(1,0), P’=(?,?)
2D Scaling from the origin.
x s x .x, y s y .y
Define the P’
matrix P
sx 0
S
0 s y
Now
x s x 0 x
P S P or y 0 .
s y y
Scale
P (x, y) P
• Given: (1.4,2.2)
S (s x ,
sy ) S (3,3)
x' s x x'
• We 3*1.4
x y'
y' 3*
want: sy y x' 3 031.4
2.2
2.2
y' 0
x' s x 0 x
y' 0 x'
• Matrix form: sy y
4.2
P' S
y'
P
6.6
2D Rotation about the origin.
P’(x’,y’)
P(x,y)
r
r
x
2D Rotation about the origin.
P’(x’,y’)
P(x,y) x
r
r.cos
r
y y
r.sin
x
Rotation
• Rotation - repositions an object along
a circular path.
• Rotation requires an and a pivot
point
Rotation
x r.cos( ) r.cos.cos
Substituting for r :
x r.cos
y r.sin
Gives us :
x x.cos y.sin
2D Rotation about the origin.
x x.cos y.sin
y x.sin
y.cos
Rewriting sin
in matrix .
form gives us : cos y
y
x cos
sin
Define the matrix R ,
cos x
sin sin cos P R P
Example
• P=(4,4)
• =45
degrees
Transformations.
• Translation.
– P=T + P
• Scale
– P=S P
• Rotation
– P=R P
• We would like all transformations to be
multiplications so we can concatenate them
express points in homogenous coordinates.
Homogeneous coordinates
• Add an extra coordinate, W, to a point.
– P(x,y,W).
• Two sets of homogeneous coordinates represent the same point if
they are a multiple of each other.
– (2,5,3) and (4,10,6) represent the same point.
• At least one component must be non-zero (0,0,0) is not defined.
• If W 0 , divide by it to get Cartesian coordinates of point
(x/W,y/W,1).
• If W=0, point is said to be at infinity.
Homogeneous coordinates
• A standard technique to expand each 2D co-ordinate position
representation (x,y) to a 3 element representation (xh,yh,h) called
homogenous coordinate
• If we represent(x,y,h) in 3-space, all triples representing the
same point describe a line passing through the origin.
• If we homogenize the point, we get a point of form (x/h,y/h,h/h)
=(x,y,1) W P
– homogenised points form a plane at h=1.
W=1 plane
Y
Translations in homogenised
coordinates
x 1 0 d x x x x d x
y 0 d y . y
1 y y d y
1 0
0 1 11
1
Concatenation.
P P T (d x 2 , d y 2 )
P P T (dx1 , d y 1 ) T (d x 2 , d y 2 ) P T (dx1 d x 2 ,
dy 1 d y 2 )
So we expect :
T (dx1 , d y 1 ) T (d x 2 , d y 2 ) T (dx1 d x 2 , d y 1 d y 2 )
Concatenation.
1. T (0,0) I
2. T (s x , s y ) T (t x , t y ) T (s x t x , s y
ty )
3.T (s x , s y ) T (t x , t y ) T (t x , t y ) T
(s x , s y ) 4. T-1 (s , s ) T (s
,s )
Note : 3. translation matrices are
commutative.
Homogeneous form of Scale.
In homogeneous coordinates :
sx 0 0
S (sx , sy ) 0 sy 0
0 1
0
Concatenation of Scales.
x cos sin 0 x
y sin cos 0 . y
1 0
0 1
For rotation matrices, 1
R 1 ( ) R( ).
Rotation matricesare orthogonal, i.e
:
R 1 ( ) R T ( )
i.e. the inverse is the transpose
Orthogonality of rotation matrices.
R(0) I
R( ) R( ) R( )
and
R( ) R( ) R( ) R( )
only if the centresof rotation are the
same, Otherwise order matters.
2D Composite Transformations
• Combine transformations of different type
– translate, rotate, translate
– translate, scale, translate
– translate, reflect, translate
• Use to rotate or scale an object w.r.t. a point that is not
the origin
• Implement by multiplication of the corresponding
homogeneous matrices
Translation
• Given
P (3.7,4.1)
: P (x, y) T (7.1,8.2)
T (t x , t y
• We ) x' 3.7
want: x' x 7.1
tx y' 4.1
• Matrix y' y 8.2
form: ty
x' x t x x' 3.7 7.1
y' 4.1 8.2
y' y t
y
P' P x'
T 3.4
Transformation in Homogeneous Coordinates
x’=x+
tx 2D-Translation
y’=y+ty
1 0 x' 1 0 tx x
y ' 0 ty y
1
0 1 1 0
tx 0 1 1
T 0 1 ty 1 0 2 2 4 4 2 4 6 6 4
(2,2
0 1 3 2 1 1 5
(4,2
) 0 )
5 4 4
0 1 1= 1
0 2 1 1 1 1 1
1
(4,5 (6,5
) )
(4,1
(2,1 )
)
tx=2, 3
ty=3 (4,4
(6,4 8
Scaling
Scaling is a non rigid body
transformation, transformed object
can be bigger or smaller Scaling
transformation can be
a) Fixed point ,
b) Direction of the scaling,
c) Scaling factor( )
For > 1, the object gets longer
in the specified direction. x' x
For 0<= < 1, the object gets smaller x y'
in that direction. y y
The negative value of gives the x' x 0 x
reflection about a fixed point in the 4
y' 0 y y 0
Scaling transformation in
Homogeneous
Coordinates
(2,2 (4,2
)
x 0
)
S 0 0 y 0
0 1
0 (4,1
(2,1 )
)
2 0 0 2 4 4 2 4 8 8
0 0 1
1
0 4
1 1 1
1 1 2
2 1 21 2 1
0 1 1 1 1
40
Transformations Concatenation
Concatenation – q=Mp
well known
strategy q = C B
Ap
interpretatio
n
q = C (B (A p) )
Transformati
on
41
Scaling with a arbitrary fixed point
Move to Scal Move
origin e back
x f , yf
4
4
1 0 xf S x 0 0 1 0 x f S x 0 x f 1 S x
0
1
yf
S 0 1 y
f 0 Sy
yf
y
y
1 0 1 S
0 0
0 1
0 0 0
0
1 0 1
0
Scaling with a arbitrary fixed point
4,8
1 0 x f S x 0 0 1 0 x f S
x 0 x f 1 Sx
0 0
1 y Sy
0 0 1 y
y f 1 Sy
3,
6 f
0 0 1
f
0 1
5
4
0 Step1:0Move 1toorigin
0 0 translate (inverse
2, 6, translate)
0 Sy
4 4 1
0
Step2 :Scale at the origin sx=2,
1 0 3 2
sy =3
0 6 8 2
0 1 6 14 1 1
2 0 0 1 3 1 2 6 4 2
1 1
0 0 2 2 6 3 1 1
0 1 1 1 Step3: translate back to original
2 6 position
4
1 3 2 1 6
0 1 1 1 0 3 2 6 2 1 92 5
0 6 6
0
0 1 1
0 1 112 1
6
Scaling with a arbitrary fixed point
4,8
3,
6
1 0 x f S x 0 0 1 0 x f
S
x 0 x f 1 Sx
0 0
1 y Sy 0 0 1 y y f 1 Sy
f
0 0 1
0 f 1
0 1
0 1
0 0 Sy
2 0 3 20 6
4 1 9 05
0 12 8 0 12
1 1 1
4 0 4
0 3 41 6
2D Rotation about a fixed point
Move to Rotat Move
origin e back
xr , y r
1 0 xr c o s sin 0 1 0 xr
0 1 y r s i n c o s 0 0 1y r
0 1 0 1 0 10
0 0
cos
sin x r 1 c o s y r s i n
s i n y r 1 c o s x r s i n
0 cos
1 5
0 1
Transformation in Homogeneous Coordinates
y y
Sy
z x z x
Enlarging object also moves it from
z x
origin Sz
x Sx 0 0 0 x
y 0 0
Sy 0 y
P z 0 S
1 0 0 Sz P
0 z
0 0
1
1
Scaling transformation in
Homogeneous
Coordinates
Scaling matrix with fixed point of the origin
allows for independent scaling along the
coordinate
The equation axes.
(in 3
D x
2D) x’= x x x’= x
y’=y
y’= y y y
z’= z
x 0 x 0 0
z
0
S 0 0 y 0 y
S 0
0 1 0 0
0 0
0 0
1
0
Inversion S-1 = S ( 1/x , 1/ y , z1/ 4
1
operations:
Scaling with respect to a fixed
point (not necessarily of
object)y
y y
x , yf , z x , yf , z x , yf , z
z f f
z f f
z f f
y
x x S x 0 0 1 S x xx f
T S T 1
Sy 1 0S y yf
0 0 0 Sz 1 S z
f
x , yf , z 0 1
z f f
z
0 0
4
x 8
Rotation
Rotation of object by an angle ϴ
Consider a point (x,y) to rotated about the origin,
to a new location (x’,y’) with an angle theta.
The polar form of (x,y) and (x’,y’)
x = r cos y = r sin
x’ = r cos (+) y’ = r sin (+)
x’ = r cos cos - r sin sin
= x cos - y sin
y’ = r cos sin - r sin
cos
= x sin + y cos
x' cos
sin cos
sin xy
y '
4
9
Rotation
Three features of Rotation
1)Rotation about fixed
point figure shows
rotation about a
fixed point is in the
center of the object.
cos
0
55
3D Rotation About a Fixed Point
Rz Rx
4
7
with one of Cartesian axes.
3. Perform specified rotation about the Cartesian axis.
4. Apply inverse rotation to return rotation axis to
original direction.
5. Apply inverse translation to return rotation axis to
original position.
Rotations About
an Arbitrary
Given:
Axis
• points p1 , p2 and rotation angle
• objects to be rotated, p0 is
fixed point, center of the
cube.
Define
vectors
u = p1 - p2 -
andv= ux/ ,| y normalize
v =[ , z ]T
u| + y +d z = 1 – directional
cosines
x
2 2 2
Transformation
R = Rx(-x) Ry(-y) Rz() Ry(y)
Rx(x)
70
Rotations About an Arbitrary Axis
• Object is moved to
the origin
• Rotation about x
axis
1 0 0 0
Rx x 0 0
z / d y / d
0 y / d z / d 10
d 2
2 0 0 0
y
71
z
Rotations About an Arbitrary Axis
• Object is moved to
the origin
• Rotation about y
axis note the “-”
position
d 0 x 0
R y y 0 0
1 0
x
0 d 0
0 0 0 1
Complete transformation
M = T(p0) Rx(-x) Ry(-y) Rz() Ry(y) Rx(x) T(-
p0 ) 72
2D Reflections
y y y
x x
1 0 0 1 0 0 1 0 0
0 0
0
1 0 0 0
1 1
0 0 1
0 0 1 0 0 1
8
0
The instance Transformation
Transformation is defined
by the instance
transformation M
M=TRS
(order is substantial!)
Each occurrence of an
object in the scene is an
instance of the object’s
prototype
To obtain proper size,
location, orientation –
instance transformation
74
to the prototype is to be
OpenGL Transformation Matrices
CM loading
C CM post-multiplication
76
OpenGL Transformation Matrices
glLoadMatrixf(pointer_to_matrix);
/* vector of 16 position – column first order */
77
OpenGL Transformation Matrices
79
Transformation Order
The sequence specified
recently:
C I, initializati
C CT (4.0, 5.0, on
6.0),
C CR (45.0, 1.0, 2.0, 3.0),translatio
rotation
C CT n translation back
(-4.0, -5.0, -6.0),
in each step the CTM matrix is post-multiplied
forming new CTM matrix
C = T (4.0, 5.0, 6.0) R(45.0, 1.0, 2.0, 3.0) T (-4.0, -
5.0, -
6.0)
Each vertex specified after the model-view
matrix has been specified will be
80
multiplied
Loading, Pushing & Popping
glLoadMatrixf(myarray);
/* 4 x 4 matrix of floats -column first order from
a vector
*/
glMultMatrixf(myarray);
/* multiplies the current matrix by user
specified matrix
*/
Sequence example
GLfloat myarray
[16];
for ( i=0; i<3; i+
81
+) for ( j=0;
Loading, Pushing & Popping
Sometimes it is reasonable to return the
transformations back after they have been
applied to some objects.
Instead of re-computation the stack mechanism
can be
utilized
glPushMatrix ( );
/* local transformation specifications
*/ glTranslatef ( .....); ................
/* DRAW OBJECTS HERE */
................
/* recover recent state */
82
glPopMatrix ( );
The LookAt Function
• GLU library contains the function (gluLookAt) to
form the modelview matrix through a simple
interface.
CS4395: Computer
Graphics 84
OpenGL Perspective
glFrustum(left,right,bottom,top,near,far)
CS4395: Computer
Graphics 85
Using Field of View
• With glFrustum it is often difficult to
get the desired view.
• gluPerpective(fovy, aspect, near,
far) often provides a better
interface.
front plane
aspect = w/h
| b y 2 y1 | c
x
2 b c
2 2
V a| c z 2 z1
1 z
|V|
87
Rotating u to coincide with z axis
cos c b c
2
2
c d , sin b d
.
b2
W e obtained a unit vector w a, c2 d in x z plane.
0,
y
1 0 0 0
u
0
R x 0 c d 0 x
0
b u a, b, c
1
d b d z
c d
88
0
Rotate w counterclockwise around y axis.
sin 0 0
R z 0 0
cos 0
0
0 11
0 0
R 1
1
1
x y z
Ry Rx
T R R R
T
M R
ab 1 cos c ac 1 cos b
a 1 cos
2
ca1 cos b
ba 1 cos c bc 1 cos a sin
cos
sin b 2 1 cos c2
sin 1 cos cos
sin
sin
cos cb 1 cos
a sin
April 9
2010 7
Reflection
sx = -1 sy = 1 original
sx = -1 sy = -1 sx = 1 sy = -1
Reflection
•A transformation that produces a mirror
after shear, as dx/dy
Shear
A transformation that distorts the shape of an object such that
the transformed shape appears as if the object were
composed of internal layers that had been caused to slide
over each other is called a shear.
GLfloat house[3]
[9]=
GLfloat rotatemat[3][3]={{0},
{{100.0,100.0,17
{0},{0}};
5.0,250.0,250.0,1
GLfloat result[3][9]={{0},
50.0,150.0,200.0,
{0},{0}}; GLfloat
200.0},
5
arbitrary_x=100.0;
{100.0,300.0,40 GLfloat 2
2D Rotation about a fixed point
void multiply()
{
int i,j,k;
for(i=0;i<3;i
++)
for(j=0;j
<9;j++)
{
result[i][j]=0;
for(k=0;k<3;k
++)
result[i][j]=result[i][j]
+rotatemat[i] 10
7
2D Rotation about a fixed point
void multiply()
{
int i,j,k;
for(i=0;i<3;i
++)
for(j=0;j
<9;j++)
{
result[i][j]=0;
for(k=0;k<3;k
++)
result[i]
[j]=result[i][j]
10
+rotatemat[i] 8
2D Rotation about a fixed point
void rotate()
{
GLfloat m,n;
m=-arbitrary_x*(cos(rotation_angle) -1) +
arbitrary_y * (sin(rotation_angle));
n=-arbitrary_y * (cos(rotation_angle) - 1) -
arbitrary_x * (sin(rotation_angle));
rotatemat[0]
[0]=cos(rotation_angle);
rotatemat[0][1]=-
sin(rotation_angle);
rotatemat[0][2]=m;
rotatemat[1]
[0]=sin(rotation_angle);
rotatemat[1]
[1]=cos(rotation_angle);
rotatemat[1][2]=n;
rotatemat[2][0]=0; 5
5
2D Rotation about a fixed point
void drawhouse()
{
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP
);
glVertex2f(house[0]
[0],house[1][0]);
glVertex2f(house[0]
[1],house[1][1]);
glVertex2f(house[0]
[3],house[1][3]);
glVertex2f(house[0]
glBegin(GL_LINE_LOOP);
[4],house[1][4]); glEnd();
glVertex2f(house[0]
[5],house[1][5]);
glVertex2f(house[0][6],house[1]
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
[6]);
glVertex2f(house[0]
glVertex2f(house[0][7],house[1]
[1],house[1][1]);
[7]);
glVertex2f(house[0][2],house[1]
glVertex2f(house[0]
glColor3f(0.0, 0.0,
[2]);
[8],house[1][8]); glEnd();
1.0);
glVertex2f(house[0]
[3],house[1][3]); glEnd();
}
11
0
2D Rotation about a fixed point
glutInitDisplayMode(GLUT_SINGLE|
GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("house rotation");
glutDisplayFunc(display);
11
}
myinit(); 3