0% found this document useful (0 votes)
50 views63 pages

2D Transformations - Basics of Viewing 1683152046587

Uploaded by

MADHUSUDAN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views63 pages

2D Transformations - Basics of Viewing 1683152046587

Uploaded by

MADHUSUDAN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Understand the Pointers

in C Programming
NTA UGC NET June
2019
(Computer Science)
2D TRANSFORMATIONS
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
2D Transformations
– Object Transformation
• Alter the coordinates descriptions an object
• Translation, rotation, scaling etc.
• Coordinate system unchanged
– Coordinate transformation
• Produce a different coordinate system
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

c a c
a  = b d
b d
Matrix Math

• Matrix Multiplication
– Dot product

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


c d
. g h c.e + d.g c.f + d.h
Matrix Math
• Is there a difference between possible representations?

a b    e  = ae + bf 
 c d   f   ce + df 
     
a b  =
[e f ]   [ae + cf
c d 
be + df ]

a c  =
[e f ]   [ae + bf
b d 
ce + df ]
Matrix Math
• We’ll 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.

A B   x   Ax + By 
C   = 
 D  y  Cx + Dy 

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: ty=4
p'x = px + tx
(2, 2) tx = 6
p'y = py + ty
• or in matrix form:
x’ P' = Px + T tx
y’ = y + ty
Rotation
• A rotation repositions
all points in an object
along a circular path in P’
the plane centered at 
the pivot point.
P

• First, we’ll assume the


pivot is at the origin.
• Review Trigonometry Rotation
=> cos  = x/r , sin = y/r
• x = r. cos , y = r.sin 
Rotation
=> cos (+ ) = x’/r
P’(x’, y’)
•x’ = r. cos (+ )
•x’ = r.coscos -r.sinsin 
•x’ = x.cos  – y.sin  r y’ P(x,y)
=>sin (+ ) = y’/r  r y
y’ = r. sin (+ ) 
x’ x
•y’ = r.cossin + r.sincos Identity of Trigonometry

•y’ = x.sin  + y.cos 


Rotation
• We can write the components:
p'x = px cos  – py sin 
p'y = px sin  + py cos 
P’(x’, y’)

• or in matrix form:

P' = R • P
•  can be clockwise (-ve) or y’ P(x,y)
counterclockwise (+ve as our  r y
example). 
• Rotation matrix x’ x
cos  sin  
R=
 sin  cos  
Rotation
• Example
– Find the transformed point, P’, caused by rotating P= (5, 1) about
the origin through an angle of 90.

cos  sin    x   x  cos  y  sin  


 sin    = 
 cos  
  y   x  sin  + y  cos 

5  cos 90  1  sin 90


= 
5  sin 90 + 1  cos 90

5  0  1 1
= 
5 1 + 1  0
  1
= 
5

Scaling
Scaling changes the size of an object
and involves two scale factors, Sx and
Sy for the x- and y- coordinates
respectively. P’

• Scales are about the origin.


• We can write the components:
p'x = sx • px
P
p'y = sy • py
or in matrix form:
P' = S • P
Scale matrix as:  s x 0
S =
0 sy 


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.

• Example :
•P(2, 5), Sx = 0.5, Sy = 0.5 P(2, 5)
•Find P’ ?
P’
Scaling
P’

P(2, 5)

•If the scale factors are larger than 1


P’
 the points will be moved away from
the origin  the object will be larger.
• Example :
•P(2, 5), Sx = 2, Sy = 2
•Find P’ ?
Scaling
• If the scale factors are the same, Sx = Sy  P’
uniform scaling
• Only change in size (as previous example)

•If Sx  Sy  differential scaling.


•Change in size and shape
•Example : square  rectangle
P(1, 2)
•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?
Homogenous Coordinates
y y
w
x 
x
• Let’s 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.
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
• Point in column-vector:
x
y
1
• Our point now has three coordinates. So our matrix is needs to be
3x3.
• Translation  x  1 0 t x   x 
 y  =  0 1 t    y 
   y  

1   0 0 1    1 

Matrix Representation
• Rotation
 x  cos  sin  0  x 
 y =  sin  cos 0   y
     

1   0 0 1
  1 

 x   s x 0 0  x 
• Scaling  y =  0
   sy 0   y
  

1  0 0 1
  1 

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)
 a b   e f  ÷  i j  = ae + bg af + bh   i j 
  c d    g h  ÷
 
k l  ce + dg cf + dh  k l 
+ + + + + +
= aei bgi afk bhk aej bgj afl bhl 
 cei + dgi + cfk + dhk cej + dgj + cfl + dhl 

a b     e f   i j  ÷ = a b    ei + fk ej + fl 
c d    g h  k l  ÷    
     c d   gi + hk gj + hl 
+ + + + + +
= aei afk bgi bhk aej afl bgj bhl 
 cei + cfk + dgi + dhk cej + cfl + dgj + dhl 
Composition Properties
• Is matrix multiplication commutative?
?
–A.B=B.A
a b e f  ae + bg af + bh
c    =
 d  g h   ce + dg cf + dh 

e f  a b   ea + fc eb + fd 
g    =
 h  c d   ga + hc gb + hd 

Order of operations
So, it does matter. Let’s look at an example:
1. Translate 1. Rotate
2. Rotate 2. Translate
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 cos -sin 0 1 0 -tx
0 1 ty sin cos 0 0 1 -ty
0 0 1 . 0 0 1
. 0 0 1
1 0 tx cos -sin -tx cos+ ty sin
0 1 ty sin cos -tx sin - ty cos
0 0 1 . 0 0 1
cos -sin -tx cos+ ty sin + tx
sin cos -tx sin - ty cos + ty
Composite Transformation Matrix

• Example
– Perform 60 rotation of a point P(2, 5) about a pivot point (1,2).
Find P’?
cos -sin -tx cos+ ty sin + tx x Sin 60 = 0.8660
sin cos -tx sin - ty cos + ty . y
cos 60 = 1/2
0 0 1 1

0.5 -0.866 -1.0.5 + 2.0.866 + 1 2


0.866 0.5 -1.0.866- 2.0.5 + 2 . 5
0 0 1 1
0.5 - 0.866 2.232 2 -1.098
0.866 0.5 0.134 . 5 = 4.366 P’ = (-1, 4)
0 0 1 1 1
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?

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


(2, 2). Use that matrix to find P’?
Answer
1 0 tx Sx 0 0 1 0 -tx
0 1 ty 0 Sy 0 0 1 -ty
0 0 1 . 0 0 1 . 0 0 1
1 0 tx Sx 0 -tx Sx Sx 0 -tx Sx + tx
0 1 ty 0 Sy -ty Sy =
0 0 1 . 0 0 1
0 Sy -ty Sy + ty
0 0 1
x =6, y = 8, Sx = 2, Sy = 3, tx =2, ty = 2

2 0 -2( 2) + 2 6 10
0 3 -2(3) + 2 . 8 = 20
0 0 1 1 1
Rotation
• Example
– (UGCNET-June2016-III-14) A point P(5,1) is rotated by 900
about a pivot point (2,2). What is the coordinate of new
transformed point P’ ?
– (A) (3,5) (B) (5,3) (C) (2,4) (D) (1,5)
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


by yourself !!
Other transformations
Reflection:
1 0 0
0 1 0

0 0 1  x-axis y-axis

 1 0 0
0 1 0

 0 0 1 
Other transformations
Reflection:

 1 0 0 0 1 0
0 1 0
 1 0  0
 0 0 1  0 0 1

origin line x=y


Other transformations
Shear:
1 shx 0  1 0 0
0 1 0  sh 1 0
  y
0 0 1  x-direction  0 0 1  y-direction
3D TRANSFORMATIONS
Basic 3D transformations: scaling
Some of the 3D transformations are just like 2D ones. For example,
scaling

Scaling
Translation in 3D
Rotation in 3D
Rotation now has more possibilities in 3D:
Rotation in 3D
What about the inverses of 3D rotations?
Shearing in 3D
Compositing multiple transformations
• Expressed as matrix multiplications:
p’ = M1 M2 p

means apply transformation M2 first, followed by M1

Historically, the vectors in graphics were row vectors,


but columns are more popular.
Compositing multiple transformations
3 steps
1) T(-2, -3)
2) R(30)
3) T(2, 3)

q’=T(2,3) R(30) T(-2,-3) q


2D Viewing
Viewing is the process of drawing a view of a model on a
2-dimensional display.
2D Viewing
• The geometric description of the object or scene provided by the model,
is converted into a set of graphical primitives, which are displayed where
desired on a 2D display.

• The same abstract model may be viewed in many different ways:


– e.g. faraway, near, looking down, looking up
Real World Coordinates
• It is logical to use dimensions which are appropriate to the object e.g.
– meters for buildings
– nanometers or microns for molecules, cells, atoms
– light years for astronomy

• The objects are described with respect to their actual physical size in the real
world, and then mapped onto screen co-ordinates.

• It is therefore possible to view an object at various sizes by zooming in and


out, without actually having to change the model.
2D Viewing
• How much of the model should be drawn?
• Where should it appear on the display?

• How do we convert Real-world coordinates into screen co-ordinates?

– We could have a model of a whole room, full of objects such as chairs, tablets
and students.
– We may want to view the whole room in one go, or zoom in on one single
object in the room.
– We may want to display the object or scene on the full screen, or we may
only want to display it on a portion of the screen.
• 2Dbeen
Once a model has Viewing
constructed, the
• programmer can specify a view.

A 2-Dimensional view consists of two rectangles:

– A Window, given in real-world co-ordinates, which defines the portion of the


model that is to be drawn

– A Viewport given in screen co-ordinates, which defines the portion of the


screen on which the contents of the window will be displayed
Basic Interactive Programming
• Window: What is to be viewed
• Viewport: Where is to be displayed

Viewport

Image
Coordinate Representations
• General graphics packages are designed to be used with Cartesian
coordinate specifications.

• Several different Cartesian reference frame are used to construct and


display a scene.
Coordinate Representations
• Modeling coordinates: We can construct the shape of individual objects
in a scene within separate coordinate reference frames called modeling
(local) coordinates.
Coordinate Representations
• World coordinates: Once individual object shapes have been
specified, we can place the objects into appropriate positions within the
scene using reference frame called world coordinate.
Coordinate Representations
• Device Coordinates: Finally, the world coordinates description of the scene
is transferred to one or more output-device reference frames for display,
called device (screen) coordinates.
Coordinate Representations
• Normalized Coordinates: A graphic system first converts world coordinate
positions to normalized device coordinates, in the range 0 to 1.This makes the
system independent of the output-devices.
Coordinate Representations
• An initial modeling coordinate position is transferred to a device
coordinate position with the sequence:
( xmc , y mc )  ( x wc , y wc )  ( xnc , y nc )  ( xdc , y dc )
• The modeling and world coordinate positions in this transformation can
be any floating values; normalized coordinates satisfy the inequalities:

0  x nc  1 0  y nc  1

• The device coordinates are integers within the range (0,0) to ( x , y


max max
)
for a particular output device.
The Viewing Pipeline
• A world coordinate area selected for display is called window.
• An area on a display device to which a window is mapped a viewport.

• Windows and viewports are rectangular in standard position.


The Viewing Pipeline
• The mapping of a part of a world coordinate scene to device coordinate is
referred to as viewing transformation or window-to-viewport
transformation or windowing transformation.

Viewport

window-to-viewport transformation
The Viewing Pipeline
1. Construct the scene in world coordinate using the output primitives.
2. Obtain a particular orientation for the window by set up a two dimensional
viewing coordinate system in the world coordinate, and define a window in
the viewing coordinate system. Transform descriptions in world coordinates
to viewing coordinates (clipping).
The Viewing Pipeline
3. Define a viewport in normalized coordinate, and map the viewing coordinate
description of the scene to normalized coordinate
4. (All parts lie outside the viewport are clipped), and contents of the viewport
are transferred to device coordinates.

1
Viewing Coordinate Normalized Coordinate Device Coordinate
The Viewing Pipeline
The Viewing Pipeline
• By Changing the position of the viewport, we can view objects at different
position on the display area of an output device.
The Viewing Pipeline
• By varying the size of viewport, we can change the size of displayed objects
(zooming).

You might also like