Transcript Document

Unit 2
2D Transformation of
Graphics
• What is geometric transformation?
• Operations that are applied to the geometric
description of an object to change its position,
orientation, or size are called geometric
transformations.
•
1.
2.
3.
4.
So the geometric-transformation functions that are
available in some system are following:
Translation
Rotation
Scaling
Other useful transforms includes: reflection and
shear.
• Translation
• Rotation
• Scaling
• Uniform Scaling
• Un-uniform Scaling
• Reflection
• Shear
•
Why do we need geometric transformations in
CG( motivation )?
•
•
•
As a viewing aid
As a modeling tool
As an image manipulation tool
• Two-Dimensional(2D) Translation
We perform a translation on a single coordinate
point by adding offsets to its coordinates so as to
generate a new coordinate position.
Similarly, a translation is applied to an object that is
defined with multiple coordinate positions by relocating
all the coordinate positions by the same displacement
along parallel paths.
Suppose tx and ty is the translation distances, (x, y)
is the original coordinates, ( x' , y ') is the new
coordinate position.
x   x  tx
y  y  ty
• Express the translation use matrix equation as
following:
 x   x  tx 
 y   y   ty 
     
• Translation is a rigid-body transformation that moves
objects without deformation.
• Ex: Translate a polygon with coordinates
A(2, 5), B(7, 10), C(10, 2) by 3 units x direction and 4
units in y direction
(Hint : Add 3 to all x values and 4 to all values).
•
•
•
•
•
Write a C++ program to translate :
A point
A line
A Rectangle
A Polygon (Triangle)
/* C++ program */
#include<iostream.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int x,y,tx,tx;
/* initialise graphics
------------------------ */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,“c:\\tc\\bgi");
cout<<“Enter x, y:”
cin>>x>>y;
cout<<“Enter tx,ty”
cin>>tx>>ty;
putpixel(x,y,15);
delay(100);
putpixel(x+tx,y+ty,15);
getch();
closegraph();}
Translate a line:
line(x1,y1,x2,y2);
line(x1+tx,y1+ty,x2+tx,y2+ty);
Translate a rectangle:
rectangle(x1,y1,x2,y2);
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
Two-Dimensional Rotation
• We generate a rotation transformation of an object by
specifying a rotation axis and a rotation angle. All
points of the object are then transformed to new
positions by rotating the points through the specified
angle about the rotation axis.
• A 2D rotation of an object is obtained by repositioning
the object along a circular path in the xy plane, the
rotation axis is perpendicular to the xy plane.
• Parameters for 2D rotation is rotation angle
and a
rotation point( pivot point)
.

• Rotation angle define a positive values for
( xr point.
, yr )
counterclockwise rotation about the pivot

• r is the constant distance of the
point form the origin, angle is
 angular position of
the original
the point from the horizontal,
and
is the rotation angle.

 x  r cos 

 y  r sin 
(1)
 x'  r cos(   )  r cos  cos  - r sin  sin 

 y '  r sin(    )  r cos  sin   r sin  cos 
• Substitute expression (1) into (2)
(2)
 x'  x cos   y sin 

 y '  x sin   y cos 
(3)
Matrix form
P’=P.R
 cos sin  
 x y   x y 

 sin  cos 
Example 1
• A point (4, 3) is rotated by an angle of 45
degrees. Find the rotation matrix and
resultant point.
• Rotate a line from A(0,0) to B(200,100) by
45 degrees.
• Rotate a polygon with coordinates A(2, 5),
B(7, 10), C(10, 2) by an angle of 45 degrees.
•
•
•
•
•
Write a C program to Rotate :
A point
A line
A Rectangle
A Polygon (Triangle)
Two-Dimensional(2D) Scaling
• To alter the size of an object, we apply a scaling
transformation. A simple two-dimensional scaling
operation is performed by multiplying object positions
(x, y) by scaling factors sx and sy to produce the
transformed coordinate
.
( xr , yr )
 x '  x  Sx

 y '  y  Sy
• Scaling factors sx scales an object in the x coordinate.
Sx 0 
 x y     x y   0 S 
y

Properties of the scaling transformation.
• Any positive values can be assigned to the
scaling factors sx and sy. Values less then 1
reduce the size of object, the object will close
up the original at the same time. In contrast ,
enlarge the size of object.
• When sx and sy are assigned to the same
values, a uniform scaling is produced which
maintains relative object proportions.
• Unequal values for sx and sy result in a
differential scaling that is often used in
design applications.
• Negative values can also be specified for the
scaling parameters, this not only resizes an
object, it reflects the object one or more of
the coordinate axes.
•
•
•
•
•
Write a C++ program to scale :
A point
A line
A Rectangle
A Polygon (Triangle)
Point:
cout<<“Enter x, y:”
cin>>x>>y;
cout<<“Enter sx,sy”
cin>>sx>>sy;
Initgraph(&gd,&gm,”..\\bgi”);
putpixel(x,y,15);
delay(100);
putpixel(x*sx,y*sy,15);
getch();
closegraph();}
Scale a line:
line(x1,y1,x2,y2);
line(x1*sx,y1*sy,x2*sx,y2*sy);
Scale a rectangle:
rectangle(x1,y1,x2,y2);
rectangle(x1*sx,y1*sy,x2*sx,y2*sy);
•
Q. Scale the polygon with coordinates A(2,5)
B(7,10) and C(10,2) by two units in xdirection and two units in y-direction and plot
the graph.
Q. Scale the polygon with coordinates A(2,5)
B(7,10) and C(10,2) by -2 units in x-direction
and 3 units in y-direction and plot the graph.
Matrix Representations and Homogeneous Coordinates
• It is well known that many application involves
sequences of geometric transformations. For example,
an animation might require an object to be translated,
rotated and scaled at each increment of the motion. If
you want to first rotates an object, then scales it, you
can combine those two transformation to a composite
transformation like the following equation:
•
 S x 0  cos   sin    S x cos  S x sin  
A  S *R  





0
S
S
sin

S
cos

cos    y
y   sin 
y


• However, it will be difficult to deal with the
above composite transformation and translation
together. Because, translation is not 2 by 2
matrix representation.
• So ,here we consider how the matrix
representations discussed in the previous
sections can be reformulated so that such
transformation sequences can be efficiently
processed.
•
In fact, we can expressed three basic twodimensional transformation( translation, rotation, and
scaling) in the general matrix form:
P '  M1  P  M 2
•
Now this equation can be reformulated to eliminate
the matrix addition operation using Homogeneous
Coordinates.
• What is Homogeneous Coordinates?
•
A standard technique for expanding each twodimensional coordinate position representation (x, y)
to three-element representation ( xh , yh , h) , called
Homogeneous Coordinates, where homogeneous h is
a nonzero value such that
•
•
•
xh
x
,
h
yh
y
h
For geometric transformation, we can chose the
homogeneous parameter h to be any nonzero value.
Thus there are an infinite number of equivalent
homogeneous representations for each coordinate
point (x, y) .
• A convenient choice is simply to set h=1, so
each two-dimensional position is then
represented with homogeneous coordinate
(x,y,1).
•
Expressing positions in homogenous
coordinates allows us to represent all geometric
transformation equations as matrix
multiplications, which is the standard method
used in graphics systems. Two dimensional
coordinate positions are represented with threeelements column vectors, and two-dimensional
transformation operations are expressed as
3X3 matrices.
• Homogeneous coordinate representation of 2D
Translation
 x 
1
 y    0
 



1 

0
0
1
0
tx   x 
 y
ty 
 
1

1 

• This translation operation can be written in the
abbreviated form
P  T (tx , t y )  P
'
• Homogeneous coordinate representation of 2D
Rotation
 x  cos
 y   sin 
  
1  0
 sin 
cos
0
0  x 
0  y 
1  1 
This translation operation can be written in the
abbreviated form
P  R( )  P
'
• Homogeneous coordinate representation of 2D
Scaling
 x   sx 0
 y   0 sy
  
1  0 0
0  x 
0  y 
1  1 
This scaling operation can be written in the abbreviated
form
P  S (sx, sy)  P
'
Two-Dimensional Composite Transformation
•
Using matrix representations, we can set up a
sequence of transformation as a composite
transformation matrix, by calculating the product of the
individual transformations.
•
And, since many positions in a scene are typically
transformed by the same sequence, its is more
efficient to first multiply the transformation matrices to
form a single composite matrix.
P '  M 2  M1  P
 M P
•
So the coordinate position is transformed using the
composite matrix M, rather than applying the individual
transformations M1 and then M2
General Two-Dimensional Pivot-Point Rotation
(Rotation about an arbitrary point)
(xr,y
r)
•
(xr,y
r)
(xr,y
r)
(xr,y
r)
So we can generate a 2D rotation about any
other pivot point (x, y) by performing the
following sequence of translate-rotate-translate
operations.
• (1) Translate the object so that the pivot-point position
is moved to the coordinate origin.
• (2) Rotate the object about the coordinate origin.
• (3) Translate the object so that the pivot point is
returned to its original position.
• The composite transformation matrix for this
sequence is obtained with the concatenation:
1 0 xr  cos   sin  0  1 0  xr  cos   sin  xr (1  cos  )  yr sin  
0 1 y  sin  cos  0  0 1  y   sin  cos  y (1  cos  )  x sin  
r
r 
r
r



0 0 1  0

0
1  0 0 1  0
0
1
Problems:
• Perform the rotation of a point A(2,3)
about point (1,1) by angle of 45 degrees.
• Perform the rotation of a point A(2,3),
B(5,5), C(4,3) about point (1,1) by angle of
45 degrees.
• Other 2D transformations:
• Reflection with respect to the axis
•
•
• X axis
 1 0 0
 0 1 0


 0 0 1 
1 0 0 
0  1 0 


0 0 1 
y
1
1
2
3
2’
3’
1’
y axis
2
x
y
3 3’
1’
2’
x
• Reflection with respect to a line y=x
y
0 1 0 
1 0 0 


0 0 1 
x
• Shear
• Two common shearing transformations are those
that shift coordinate x values and those that shift y
values.
• An x-direction shear relative to the x axis is
produced with the transformation matrix
x’ = x + shx · y,
y’ = y
y
1 sh x
0 1

0 0
0
0
1
(0,1)
(1,1)
(0,0)
(1,0)
y
(2,1)
x
(0,0) (1,0)
(3,1)
x
• Y-Shear
x’ = x ,
y’ = y + shy*x
 1 0 0
 Shy 1 0


 0 0 1
Thank You!!
General 3D Rotations
(1) Rotating about an axis that is parallel to one
of the coordinates axes.
• Step1, Translate the object so that the rotation
axis coincides with the parallel coordinate axis.
• Step2, Perform the specified rotation about that
axis.
• Step3, Translate the object so that rotation axis
is moved back to its original.
• A coordinate position P is transformed with the
sequence
P  T  Rx ( )  T  P
'
1
• (2) Rotated about an axis that is not parallel to
one of the coordinate axes.
• In this case, we also need rotation to align the
rotation axis with a selected coordinate axis and
then to bring the rotation axis back to its original
orientation.
•
A rotation axis can be defined with two
coordinate position, or one position and
direction angles (direction cosines ).
• Now we assume that the rotation axis is
defined by two points, and that the direction of
rotation is to be counter clockwise when looking
along the axis from p2 to p1.
• The components of the rotation
axis vector are then computed as:
V  P2  P1
 ( x2  x1 , y2  y1 , z2  z1 )
• And the unit rotation-axis vector u
is
V
u
V
 (a, b, c)
• Where
•
x2  x1
y2  y1
z2  z1
a
,b 
,c 
V
V
V
• Step1. Translate the object so that the rotation axis
passes through the coordinate origin
• Step2, Rotate the object so that the axis of rotation
coincides with one of the coordinate axes.
• Step3, Perform the specified rotation about the
selected coordinate axis
• Step4, Apply inverse rotations to bring the rotation
axis back to its original orientation.
• Step5 ,Apply the inverse translation to bring the
rotation axis to its original spatial position.
• we can transform the rotation axis onto any one of
the three coordinate axes. But the z axis is often a
convenient choice.
•
• The first step in the rotation sequence is to set
up the translation matrix that repositions the
rotation axis so that it passes through the
coordinate origin. We move p1 to the origin.
• The next step we will put the rotation axis onto
the z axis. We can use the coordinate-axis
rotations to accomplish this alignment in two
steps. For this example, we first rotate about
the x axis, then rotate about y axis. The x axis
rotation gets vector u into the x-z plane, and the
y-axis rotation swings u around to the z axis.
y
u
x
z
• Since rotation calculations involve sine and
cosine functions, so we can use standard
vector operation to obtain elements of the two
rotation matrices. A vector dot product can be
used to determine the cosine term, and a vector
cross product can be used to calculate the sine
term.
• Firstly, we establish the transformation matrix
for rotation around the x axis by determining the
values for the sine and cosine of the rotation
angle necessary to get u into the x-z plane.
Inverse Transformation
• To undo the applied transformation
Inverse Transformation is applied.
• Inverse of T is T-1.
• T T-1=I (Identity Matrix)
Thank you !!!
• Not required
• Question 1:
• If the rotation point is an arbitrary pivot position ( xr , yr ),
can you give the correct rotation expression?
 x '  xr  ( x  xr )cos   ( y  yr )sin 

 y '  yr  ( x  xr )sin   ( y  yr )cos 
• How to rotate a line segment or a polygon?
(3)
• Similarly, we can obtain the matrix
representation of General 2D Fixed-Point
Scaling.( question ).
• (1) Translate the object to so that the fixed point
coincides with the coordinate origin.
• (2) Scale the object with respect to the
coordinate origin.
• (3) Use the inverse of the translation in step (1)
to return the object to its original position.
•
(xr,y
r)
(xr,y
r)
Scale
Translate
(xr,y
r)
(xr,y
r)
Translate
T x f , y f  S sx , sy  T  x f , y f   S x f , y f , sx , sy 
1 0
0 1

0 0
x f  s x
y f    0
1   0
0
sy
0
0  1 0
0  0 1
1 0 0
 x f  s x
 y f    0
1   0
0
sy
0
x f (1  s x ) 
y f (1  s y )

1
• We also obtain General 2D Scaling Directions.
• Suppose we want to apply scaling factors with
values specified by parameters s1 and s2 in the
directions shown in the following fig.

• To accomplish the scaling without changing the
orientation of the object.
(1) we perform a rotation so that the directions
for s1 and s2 coincide with the x and y axes,
respectively.
(2) Then the scaling transformation S(s1,s2) is
applied.
(3) An opposite rotation to return points to their
original orientations.
 s1 cos2   s2 sin 2 

1
R ( )  S ( s1 , s2 )  R( )   ( s2  s1 ) cos sin 

0

0

2
2
s1 sin   s2 cos  0
0
1
( s2  s1 ) cos sin 
•
For example, we turn a unit square into a
parallelogram by stretching it along the diagonal
from (0,0) to (1,1).
y
y
(1/2,3/2)
(0,1)
(2,2)
(1,1)
(3/2,1/2)
(0,0)
(1,0)
x
(0,0)
Scale
x
•
We first rotate the diagonal onto the y axis
using angle 45 degree. Then we double its
length with the scaling values s1=1 and s2=2,
and then we rotate again to return the diagonal
to its original orientation.
• Matrix Concatenation Properties
• (1) Multiplication of matrices is associative.
• (2) Transformation products, on the other hand,
may not be commutative.
• Other 2D transformations:
• Reflection with respect to the axis
•
•
• X axis
 1 0 0
 0 1 0


 0 0 1 
1 0 0 
0  1 0 


0 0 1 
y
1
1
2
3
2’
3’
1’
•
y axis
2
x
y
3 3’
original point
 1 0 0
 0  1 0


 0
0 1
y
1’
2
x
1’
3’
3
1
2
2
x
• Reflection with respect to a line
y
0 1 0 
1 0 0 


0 0 1 
x
• Shear
• Two common shearing transformations are those
that shift coordinate x values and those that shift y
values.
• An x-direction shear relative to the x axis is
produced with the transformation matrix
x’ = x + shx · y,
1 sh x
0 1

0 0
y’ = y
0
0
1
y
(0,1)
(1,1)
(0,0)
(1,0)
y
(2,1)
x
(0,0) (1,0)
(3,1)
x
• Any real number can be assigned to the shear
parameter shx. A coordinate position (x, y) is
then shifted horizontally by an amount
proportional to its perpendicular distance ( y(3,1)
value ) from the x axis.
• We can generate x-direction shears relative to
other reference lines with yref=-1.
• Now, coordinate positions are transformed as
x’ = x + shx · (y-yref),
•
y’ = y
1
0

0
sh x
1
0
 sh x  y ref 

0


1
• The following Fig. illustrates the conversion of a
square into a parallelogram with shy=0.5 and yref=-1.
y
(0,1)
y
(1,1)
(1,1)
(2,1)
(1/2,0)
(0,0)
(1,0)
x
(3/2,0)x
(0,-1)
• Shearing operations can be expressed as a
sequences of basic transformations. Such as a
series of rotation and scaling matrices
General 3D Rotations
(1) Rotating about an axis that is parallel to one
of the coordinates axes.
• Step1, Translate the object so that the rotation
axis coincides with the parallel coordinate axis.
• Step2, Perform the specified rotation about that
axis.
• Step3, Translate the object so that rotation axis
is moved back to its original.
• A coordinate position P is transformed with the
sequence
P  T  Rx ( )  T  P
'
1
• (2) Rotated about an axis that is not parallel to
one of the coordinate axes.
• In this case, we also need rotation to align the
rotation axis with a selected coordinate axis and
then to bring the rotation axis back to its original
orientation.
•
A rotation axis can be defined with two
coordinate position, or one position and
direction angles (direction cosines ).
• Now we assume that the rotation axis is
defined by two points, and that the direction of
rotation is to be counter clockwise when looking
along the axis from p2 to p1.
• The components of the rotation
axis vector are then computed as:
V  P2  P1
 ( x2  x1 , y2  y1 , z2  z1 )
• And the unit rotation-axis vector u
is
V
u
V
 (a, b, c)
• Where
•
x2  x1
y2  y1
z2  z1
a
,b 
,c 
V
V
V
• Step1. Translate the object so that the rotation axis
passes through the coordinate origin
• Step2, Rotate the object so that the axis of rotation
coincides with one of the coordinate axes.
• Step3, Perform the specified rotation about the
selected coordinate axis
• Step4, Apply inverse rotations to bring the rotation
axis back to its original orientation.
• Step5 ,Apply the inverse translation to bring the
rotation axis to its original spatial position.
• we can transform the rotation axis onto any one of
the three coordinate axes. But the z axis is often a
convenient choice.
•
• The first step in the rotation sequence is to set
up the translation matrix that repositions the
rotation axis so that it passes through the
coordinate origin. We move p1 to the origin.
• The next step we will put the rotation axis onto
the z axis. We can use the coordinate-axis
rotations to accomplish this alignment in two
steps. For this example, we first rotate about
the x axis, then rotate about y axis. The x axis
rotation gets vector u into the x-z plane, and the
y-axis rotation swings u around to the z axis.
y
u
x
z
• Since rotation calculations involve sine and
cosine functions, so we can use standard
vector operation to obtain elements of the two
rotation matrices. A vector dot product can be
used to determine the cosine term, and a vector
cross product can be used to calculate the sine
term.
• Firstly, we establish the transformation matrix
for rotation around the x axis by determining the
values for the sine and cosine of the rotation
angle necessary to get u into the x-z plane.
• We need the angle a between
vector u and x-z plane .
• We directly calculate cosine and
sine values of a .
• If we represent the projection of
u in the zoy plane as the vector
u '  (0, b, c)
a
• Then the cosine of the rotation
angle a can be determined from
the dot product of u ' and then z
unit vector uz along the z axis.
u '  uz c
cos a  '

u uz d
y
(d  b 2  c 2 )
b
x
Similarly, we can determine the sine of a from the
cross product of
u '  u z  u x u ' u z sin a
u'  uz  ux  b
So
u x u ' u z sin a  u x  b
b
sin a 
d
• So the matrix elements for rotation of this vector
about the x axis and into the xz plane:
1
0
Rx  
0

0
0
0
cos a
 sin a
sin a
0
cos a
0
1
0 
0
0  

0
 0
1 
 0
0
c
d
b
d
0
0
b

d
c
d
0
0

0


0

1 
• The rotation matrix about y axis is
cos b
 0
Ry  
 sin b

 0
0  sin b
1
0
0 cos b
0
0
0  d
0   0


0  a
 
1  0
0
1
0
0
a
0
d
0
0
0 
0

1
• The third step we have aligned the rotation axis
with the positive z axis. The specified rotation
angle  we can now be applied as a rotation
about the z axis
y
cos 
 sin 
Rz  
 0

 0
 sin 
cos 
0
0
0
0
1
0
0
0 
0

1
x
z
• So the transformation matrix for rotation about an
arbitrary axis can then be expressed as the
composition of these seven individual transformations:
•
1
1
R( )  T  Rx (a )  Ry (b )  Rz ( )  Ry (b )  Rx (a )  T
1