74
Computer Graphics 2D Transformations

Computer Graphics 2D Transformations

  • Upload
    hesper

  • View
    87

  • Download
    2

Embed Size (px)

DESCRIPTION

Computer Graphics 2D Transformations. Contents. In today’s lecture we’ll cover the following: Why transformations Transformations Translation Scaling Rotation Homogeneous coordinates Matrix multiplications Combining transformations. Why Transformations?. - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Graphics  2D Transformations

Computer Graphics 2D Transformations

Page 2: Computer Graphics  2D Transformations

2of74

Contents

In today’s lecture we’ll cover the following:– Why transformations– Transformations

• Translation• Scaling• Rotation

– Homogeneous coordinates– Matrix multiplications– Combining transformations

Page 3: Computer Graphics  2D Transformations

3of74

Why Transformations?

In graphics, once we have an object described, transformations are used to move that object, scale it and rotate it

Imag

es t

aken

fro

m H

earn

& B

aker

, “C

ompu

ter

Gra

phic

s w

ith O

penG

L” (

2004

)

Page 4: Computer Graphics  2D Transformations

4of74

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′)

Page 5: Computer Graphics  2D Transformations

5of74

Translation

•The translation distance pair (tx , ty ) is called a translation vector or shift vector

Page 6: Computer Graphics  2D Transformations

6of74

Translation

•This allows us to write the two-dimensional translation equations in the matrix form:

•where

Page 7: Computer Graphics  2D Transformations

7of74

Example

Note: House shifts position relative to origin

y

x 0

1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

Page 8: Computer Graphics  2D Transformations

8of74

Scaling

•A scaling transformation alters the size of an object.

•This operation can be carried out for polygons by multiplying the coordinate values (x,y) of each vertex by scaling factors sx , sy to produce the transformed coordinates (x′, y′):

Page 9: Computer Graphics  2D Transformations

9of74

Scaling

•Transformation Equations (Matrix Form):

•Or

where S is the 2 by 2 scaling matrix.

Page 10: Computer Graphics  2D Transformations

10of74

Scaling

Remarks:

1. When sx and sy are assigned the same value, a uniform scaling is produced that maintains relative object proportions.

2. Unequal values for sx and sy result in a differential scaling that is often used in design applications, where pictures are constructed from a few basic shapes that can be adjusted by scaling and positioning transformations.

Page 11: Computer Graphics  2D Transformations

11of74

Example

•Turning a square (a) into a rectangle (b) with scaling factors sx = 2 and sy =1.

Page 12: Computer Graphics  2D Transformations

12of74

Scaling

•When the scaling factors sx and sy are assigned the values less than 1, the objects move closer to the coordinate origin.

•Similarly, the values of sx and sy greater than 1, move coordinate positions farther from the origin.

Page 13: Computer Graphics  2D Transformations

13of74

Example

Page 14: Computer Graphics  2D Transformations

14of74

Scaling

•When the scaling factors sx and sy are assigned the values less than 1, the objects reduce in size.

•Similarly, the values of sx and sy greater than 1, enlarge the object size (see Fig. 7).

•Warning: Negative values of sx and sy are not permissible.

Page 15: Computer Graphics  2D Transformations

15of74

Scaling

•We can control the location of a scaled object by choosing a position, called the fixed point, that is to remain unchanged after the scaling transformation.

•Coordinates for the fixed point (xf , yf )can be chosen as one of the vertices, the object centroid , or any other position see next Figure.

Page 16: Computer Graphics  2D Transformations

16of74

Example

Page 17: Computer Graphics  2D Transformations

17of74

Scaling

Transformation equations, with the fixed point ( xf , yf ), are calculated as:

We can rewrite these scaling transformations to separate the multiplicative and the additive terms as follows:

(13)

(14)

Page 18: Computer Graphics  2D Transformations

18of74

Scaling

•The additive terms xf (1 - sx) and yf (1-sy) are constant for all points in the object.

•Polygons are scaled by applying transformations (14) to each vertex and then regenerating the polygon using the transformed vertices.

•Other objects are scaled by applying transformations (14) to the parameters defining the objects. •For example, an ellipse in the standard position is resized by scaling the semi-major and semi-minor axes and redrawing the ellipse about the designated center coordinates.

Page 19: Computer Graphics  2D Transformations

19of74

Rotation

•The two-dimensional rotation is applied to an object by repositioning it along a circular path in the x-y plane.

•To generate a rotation, we specify a rotation angle θ and the position (xr,yr) of the rotation point (or pivot point) about which object is rotated as shown in the Figure.

Page 20: Computer Graphics  2D Transformations

20of74

Rotation

•This transformation can also be described as a rotation about a rotation axis that is perpendicular to the x-y plane and passes through the pivot point.

Page 21: Computer Graphics  2D Transformations

21of74

Rotation

•Rotation about the Origin:

Page 22: Computer Graphics  2D Transformations

22of74

Rotation

•In the previous Figure:• r – is the constant distance of the point from the origin.• is the original angular position of the point from th horizontal• is the rotation angle.•Using the standard trigonometric identities, we can express the transformed coordinates in terms of the two angles as

Page 23: Computer Graphics  2D Transformations

23of74

Rotation

The original coordinates of the point in polar coordinates are:

x = r cos φ,

y = r sin φ (5)

• Substituting expressions (5) into (4) , we obtain the transformation equations for rotating a point at position (x, y) through and an angle about the origin:

x' = x cos θ - y sin θ,

y' = x sin θ + y cos θ (6)

Page 24: Computer Graphics  2D Transformations

24of74

Rotation

•Rotation Equations in the Matrix Form:

P' = R . P (7)

•Where the rotation matrix is

Page 25: Computer Graphics  2D Transformations

25of74

•Rotation of a point about an arbitrary pivot position is shown bellow:

Page 26: Computer Graphics  2D Transformations

26of74

Rotation

•Using the trigonometric relationships indicated by the two right triangles in the previous Figue, we can generalize equation (6) to obtain the transformation equations for rotation of a point about any specified rotation position (xr,yr):

Page 27: Computer Graphics  2D Transformations

27of74

Rotation

•Rotation is a rigid-body transformations that move objects without deformation.

•Every point on an object is rotated through the same angle.

•A straight line segment is rotated by applying the rotation equation (9) to each of the two line endpoints and redrawing the line between the new endpoint positions.

Page 28: Computer Graphics  2D Transformations

28of74

Rotation

•A polygon is rotated by displaying each vertex using the specified rotation angle and then generating the polygon using the new vertices.

•A curve is rotated by repositioning the defining points for the curve and then redrawing it.

•An ellipse is rotated about its center coordinates simply by rotating the major and minor axes.

Page 29: Computer Graphics  2D Transformations

29of74

Example

6

y

x 0 1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

Page 30: Computer Graphics  2D Transformations

30of74

Matrix Representationsand Homogeneous Coordinates

•All transformations can be expressed in the following general Matrix Form:

where– P and P′ are column vectors for the

coordinate positions.– Matrix M1 is a 2 by 2 array containing

multiplicative factors.– Matrix M2 is a 2-element column matrix,

containing translational terms factors.

Page 31: Computer Graphics  2D Transformations

31of74

Matrix Representationsand Homogeneous Coordinates

•More Convenient Method:

Combine the multiplicative and translational terms into a single Matrix Representation, by expanding 2 by 2 Matrix Representation to 3 by 3 matrices.

Page 32: Computer Graphics  2D Transformations

32of74

Matrix Representationsand Homogeneous Coordinates

•Represent each Cartesian coordinate position (x, y) with homogeneous coordinate triple (xh , yh ,h),

•where

Page 33: Computer Graphics  2D Transformations

33of74

Matrix Representationsand Homogeneous Coordinates

•Thus, a general homogeneous coordinate representation can also be written as (h.x, h.y, h).

– h can be selected to be any nonzero value. – Thus, there is an infinite number of equivalent

homogeneous representations for each coordinate point (x, y).

– A convenient choice is h =1, so that (x, y) becomes (x, y, 1)

Page 34: Computer Graphics  2D Transformations

34of74

Matrix Representationsand Homogeneous Coordinates

•For Translation

•Equation (13) can be expressed as:

Page 35: Computer Graphics  2D Transformations

35of74

Matrix Representationsand Homogeneous Coordinates

•Or simply

• with T (tx ,ty ) as the 3 by 3 translation matrix in Eqn. (15).

• Note: The inverse of the translation matrix is obtained by replacing the translation parameters tx and ty with their negatives: -tx and - ty .

Page 36: Computer Graphics  2D Transformations

36of74

Matrix Representationsand Homogeneous Coordinates

•For Rotation

•Equation (13) can be expressed as:

Page 37: Computer Graphics  2D Transformations

37of74

Matrix Representationsand Homogeneous Coordinates

•Or simply

•with R(θ) as 3 by 3 Rotation matrix in Eqn. (17).

•Note: The inverse of the Rotation matrix is obtained by replacing the Rotation angle θ with -θ.

Page 38: Computer Graphics  2D Transformations

38of74

Matrix Representationsand Homogeneous Coordinates

•For Scaling

•Equation (13) can be expressed as:

Page 39: Computer Graphics  2D Transformations

39of74

Matrix Representationsand Homogeneous Coordinates

•Or simply

•with S (sx ,sy ) as 3 by 3 Scaling matrix in Eqn. (19).

•Note: The inverse of the scaling matrix is obtained by replacing the scaling parameters sx and sy with their multiplicative inverses 1/ sx and 1/ sy .

Page 40: Computer Graphics  2D Transformations

40of74

Why Homogeneous Coordinates?

• All of the transformations can be represented as 3*3 matrices.

• The use of matrix multiplication to calculate transformations is efficient.

Page 41: Computer Graphics  2D Transformations

41of74

Summary Homogeneous Translation

The translation of a point by (tx ,ty ) can be written in matrix form as:

Representing the point as a homogeneous column vector we perform the calculation as:

100

10

01

y

x

t

t

11*1*0*0

1**1*0

1**0*1

1100

10

01

y

x

y

x

y

x

ty

tx

yx

tyx

tyx

y

x

t

t

Page 42: Computer Graphics  2D Transformations

42of74

Remember Matrix Multiplication

Recall how matrix multiplication takes place:

ziyhxg

zfyexd

zcybxa

z

y

x

ihg

fed

cba

***

***

***

Page 43: Computer Graphics  2D Transformations

43of74

Summary Homogenous Coordinates

To make operations easier, 2-D points are written as homogenous coordinate column vectors

PttTPty

tx

y

x

t

t

yxy

x

y

x

),(':

11100

10

01

PssSPys

xs

y

x

s

s

yxy

x

y

x

),(':

11100

00

00

Translation:

Scaling:

Page 44: Computer Graphics  2D Transformations

44of74

Summary Homogenous Coordinates

PRPyx

yx

y

x

)(':

1

cossin

sincos

1100

0cossin

0sincos

Rotation:

Page 45: Computer Graphics  2D Transformations

45of74

Inverse Transformations

Transformations can easily be reversed using inverse transformations

100

10

011

y

x

t

t

T

100

01

0

001

1

y

x

s

s

S

100

0cossin

0sincos1

R

Page 46: Computer Graphics  2D Transformations

46of74

Composite Transformations

•With the matrix representations, we can set up matrix for any sequence of transformations as a composite transformation matrix by calculating the matrix product of the individual transformations.

•Forming products of transformation matrices is often referred to as a concatenation, or composition of matrices.

Page 47: Computer Graphics  2D Transformations

47of74

Composite Transformations

•Two successive Translations

•where P and P′ are represented as homogeneous coordinate column vectors.

Page 48: Computer Graphics  2D Transformations

48of74

Composite Transformations

•The composite Transformation matrix is as follows:

•Or

Page 49: Computer Graphics  2D Transformations

49of74

Composite Transformations

•Two successive Rotations

• The composite Transformation matrix is as follows:

)25()().( 2121 RRR

Page 50: Computer Graphics  2D Transformations

50of74

Composite Transformations

•so that the final rotated coordinates can be calculated as:

Page 51: Computer Graphics  2D Transformations

51of74

Composite Transformations

•Two successive Scalings

•Or

Page 52: Computer Graphics  2D Transformations

52of74

Composite Transformations

•General Pivot-Point Rotation

•Rotations about any pivot point (xr,yr) involves the following steps:

•Step 1. Translation of object so that Pivot Point (xr,yr) is at origin.

•Step 2. Rotation about origin.

•Step 3. Translation of object so that the Pivot Point is returned to position (xr,yr).

Page 53: Computer Graphics  2D Transformations

53of74

Example General Pivot-Point Rotation

Page 54: Computer Graphics  2D Transformations

54of74

Composite Transformations

•The matrix formulation is as follows:

Page 55: Computer Graphics  2D Transformations

55of74

Composite Transformations

•Or

•Where

Page 56: Computer Graphics  2D Transformations

56of74

Composite Transformations

•General Fixed-Point Scaling

•Scaling with respect to any selected fixed point (xf,yf) involves the following steps:

•Step 1. Translate Object so that Fixed Point (xf,yf) is at Origin.

•Step 2. Scale Object with respect to Origin

•Step 3. Translate Object so that Fixed Point is returned to position (xf,yf)

Page 57: Computer Graphics  2D Transformations

57of74

Example General Fixed-Point Scaling

Page 58: Computer Graphics  2D Transformations

58of74

Composite Transformations

•The matrix formulation is as follows:

•Where

Page 59: Computer Graphics  2D Transformations

59of74

Composite Transformations

•General Scaling Directions

•The parameters sx and sy scale objects along x and y directions.

•We can scale an object in other directions by rotating the object to align the desired scaling directions with the coordinate axes

before applying the scaling transformations.

Page 60: Computer Graphics  2D Transformations

60of74

Example

•Suppose we want to apply scaling factors with values specified by parameters s1and s2 in the directions shown in the Figure.

Page 61: Computer Graphics  2D Transformations

61of74

Composite Transformations

•To accomplish the scaling without changing the orientation of the object, we do the followings:

•Step 1. Perform a rotation so that the directions for s1 and s2 coincide with the x and y axes, respectively.

•Step 2. Apply scaling transformation.

•Step 3. Apply opposite rotation to return points to their original orientations.

Page 62: Computer Graphics  2D Transformations

62of74

Composite Transformations

•The matrix formulation is as follows:

S

Page 63: Computer Graphics  2D Transformations

63of74

Example

• As an example, we turn a unit squre into a parallelogram by streching it along the diagonal from (0, 0) to (1, 1).

• We do the following1. Rotate the diagonal onto y axis using θ=45o

2. Double its length with the scaling values (s1,s2)=(1,2).

3. Rotate again to return the digonal to its original orientation.

Page 64: Computer Graphics  2D Transformations

64of74

Example

Page 65: Computer Graphics  2D Transformations

65of74

Composite Transformations

•Concatenation Properties

•For any three matrices, A, B, CA.B.C = (A.B ).C = A.(B.C)

•In general, for two matrices A and B

•A.B ≠ B.A

Page 66: Computer Graphics  2D Transformations

66of74

Example Composite Transformations

•Assume that we want to translate and rotate an object, we must be careful about the order in which the composite matrix is evaluated.

•Consider the next Figure.– (a) an object is first translated in the x

direction, then rotated 450 counterclockwise.– (b) the object is first rotated 450

counterclockwise, then translated in the x direction.

Page 67: Computer Graphics  2D Transformations

67of74

Example Composite Transformations

Page 68: Computer Graphics  2D Transformations

68of74

Composite Transformations

•For some special cases – such as a sequence of transformations of the same kind – the multiplication of transformation matrices is commutative.

• Examples of transformation that could be done in any order:

– Two successive rotations– Two successive translations– Two successive scaling

– Rotation and uniform scaling (sx = sy )

Page 69: Computer Graphics  2D Transformations

69of74

Composite Transformations

•General Composite Transformations and Computational Efficiency

•General representation is as follows:

Page 70: Computer Graphics  2D Transformations

70of74

Composite Transformations

•where the four elements rsij are the multiplicative rotation-scaling terms that involve only rotation angles and scaling factors.

Elements trsx and trsy are the translational terms containing combinations of translation distances, pivot-point and fixed-point coordinates, rotation angles and scaling parameters.

Page 71: Computer Graphics  2D Transformations

71of74

Example Composite Transformations

Page 72: Computer Graphics  2D Transformations

72of74

Composite Transformations

•Although matrix equation (31) requires nine multiplications and six additions, the explicit calculations for the transformed coordinates are:

•Thus, we actually only need to perform four multiplications and four additions to transform coordinate positions.

Page 73: Computer Graphics  2D Transformations

73of74

Summary

In this lecture we have taken a look at:– 2D Transformations

• Translation• Scaling• Rotation

– Homogeneous coordinates– Matrix multiplications– Composite transformations

Page 74: Computer Graphics  2D Transformations

74of74

Summary Equations

Translation:

xnew = xold + dx ynew = yold + dy

Scaling:

xnew = Sx × xold ynew = Sy × yold

Rotation

xnew = xold × cosθ – yold × sinθ

ynew = xold × sinθ + yold × cosθ