1 Computer Graphics Chapter 6 2D Transformations.
-
Upload
dennis-barber -
Category
Documents
-
view
239 -
download
2
Transcript of 1 Computer Graphics Chapter 6 2D Transformations.
1
Computer Graphics
Chapter 62D Transformations
[6]-2RM
Transform every point on an object according to certain rule.
Initial Object
Transformed Object
P (x,y)
Q (x’, y’)
x x’y y’
Transformation
T
The point Q is the image of P under the transformation T.
[6]-3RM
x x t
y y tx
y(10,5) (30,5)
(20,35)
(55,60)
(65,30)(45,30)
Translation
The vector (tx, ty) is called the offset vector.
[6]-4RM
Translation (OpenGL)
Specifying a 2D-Translation:
glTranslatef(tx, ty, 0.0);
(The z component is set to 0 for 2D translation).
[6]-5RM
(x,y)
(x’,y’)
x x y
y x y
cos sin
sin cos
o
(x,y)(x’,y’)
x
y
Rotation About the Origin
The above 2D rotation is actually a rotation about the z-axis (0,0,1) by an angle .
[6]-6RM
Rotation About the Origin
Specifying a 2D-Rotation about the origin:
glRotatef(theta, 0.0, 0.0, 1.0);
theta: Angle of rotation in degrees.The above function defines a rotation about the z-axis (0,0,1).
[6]-7RM
(xp , yp)
(x,y)(x’,y’)
Pivot Point
•Pivot point is the point of rotation•Pivot point need not necessarily be on the object
Rotation About a Pivot Point
[6]-8RM
(xp , yp)
p
p
yyy
xxx
1
1
(x,y)
(x1, y1)
STEP-1: Translate the pivot point to the origin
Rotation About a Pivot Point
[6]-9RM
cos1sin12
sin1cos12
yxy
yxx
(x1, y1)
STEP-2: Rotate about the origin
(x2, y2)
Rotation About a Pivot Point
[6]-10RM
p
p
yyy
xxx
2
2
STEP-3: Translate the pivot point to original position
(x2, y2)
(xp, yp)
(x’, y’)
Rotation About a Pivot Point
[6]-11RM
x x x y y x
y x x y y y
p p p
p p p
( ) cos ( ) sin
( ) sin ( ) cos
Specifying a 2D-Rotation about a pivot point (xp,yp):
glTranslatef(xp, yp, 0);glRotatef(theta, 0, 0, 1.0);glTranslatef(-xp, -yp, 0);
Note the OpenGL specification of the sequence of transformations in thereverse order !
Rotation About a Pivot Point
[6]-12RM
x x s
y y sx
y
.
.
s sx y
Uniform Non-Uniform
s sx y( , )s sx y 0
(x,y)
(x’,y’)
(x,y)
(x’,y’)
Scaling About the Origin
The parameters sx, sy are called scale factors.
[6]-13RM
Specifying a 2D-Scaling with respect to the origin:
glScalef(sx, sy, 1.0);
sx, sy: Scale factors along x, y.For proper scaling sx, sy must be positive.For 2D scaling, the third scale factor must be set to 1.0.
Scaling About the Origin
[6]-14RM
(xf , yf )
fyf
fxf
ysyyy
xsxxx
).(
).(
(x,y)
(x’,y’)
• Translate the fixed point to origin• Scale with respect to the origin•Translate the fixed point to its original position.
Scaling About a Fixed Point
[6]-15RM
Reflections
InitialObject
Reflection about x
y = y
x
y
Reflection aboutorigin
x = x y = y
Reflection about y
x = x
[6]-16RM
Reflections
Reflection about x: glScalef(1, -1, 1);Reflection about y: glScalef(-1, 1, 1);Reflection about origin: glScalef(-1, -1, 1);
[6]-17RM
Shear
x x h y
y yx
•A shear transformation in the x-direction (along x) shifts the points in the x-direction proportional to the y-coordinate. •The y-coordinate of each point is unaffected.
[6]-18RM
x
y
x
y
t
t
x
y
x
y
x
y
s
s
x
y
x
y
x
y
cos sin
sin cos
0
0
Translation
Rotation [Origin]
Scaling [Origin]
Matrix Representations
[6]-19RM
y
x
y
x
y
x
y
x
y
x
y
x
10
01
10
01
10
01Reflection about x
Matrix Representations
Reflection about y
Reflection about the Origin
[6]-20RM
y
x
hy
x
y
xh
y
x
1
01
10
1Shear along x
Matrix Representations
Shear along y
[6]-21RM
Homogeneous CoordinatesTo obtain square matrices an additional row was added to the matrix and an additional coordinate, the w-coordinate, was added to the vector for a point.
In this way a point in 2D space is expressed in three-dimensional homogeneous coordinates.
This technique of representing a point in a space whose dimension is one greater than that of the point is called homogeneous representation. It
provides a consistent, uniform way of handling affine transformations.
[6]-22RM
Cartesian Homogeneous
0),,,(,
0),,,(),(
ccbac
b
c
a
hhyhxhyx
Examples: (5, 8) (15, 24, 3) (x, y) (x, y, 1)
Homogeneous Coordinates•If we use homogeneous coordinates, the geometric transformations given above can be represented using only a matrix pre-multiplication.• A composite transformation can then be represented by a product of the corresponding matrices.
[6]-23RM
x
y
t
t
x
y
x
y
x
y
x
y
s
s
x
y
x
y
x
y
1
1 0
0 1
0 0 1 1
1
0
0
0 0 1 1
1
0 0
0 0
0 0 1 1
cos sin
sin cos
TranslationP’=TP
Rotation [O]P’=RP
Scaling [O]P’=SP
Basic Transformations
Homogeneous Coordinates
[6]-24RM
1
][
1
y
x
Ty
xIf,
1
][
1
1 y
x
Ty
xthen,
)()(
1,
1),(
)()(
),(),(
1
1
1
1
hHhH
ssSssS
RR
ttTttT
xx
yxyx
yxyx
Examples:
Inverse of Transformations
[6]-25RM
),(),(),(
1)(
)()()(
),(),(),(
2121
yyxxyxyx
yyxxyxyx
wswsSwwSssS
R
RRR
ututTuuTttT
Additional Properties:
Transformation Matrices
[6]-26RM
Transformation T followed by Transformation Q followed by Transformation R:
1
]][][[
1
y
x
TQRy
x
Example: (Scaling with respect to a fixed point)
x
y
x
y
s
s
x
y
x
yf
f
x
y
f
f
1
1 0
0 1
0 0 1
0 0
0 0
0 0 1
1 0
0 1
0 0 1 1
Composite Transformations
Order of Transformations
[6]-27RM
In composite transformations, the order of transformations is very important.
Order of Transformations
Rotation followed by Translation:
Translation followed by Rotation:
[6]-28RM
OpenGL postmultiplies the current matrix with the new transformation matrix
Order of Transformations (OpenGL)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(tx, ty, 0);
glRotatef(theta, 0, 0, 1.0);
glVertex2f(x,y);
Rotation followed by Translation !!
Current Matrix
[ I ]
[ T ]
[ T ] [ R ]
[ T ] [ R ] P
[6]-29RM
Line Angle Distance Area
Translation Yes Yes Yes Yes
Rotation Yes Yes Yes Yes
Scaling Yes No No No
Reflection Yes Yes Yes Yes
Shear Yes No No Yes
Preserved Attributes
General Properties
[6]-30RM
A general invertible, linear, transformation.
x a x b y c
y a x b y c
a b a b
1 1 1
2 2 2
1 2 2 1 0( )
Affine Transformation
Transformation Matrix:
100222
111
cba
cba
[6]-31RM
• Product of affine transformations is affine.
•Affine transformations preserve linearity of segments.
•Affine transformations preserve parallelism between lines.
•Affine transformations are invertible.
Affine Transformation: Properties