60
Computer Graphics 2D TRANSFORMATIONS By Dr. Rajeev Srivastava CSE, IIT(BHU)

2d Transformations

Embed Size (px)

DESCRIPTION

Computer graphics 2D transformation.

Citation preview

  • Computer Graphics

    2D TRANSFORMATIONS

    By

    Dr. Rajeev Srivastava

    CSE, IIT(BHU)

  • 2D Transformations

    What is transformations?

    The geometrical changes of an object from a

    current state to modified state.

    Why the transformations is needed?

    To manipulate the initially created object and to

    display the modified object without having to

    redraw it.

  • 2 ways

    Object Transformation

    Alter the coordinates descriptions an object

    Translation, rotation, scaling etc.

    Coordinate system unchanged

    Coordinate transformation

    Produce a different coordinate system

    2D Transformations

  • Matrix Math

    Why do we use matrix?

    More convenient organization of data.

    More efficient processing

    Enable the combination of various

    concatenations

    Matrix addition and subtraction

    a

    b

    c

    d

    a c

    b d =

  • How about it?

    Matrix Multiplication

    Dot product

    Matrix Math

    a

    b

    c d

    e f

    a b

    c d

    e f

    g h . = a.e + b.g a.f + b.h

    c.e + d.g c.f + d.h

  • What about this?

    Type of matrix

    Matrix Math

    1 2 . 1 2 3 1

    = 6 6

    2

    3 .

    1 2

    3 1 = tak boleh!!

    a b a

    b

    Row-vector Column-vector

  • Is there a difference between possible

    representations?

    Matrix Math

    =

    +

    +

    df ce

    bf ae

    f

    e

    d c

    b a

    [ ] df be cf ae + + = [ ] d c

    b a f e

    [ ] df ce bf ae + + = [ ] d b

    c a f e

  • Well use the column-vector representation for a point.

    Which implies that we use pre-multiplication of the transformation it appears before the point to be transformed in the equation.

    What if we needed to switch to the other convention?

    +

    +=

    DyCx

    ByAx

    y

    x

    DC

    BA

    Matrix Math

  • Translation A translation moves all points

    in an object along the same

    straight-line path to new

    positions.

    The path is represented by a vector, called the translation or

    shift vector.

    We can write the components:

    p'x = px + tx

    p'y = py + ty

    or in matrix form:

    P' = P + T

    tx

    ty

    x

    y

    x

    y

    tx

    ty = +

    (2, 2) = 6

    =4

    ?

  • Rotation

    A rotation repositions

    all points in an object

    along a circular path in

    the plane centered at

    the pivot point.

    First, well assume the

    pivot is at the origin.

    P

    P

  • Rotation Review Trigonometry

    => cos = x/r , sin = y/r

    x = r. cos , y = r.sin

    P(x,y)

    x

    y r

    x

    y

    P(x, y)

    r

    => cos (+ ) = x/r

    x = r. cos (+ )

    x = r.coscos -r.sinsin

    x = x.cos y.sin

    =>sin (+ ) = y/r

    y = r. sin (+ )

    y = r.cossin + r.sincos

    y = x.sin + y.cos

    Identity of Trigonometry

  • Rotation We can write the components:

    p'x = px cos py sin

    p'y = px sin + py cos

    or in matrix form:

    P' = R P

    can be clockwise (-ve) or counterclockwise (+ve as our

    example).

    Rotation matrix

    P(x,y)

    x

    y r

    x

    y

    P(x, y)

    =

    cossin

    sincosR

  • Example

    Find the transformed point, P, caused by

    rotating P= (5, 1) about the origin through an

    angle of 90.

    Rotation

    +

    =

    cossin

    sincos

    cossin

    sincos

    yx

    yx

    y

    x

    +

    =

    90cos190sin5

    90sin190cos5

    +

    =

    0115

    1105

    =

    5

    1

  • Scaling Scaling changes the size of an

    object and involves two scale

    factors, Sx and Sy for the x-

    and y- coordinates

    respectively.

    Scales are about the origin.

    We can write the components:

    p'x = sx px p'y = sy py

    or in matrix form:

    P' = S P

    Scale matrix as:

    =

    y

    x

    s

    sS

    0

    0

    P

    P

  • Scaling If the scale factors are in between 0

    and 1 the points will be moved closer to the origin the object will be smaller.

    P(2, 5)

    P

    Example :

    P(2, 5), Sx = 0.5, Sy = 0.5

    Find P ?

  • Scaling If the scale factors are in between 0

    and 1 the points will be moved closer to the origin the object will be smaller.

    P(2, 5)

    P

    Example :

    P(2, 5), Sx = 0.5, Sy = 0.5

    Find P ?

    If the scale factors are larger than 1

    the points will be moved away from the origin the object will be larger.

    P

    Example :

    P(2, 5), Sx = 2, Sy = 2

    Find P ?

  • Scaling If the scale factors are the same,

    Sx = Sy uniform scaling Only change in size (as previous

    example)

    P(1, 2)

    P

    If Sx Sy differential scaling. Change in size and shape

    Example : square rectangle P(1, 3), Sx = 2, Sy = 5 , P ?

    What does scaling by 1 do?

    What is that matrix called?

    What does scaling by a negative value do?

  • Combining transformations

    We have a general transformation of a point:

    P' = M P + A

    When we scale or rotate, we set M, and A is the additive identity.

    When we translate, we set A, and M is the multiplicative identity.

    To combine multiple transformations, we must explicitly compute each transformed point.

    Itd be nicer if we could use the same matrix operation all the time. But wed have to combine multiplication and addition into a single operation.

  • Homogenous Coordinates

    Lets move our problem into 3D.

    Let point (x, y) in 2D be represented by point (x, y, 1) in the

    new space.

    Scaling our new point by any value a puts us somewhere along

    a particular line: (ax, ay, a).

    A point in 2D can be represented in many ways in the new

    space.

    (2, 4) ---------- (8, 16, 4) or (6, 12, 3) or (2, 4, 1) or etc.

    y y

    x

    x

    w

  • Homogenous Coordinates

    We can always map back to the original 2D point by dividing

    by the last coordinate

    (15, 6, 3) --- (5, 2).

    (60, 40, 10) - ?.

    Why do we use 1 for the last coordinate?

    The fact that all the points along each line can be mapped

    back to the same point in 2D gives this coordinate system its

    name homogeneous coordinates.

  • Matrix Representation-

    Homogeneous Transformations Point in column-vector:

    Our point now has three coordinates. So our matrix is

    needs to be 3x3.

    Translation

    x

    y

    1

    =

    1100

    10

    01

    1

    y

    x

    t

    t

    y

    x

    y

    x

  • Rotation

    Scaling

    Matrix Representation

    =

    1100

    0cossin

    0sincos

    1

    y

    x

    y

    x

    =

    1100

    00

    00

    1

    y

    x

    s

    s

    y

    x

    y

    x

  • Inverse Transformations

    1 1 1

    1 0 cos sin 0 1 0 0

    0 1 , sin cos 0 , 0 1 0

    0 0 1 0 0 1 0 0 1

    x x

    y y

    t S

    t S

    = = =

    T R S

  • Composite Transformation

    We can represent any sequence of transformations as a single matrix.

    No special cases when transforming a point matrix vector.

    Composite transformations matrix matrix.

    Composite transformations: Rotate about an arbitrary point translate, rotate,

    translate

    Scale about an arbitrary point translate, scale, translate

    Change coordinate systems translate, rotate, scale

    Does the order of operations matter?

  • Composition Properties

    Is matrix multiplication associative?

    (A.B).C = A.(B.C)

    + + + + + +

    + + + + + + =

    + +

    + + =

    dhl cfl dgj cej dhk cfk dgi cei

    bhl afl bgj aej bhk afk bgi aei

    l k

    j i

    dh cf dg ce

    bh af bg ae

    l k

    j i

    h g

    f e

    d c

    b a

    + + + + + +

    + + + + + + =

    + +

    + +

    =

    dhl dgj cfl cej dhk dgi cfk cei

    bhl bgj afl aej bhk bgi afk aei

    hl gj hk gi

    fl ej fk ei

    d c

    b a

    l k

    j i

    h g

    f e

    d c

    b a

    ?

  • Is matrix multiplication commutative?

    A . B = B . A

    Composition Properties

    ?

    ++

    ++=

    dhcfdgce

    bhafbgae

    hg

    fe

    dc

    ba

    ++

    ++=

    hdgbhcga

    fdebfcea

    dc

    ba

    hg

    fe

  • Order of operations

    So, it does matter. Lets look at an example:

    1. Translate

    2. Rotate

    1. Rotate

    2. Translate

  • Composite Transformations

    Translations

    +

    +

    =

    100

    10

    01

    100

    10

    01

    100

    10

    01

    21

    21

    1

    1

    2

    2

    yy

    xx

    y

    x

    y

    x

    tt

    tt

    t

    t

    t

    t

    }),({),( 1122' PttTttTP yxyx =

    PttTttT yxyx = )},(),({ 1122

    ),(),(),( 21211122 yyxxyxyx ttttTttTttT ++=

  • Composite Transformations

    Scaling

    =

    100

    00

    00

    100

    00

    00

    100

    00

    00

    21

    21

    1

    1

    2

    2

    yy

    xx

    y

    x

    y

    x

    ss

    ss

    s

    s

    s

    s

    ),(),(),(21211122 yyxxyxyx

    ssssSssSssS =

  • Composite Transformations

    Rotations

    })({)( 12' PRRP =

    PRR = )}()({ 12

    )()()( 2112 += RRR

    ++

    ++

    =

    100

    0)cos()sin(

    0)sin()cos(

    100

    0cossin

    0sincos

    100

    0cossin

    0sincos

    2121

    2121

    11

    11

    22

    22

  • Composite Transformation Matrix

    Arrange the transformation matrices in order from right to left.

    General Pivot- Point Rotation Operation :-

    1. Translate (pivot point is moved to origin)

    2. Rotate about origin

    3. Translate (pivot point is returned to original position)

    T(pivot) R() T(pivot)

    1 0 -tx 0 1 -ty 0 0 1

    cos -sin 0

    sin cos 0

    0 0 1

    1 0 tx 0 1 ty 0 0 1 . .

    cos -sin -tx cos+ ty sin + tx

    sin cos -tx sin - ty cos + ty

    0 0 1

    cos -sin -tx cos+ ty sin

    sin cos -tx sin - ty cos

    0 0 1

    1 0 tx 0 1 ty 0 0 1 .

  • General Pivot-Point Rotation

    Rotations about any selected pivot point (xr,yr)

    Translate-rotate-translate

  • General Pivot-Point Rotation

    100

    10

    01

    100

    0cossin

    0sincos

    100

    10

    01

    r

    r

    r

    r

    y

    x

    y

    x

    +

    =

    100

    sin)cos1(cossin

    sin)cos1(sincos

    rr

    rr

    xy

    yx

    ),,(),()(),( rrrrrr yxRyxTRyxT =

  • Example

    Perform 60 rotation of a point P(2, 5) about a

    pivot point (1,2). Find P?

    General Pivot Point Rotation

    cos -sin -tx cos+ ty sin + tx

    sin cos -tx sin - ty cos + ty

    0 0 1

    x

    y

    1 .

    0.5 -0.866 -1.0.5 + 2.0.866 + 1

    0.866 0.5 -1.0.866- 2.0.5 + 2

    0 0 1

    2

    5

    1 .

    0.5 - 0.866 2.232

    0.866 0.5 0.134

    0 0 1

    2

    5

    1

    . = -1.098

    4.366

    1 P = (-1, 4)

    Sin 60 = 0.8660

    Cos 60 = 1/2

  • Without using composite

    homogenous matrix Example

    Perform 90 rotation of a point P(5, 1) about a pivot

    point (2, 2). Find P?

    1. Translate pivot point ( tx = -2, ty = -2) P(5, 1 ) P (3, -1)

    2. Rotate P = 90 degree

    P(3, -1) -- > cos 90 -sin 90 3 = 0 -1 3 = 1

    sin 90 cos 90 -1 1 0 -1 3

    3. Translate back pivot point (tx = 2 , ty = 2)

    original point (1, 3 ) transformed point (3, 5)

  • Composite Transformation Matrix

    General Fixed-Point Scaling

    Operation :- 1. Translate (fixed point is moved to origin)

    2. Scale with respect to origin

    3. Translate (fixed point is returned to original position)

    T(fixed) S(scale) T(fixed)

    Find the matrix that represents scaling of an

    object with respect to any fixed point?

  • General Fixed-Point Scaling

    Scaling with respect to a selected fixed position (xf,yf)

  • General Fixed-Point Scaling

    Translate-scale-translate

    100

    10

    01

    100

    00

    00

    100

    10

    01

    r

    r

    y

    x

    r

    r

    y

    x

    s

    s

    y

    x

    =

    100

    )1(0

    )1(0

    yfy

    xfx

    sys

    sxs

    ),,,(),(),(),( rrffffyxff yxyxSyxTssSyxT =

  • Given P(6, 8) , Sx = 2, Sy = 3 and fixed point (2, 2). Use

    composite transformation matrix to find P?

    1 0 -tx 0 1 -ty 0 0 1

    Sx 0 0

    0 Sy 0

    0 0 1

    1 0 tx 0 1 ty 0 0 1 . .

    Sx 0 -tx Sx

    0 Sy -ty Sy

    0 0 1

    1 0 tx 0 1 ty 0 0 1 . =

    x =6, y = 8, Sx = 2, Sy = 3, tx =2, ty = 2

    Sx 0 -tx Sx + tx

    0 Sy -ty Sy + ty

    0 0 1

    2 0 -2( 2) + 2

    0 3 -2(3) + 2

    0 0 1 .

    6

    8

    1 =

    10

    20

    1

  • Composite Transformation Matrix

    General Scaling Direction

    Operation :- 1. Rotate (scaling direction align with the coordinate axes)

    2. Scale with respect to origin

    3. Rotate (scaling direction is returned to original position)

    R() S(scale) R()

    Find the composite transformation matrix?

  • General Scaling Directions

    Scaling factors sx and sy scale objects along the x and y

    directions.

    We scale an object in other directions with scaling

    factors s1 and s2

  • General Scaling Directions

  • Another Example

    Rotation followed by translation followed by

    scaling

  • cos -sin 0

    sin cos 0

    0 0 1

    Sx 0 0

    0 Sy 0

    0 0 1

    1 0 tx 0 1 ty 0 0 1 . .

    S . T . R

    cos -sin tx

    sin cos ty

    0 0 1

    Sx 0 0

    0 Sy 0

    0 0 1 .

    Sxcos Sx(-sin) Sx tx

    Sy sin Sy cos Sy ty

    0 0 1

  • Other transformations

    Reflection:

    x-axis y-axis

    100

    010

    001

    100

    010

    001

  • Other transformations

    Reflection:

    origin line x=y

    100

    010

    001

    100

    001

    010

  • Reflection

    A transformation produces a mirror image of an object.

    Axis of reflection

    A line in the xy plane

    A line perpendicular to the xy plane

    The mirror image is obtained by rotating the object 1800 about

    the reflection axis.

    Rotation path

    Axis in xy plane: in a plane perpendicular to the xy plane.

    Axis perpendicular to xy plane: in the xy plane.

  • Reflection

    Reflection about the x axis

  • Reflection

    Reflection about the y axis

  • Reflection

    Reflection relative to the coordinate origin

  • Reflection

    Reflection of an object relative to an axis perpendicular

    to the xy plane through Prfl

  • Reflection

    Reflection about the line y = x

  • Other transformations

    Shear:

    x-direction y-direction

    100

    010

    01 xsh

    100

    01

    001

    ysh

  • Shear

    The x-direction shear relative to x axis

    100

    010

    01 xsh yshxx x +='

    yy ='

    If shx = 2:

  • Shear

    The x-direction shear relative to y = yref

    100

    010

    1 refxx yshsh )(' refx yyshxx +=

    yy ='

    If shx = yref = -1:

    1 1/2 3/2

  • Shear

    The y-direction shear relative to x = xref

    100

    1

    001

    refyy xshshxx ='

    yxxshy refy += )('

    If shy = xref = -1:

    1

    1/2

    3/2

  • Coordinate System Transformations

    We often need to transform points from one coordinate system to

    another:

    1. We might model an object in non-Cartesian space (polar)

    2. Objects may be described in their own local system

    3. Other reasons: textures, display, etc

  • Transformations between Coordinate

    Systems

    x

    y y

    x

    x0

    y0

    x

    y

    y x

  • Transformations between Coordinate

    Systems

    =

    100

    10

    01

    ),( 0

    0

    00 y

    x

    yxT

    =

    100

    0cossin

    0sincos

    )(

    R

    ),()( 00'', yxTRM yxxy =

    Method 1:

    Method 2:

    ),( yx vvV

    Vv ==

    ),(),( yxxy uuvvu ==

    =

    100

    0

    0

    yx

    yx

    vv

    uu

    R

  • END