Computer Graphics

advertisement
Computer Graphics
Chapter 5
Geometric Transformations
Andreas Savva
2D Translation
 Repositioning an object along a straight line path from
one co-ordinate location to another
(x,y)
(x’,y’)
To translate a 2D position, we add translation distances tx
and ty to the original coordinates (x,y) to obtain the
new coordinate position (x’,y’)
x’= x + tx ,
y’= y + ty
Matrix form
 x   x   t x 
      t 
 y   y  y 
P  P  T
y
T
x
2
2D Translation
 Moving a polygon from position (a) to position (b) with
the translation vector (-5, 10), i.e.
 5 
T  
 10 
y
y
20
20
15
15
10
10
5
5
5
10
15
(a)
20
x
5
10
15
20
x
(b)
3
Translating a Polygon
class Point2D {
public:
GLfloat x, y;
};
void translatePoly(Point2D P[], GLint n,
GLfloat tx, GLfloat ty)
{
GLint i;
for (i=0; i<n; i++) {
P[i].x = P[i].x + tx;
P[i].y = P[i].y + ty;
}
glBegin (GL_POLYGON);
for (i=0; i<n; i++)
glVertex2f(P[i].x, P[i].y);
glEnd();
}
4
2D Rotation
 Repositioning an object along a circular path in the xyplane
y
(x’,y’)
r
θ
r
φ
(x,y)
x
x  r cos(   )  r cos  cos   r sin  sin 
y  r sin(   )  r cos  sin   r sin  cos 
The original coordinates are:
x  r cos( )
y  r sin( )
5
2D Rotation
 Substituting
y
x  x cos   y sin 
y  x sin   y cos 
(x’,y’)
r
θ
r
φ
(x,y)
x
Matrix form
 x   cos 
   
 y   sin 
 sin   x 
 
cos   y 
P  R  P
6
2D Rotation about a Pivot position
 Rotating about pivot position (xr, yr)
y
r
θ
yr
xr
(x’,y’)
φ
r
(x,y)
x
x  xr  ( x  xr ) cos   ( y  yr )sin 
y  yr  ( x  xr )sin   ( y  yr ) cos 
7
Translating a Polygon
class Point2D {
public:
GLfloat x, y;
};
void rotatePoly(Point2D P[], GLint n,
Point2D pivot, GLdouble theta)
{
Point2D *V;
V = new Point2D[n];
GLint i;
for (i=0; i<n; i++) {
V[i].x = pivot.x + (P[i].x – pivot.x) * cos(theta)
- (P[i].y – pivot.y) * sin(theta);
V[i].y = pivot.y + (P[i].x – pivot.x) * sin(theta)
- (P[i].y – pivot.y) * cos(theta);
}
glBegin (GL_POLYGON);
for (i=0; i<n; i++
glVertex2f(V[i].x, V[i].y);
glEnd();
delete[] V;
}
8
2D Scaling
 Altering the size of an object. Sx and Sy are the scaling
factors. If Sx = Sy then uniform scaling.
x  xS x
y   yS y
y
Matrix form
 x   S x
    0
y  
P  S  P
0  x 
 
Sy  y 
Sx = Sy = ½
x
x’ x
Sx = Sy = ½
Reduced in size and moved
closer to the origin
9
2D Scaling relative to Fixed point
 Scaling relative to fixed point (xf, yf)
x  x f  ( x  x f ) S x
y  y f  ( y  y f ) S y
OR
x  xS x  x f (1  S x )
y
(xf , yf)
P1
’
P1
P2’
P2
P3’
P3
Sx = ¼ , Sy = ½ x
y  yS y  y f (1  S y )
where the additive terms xf(1-Sx) and yf(1-Sy) are
constants for all points in the object.
10
Translating a Polygon
class Point2D {
public:
GLfloat x, y;
};
void scalePoly(Point2D P[], GLint n, Point2D fixedPt,
GLfloat Sx, GLfloat Sy)
{
Point2D *V;
V = new Point2D[n];
GLfloat addx = fixedPt.x * (1 – Sx);
GLfloat addy = fixedPt.y * (1 – Sy);
GLint i;
for (i=0; i<n; i++) {
V[i].x = P[i].x * Sx + addx;
V[i].y = P[i].y * Sy + addy;
}
glBegin (GL_POLYGON);
for (i=0; i<n; i++
glVertex2f(V[i].x, V[i].y);
glEnd();
delete[] V;
}
11
Matrix Representation
 Use 3×3 matrices to combine transformations
 Translation
 x   1 0 t x   x 
  
 
y

0
1
t
y  y 
  
 1  0 0 1  1 
  
 
 Rotation
 x   cos 
  
 y    sin 
1  0
  
 Scaling
 x   S x
  
y  0
1  0
  
 sin 
cos 
0
0
Sy
0
0 x 
 
0 y 
1   1 
0 x 
 
0 y 
1   1 
12
Inverse Transformations
 Translation
 1 0 t x 


T 1   0 1 t y 
0 0 1 


 Rotation
 cos 

R 1    sin 
 0

0

cos  0 
0
1 
 Scaling
 S1x

1
S  0

0
0

0

1
0
1
Sy
0
sin 
13
Example
 Consider the line with endpoints (10, 10) and (30, 25).
Translate it by tx = -20, ty = -10 and then rotate it by θ = 90º.
y
(30, 25)
(10, 10)
x
Right-to-left
 x   cos 90  sin 90 0   1 0 20   x 
  

 
y

sin
90

cos
90

0
0
1

10
  

 y 
1  0
0
1   0 0 1   1 
  
14
Solution
 x   cos 90  sin 90 0  1 0 20  x 
  

 
y

sin
90

cos
90

0
0
1

10
  

 y 
1  0
 
0
1 
  
 0 0 1  1 
 0 1 0  1 0 20  x 


 
  1 0 0  0 1 10  y 
 0 0 1  0 0 1  1 


 
 0 1 10  x 

 
  1 0 20  y 
0 0
 1 
1

 
y
(30, 25)
(10, 10)
x
15
Solution (continue)
 x   0 1 10   x 
  
 
y

1
0

20
  
 y 
 1  0 0
 1 
1
  
 
y
(30, 25)
(-15, 10)
Point (10, 10)
(10, 10)
(0, -10)
x
 x   0 1 10  10   0 
  
  

y

1
0

20
10


10
  
  

 1  0 0
 1   1 
1
  
  

Point (30, 25)
 x   0 1 10  30   15 
  
  

y

1
0

20
25

10
  
  

 1  0 0
 

1 
  
 1   1 
16
Result
y
(30, 25)
(-15, 10)
(10, 10)
x
(0, -10)
Step-by-step
y
y
(10, 15)
(-15, 10)
(-10, 0)
x
T(-20, -10)
(0, -10)
R(90º)
x
17
Exercises
 Consider the following object:
y
25
10
10
45
x
1. Apply a rotation by 145º then scale it by Sx=2 and Sy=1.5
and then translate it by tx=20 and ty=-30.
2. Scale it by Sx=½ and Sy=2 and then rotate it by 30º.
3. Apply a rotation by 90º and then another rotation by 45º.
4. Apply a rotation by 135º.
18
Exercises
 Composite 2D Transformations
1.
Translation: Show that:
T (t1x , t1y )  T (t2 x , t2 y )  T (t1x  t2 x , t1 y  t2 y )
2.
Rotation: Show that:
R(1 )  R(2 )  R(1  2 )
3.
Scaling: Show that:
S (s1x , s1y )  S (s2 x , s2 y )  S (s1x  s2 x , s1y  s2 y )
19
General 2D Pivot-Point Rotation
y
(xr , yr)
y
x
Original position
and Pivot Point
Translate Object so that
Pivot Point is at origin
y
y
x
Rotation about origin
x
(xr , yr)
x
Translate object so that Pivot Point
is return to position (xr , yr)
20
General Pivot-point Rotation
Using Matrices
 x   1 0
  
 y   0 1
 1  0 0
  
xr  cos   sin  0  1 0  xr  x 


 
yr  sin  cos  0  0 1  yr  y 
 0 0 1  1 
1 
0
0
1


 
 T ( xr , yr )  R( )  T ( xr ,  yr )  P
21
Exercises
 Consider the following object:
y
25
10
10
45
x
1. Apply a rotation by 60° on the Pivot Point (-10, 10) and
display it.
2. Apply a rotation by 30° on the Pivot Point (45, 10) and
display it.
3. Apply a rotation by 270° on the Pivot Point (10, 0) and
then translate it by tx = -20 and ty = 5. Display the final
result.
22
General 2D Fixed-Point Scaling
y
(xf , yf)
y
x
Original position
and Fixed Point
Translate Object so that
Fixed Point is at origin
y
y
x
Scale Object with
respect to origin
x
(xf , yf)
x
Translate Object so that Fixed Point
is return to position (xf , yf)
23
General 2D Fixed-Point Scaling
Using Matrices
 x   1 0
  
 y   0 1
 1  0 0
  
xr  sx

yr  0
1 
 0
0
sy
0
0  1 0  xr  x 

 
0  0 1  yr  y 
 
1 
 0 0 1  1 
 T ( xr , yr )  S ( sx , s y )  T ( xr ,  yr )  P
24
Exercises
 Consider the following object:
y
125
50
60
220
x
1. Scale it by sx = 2 and sy = ½ relative to the fixed point
(140, 125) and display it.
2. Apply a rotation by 90° on the Pivot Point (50, 60) and
then scale it by sx = sy = 2 relative to the Fixed Point
(0, 200). Display the result.
3. Scale it sx = sy = ½ relative to the Fixed Point (50, 60)
and then rotate it by 180° on the Pivot Point (50, 60).
Display the final result.
25
Order of Transformations
y
x
Object is first translated in the x direction
and then rotated by 90º
y
x
Object is first rotated by 90º and then
translated in the x direction
26
Reflection
About the x axis
y
 x   1 0 0  x 
  
 
y

0

1
0
  
 y 
 1   0 0 1  1 
  
 
x
Reflection of an object about the x axis
About the y axis
 x   1 0 0  x 
  
 
y

0
1
0
  
 y 
 1   0 0 1  1 
  
 
y
x
Reflection of an object about the y axis
27
Reflection
Relative to the coordinate origin
y
 x    1 0 0   x 
  
 
y

0

1
0
  
 y 
 1   0 0 1 1 
  
 
x
Same as a rotation with 180º
With respect to the line y = x
 x   0 1 0   x 
  
 
y

1
0
0
  
 y 
 1  0 0 1 1 
  
 
y
y=x
x
28
2D Shear
 x-direction shear
x  x  shx  y
y  y
y
1
1
Matrix form
 x   1 shx
  
y

0
1
  
 1  0 0
  
x
Initial object
0 x 
 
0 y 
1   1 
y
1
1
2
shx = 2
3
x
29
2D Shear
 x-direction relative to other reference line
y
x  x  shx ( y  yref )
1
y  y
1
x
yref = -1
y
Matrix form
 x   1 shx
  
 y   0 1
 1  0 0
  
 shx  yref  x 
 
0
 y 
 1 
1
 
1
1
2
3
x
yref = -1
shx = ½, yref = -1
30
2D Shear
 y-direction shear
x  x
y  y  shy  x
y
1
1
Matrix form
 x   1
  
y

sh
   y
1  0
  
Initial object
0 0 x 
 
1 0 y 
0 1   1 
x
y
3
2
1
x
1
shy = 2
31
2D Shear
 y-direction relative to other reference line
y
x  x
y  y  shy ( x  xref )
1
1
xref = -1
Matrix form
 x   1
  
 y    shx
1  0
  
x
y
 x 
 
1  shy  xref  y 
 1 
0
1
 
0
0
2
1
xref = -1
1
shy = ½, xref = -1
x
32
Transformations between 2D
Coordinate Systems
y

y0
x0
x
 To translate object descriptions from xy coordinates to x’y’
coordinates, we set up a transformation that superimposes
the x’y’ axes onto the xy axes. This is done in two steps:
1. Translate so that the origin (x0, y0) of the x’y’ system is
moved to the origin (0, 0) of the xy system.
2. Rotate the x’ axis onto the x axis.
33
Transformations between 2D
Coordinate Systems
 i.e.
1)
 1 0  x0 


T ( x0 ,  y0 )   0 1  y0 
0 0 1 


2)
 cos 

R( )    sin 
 0

sin 
cos 
0
0

0
1 
Concatenating:
M xy, xy  R( )  T ( x0 ,  y0 )
34
Example
 Find the x’y’-coordinates of the xy points (10, 20) and (35,
20), as shown in the figure below:
y
(35, 20)
(10, 20)
30º
10
30
M xy , xy
x
 cos 30 sin 30 0  1 0 30 



   sin 30 cos 30 0  0 1 10 
 0

0
1 

 0 0 1 
31 
 0.866 0.5 0  1 0 30   0.866 0.5


 

  0.5 0.866 0  0 1 10    0.5 0.866 6.34 
 0
 
0
1 
0
1 

 0 0 1   0
35
y
(-12.38, 18.66)
y’
(35, 20)
(10, 20)
30º
10
(9.31, 6.16)
x’
30
x

P(10,20)
31  10   12.34 
 0.866 0.5

  

  0.5 0.866 6.34  20    18.66   (-12.38, 18.66)
 0
 

0
1 

 1   1 

P(35,20)
31  35   9.31 
 0.866 0.5

  

  0.5 0.866 6.34  20    6.16   (9.31, 6.16)
 0
 

0
1 

 1   1 
36
Exercise
 Find the x’y’-coordinates of the rectangle shown in the
figure below:
y
20
10
60º
10
x
37
3D Translation
 Repositioning an object along a straight line path from
one co-ordinate location to another
(x,y,z)
(x’,y’,z’)
To translate a 3D position, we add translation distances tx
ty and tz to the original coordinates (x,y,z) to obtain
the new coordinate position (x’,y’)
x’= x + tx , y’= y + ty , z’= z + tz
Matrix form (4 × 4)
 x   1
 y    0
 z   0
1 
  0
0
1
0
0
y
0 tx   x 
0 ty   y 
1 tz   z 
 
0 1   1 
P  T (t x , t y , t z ) P
T(tx, ty, tz)
x
z
38
3D Rotation
y
z-axis
x
– The 2D z-axis rotation equations are
z
extended to 3D.
x  x cos  y sin 
y  x sin   y cos 
z  z
Matrix form
 x   cos 
 y    sin 
 z   0
1  0
  
P  Rz ( ) P
 sin 
cos 
0
0
y
0
0
1
0
0 x 
0 y 
0 z 
1   1 
x
z
39
3D Rotation
x-axis
y
y  y cos   z sin 
z  y sin   z cos 
x  x
x
z
Matrix form
0
 x   1
 y    0 cos 
 z   0 sin 
 1  0
0
  
P  Rx ( ) P
0
 sin 
cos 
0
0 x 
0 y 
0 z 
1   1 
40
3D Rotation
y-axis
y
z   z sin   x cos 
x  z sin   x cos 
y  y
x
z
Matrix form
 x   cos 
 y    0
 z    sin 
1  0
  
P  Ry ( ) P
0 sin 
1
0
0 cos 
0
0
0 x 
0 y 
0 z 
1   1 
41
3D Scaling
x  xsx
y  ys y
z   zsz
y
x
z
Matrix form
 x   s x
  
y  0
 z   0
  
1 0
0
sy
0
0
0
0
sz
0
P  S ( s x , s y , s z ) P
0 x 
 
0 y 
0 z 
 
1 1 
42
Other 3D Transformations
 Reflection z-axis
 x   1
 y   0
 
 z   0
 1  0
  
0 0
1 0
0 1
0 0
y
0  x 
0  y 
 
0  z 

1 
1
 
x
z
 Shears z-axis
 x   1
 y   0
 
 z   0
 1  0
  
0 shx
1 shy
0 1
0 0
0 x 
0 y 
 
0 z 
1   1 
43
Download