1 of 33

Computer Graphics

Dr.S.Sivakumar,Principal

C.P.A College, Bodinayakanur

*

1

2 of 33

Transformations

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

2

3 of 33

Translation

Simply moves an object from one position to another

xnew = xold + dx ynew = yold + dy

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

3

4 of 33

Rotation

Rotates all coordinates by a specified angle

  • xnew = xold × cosθyold × sinθ
  • ynew = xold × sinθ + yold × cosθ

Points are always rotated about the origin

y

x

0

1

1

2

2

3

4

5

6

7

8

9

10

3

4

5

6

4

5 of 33

2-D Rotation

θ

(x, y)

(x’, y’)

x’ = x cos(θ) - y sin(θ)

y’ = x sin(θ) + y cos(θ)

5

6 of 33

2-D Rotation

x = r cos (φ)

y = r sin (φ)

x’ = r cos (φ + θ)

y’ = r sin (φ + θ)

Trig Identity…

x’ = r cos(φ) cos(θ) – r sin(φ) sin(θ)

y’ = r sin(φ) sin(θ) + r cos(φ) cos(θ)

Substitute…

x’ = x cos(θ) - y sin(θ)

y’ = x sin(θ) + y cos(θ)

θ

(x, y)

(x’, y’)

φ

6

7 of 33

2-D Rotation

This is easy to capture in matrix form:

Even though sin(θ) and cos(θ) are nonlinear functions of θ,

    • x’ is a linear combination of x and y
    • y’ is a linear combination of x and y

7

8 of 33

Scaling

Scalar multiplies all coordinates

WATCH OUT: Objects grow and move!

xnew = Sx × xold ynew = Sy × yold

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

8

9 of 33

Scaling

Scaling a coordinate means multiplying each of its components by a scalar

Uniform scaling means this scalar is the same for all components:

× 2

9

10 of 33

Scaling

Non-uniform scaling: different scalars per component:

How can we represent this in matrix form?

X × 2,�Y × 0.5

10

11 of 33

Scaling

Scaling operation:

Or, in matrix form:

scaling matrix

11

12 of 33

Matrix Representation

Represent 2D transformation by a matrix��

Multiply matrix by column vector� ⇔ apply transformation to point

12

13 of 33

Matrix Representation

Transformations combined by multiplication

Matrices are a convenient and efficient way

to represent a sequence of transformations!

13

14 of 33

2x2 Matrices

Transformations can be represented with a 2x2 matrix?

2D Identity?

2D Scale around (0,0)?

2D Rotate around (0,0)?

2D Shear?

14

15 of 33

2x2 Matrices

Transformations can be represented with a 2x2 matrix?

2D Mirror about Y axis?

2D Mirror over (0,0)?

2D Translation?

NO!

Only linear 2D transformations

can be represented with a 2x2 matrix

15

16 of 33

Basic 2D Transformations

  • Translation:
    • x’ = x + tx
    • y’ = y + ty
  • Scale:
    • x’ = x * sx
    • y’ = y * sy
  • Shear:
    • x’ = x + hx*y
    • y’ = y + hy*x
  • Rotation:
    • x’ = x*cosθ - y*sinθ
    • y’ = x*sinθ + y*cosθ

16

17 of 33

Homogeneous Coordinates

Homogeneous coordinates

    • represent coordinates in 2 dimensions with a 3-vector

Homogeneous coordinates seem unintuitive, but they make graphics operations much easier

17

18 of 33

Homogeneous Coordinates?

Mathematicians commonly use homogeneous coordinates as they allow scaling factors to be removed from equations

The transformations we discussed previously can be represented as 3*3 matrices

Using homogeneous coordinates allows us use matrix multiplication to calculate transformations – extremely efficient!

18

19 of 33

Homogeneous Coordinates

  • A point (x, y) can be re-written in homogeneous coordinates as (xh, yh, h)

  • The homogeneous parameter h is a non-�zero value such that:

  • We can then write any point (x, y) as (hx, hy, h)

  • We can conveniently choose h = 1 so that �(x, y) becomes (x, y, 1)

19

20 of 33

Homogeneous Coordinates

  • Add a 3rd coordinate to every 2D point
    • (x, y, w) represents a point at location (x/w, y/w)
    • (x, y, 0) represents a point at infinity
    • (0, 0, 0) is not allowed

Convenient coordinate system to represent many useful transformations

1

2

1

2

(2,1,1)

or (4,2,2)

or (6,3,3)

x

y

20

21 of 33

Homogeneous Coordinates

Represent translation as a 3x3 matrix?

Using the rightmost column:

21

22 of 33

Translation

Example of Translation

  • α

tx = 2�ty = 1

Homogeneous Coordinates

22

23 of 33

Basic 2D Transformations

Basic 2D transformations as 3x3 homogeneous matrices

Translate

Rotate

Shear

Scale

23

24 of 33

Remember Matrix Multiplication

Recall how matrix multiplication takes place:

24

25 of 33

Homogeneous Translation

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

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

25

26 of 33

Homogenous Transformations

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

Translation:

Scaling:

Rotation:

26

27 of 33

Inverse Transformations

Transformations can easily be reversed using inverse transformations

27

28 of 33

Combining Transformations

A number of transformations can be combined into one matrix to make things easy

    • Allowed by the fact that we use homogenous coordinates

Rotating a polygon around a point other than the origin

    • Transform to centre point to origin
    • Rotate around origin
    • Transform back to centre point

28

29 of 33

Combining Transformations (cont.)

1

2

3

4

29

30 of 33

Combining Transformations (cont.)

The three transformation matrices are combined as follows

Remember: Matrix multiplication is not commutative

so order matters

30

31 of 33

Matrix Composition

Transformations can be combined by �matrix multiplication

p’ = T(tx,ty) R(Θ) S(sx,sy) p

31

32 of 33

Matrix Composition

  • Matrices are a convenient and efficient way to represent a sequence of transformations
    • General purpose representation
    • Hardware matrix multiply

p’ = (T * (R * (S*p) ) )

p’ = (T*R*S) * p

32

33 of 33

Matrix Composition

  • After correctly ordering the matrices
  • Multiply matrices together
  • What results is one matrix – store it (on stack)!
  • Multiply this matrix by the vector of each vertex
  • All vertices easily transformed with one matrix multiply

33