Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module-2 Fill Area Primitives, 2D Geometric Transformations and 2D Viewing

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 54

Module-2

Fill area Primitives, 2D Geometric


Transformations and 2D viewing

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 1


06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 2
(Line Walking
approach)

(When deciding if scan-line is


inside the polygon)

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 3


06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 4
Area Filling (Scan line Approach)

• For each scan line (increasing y order)


(1) Find intersections (the extrema of spans)
(2) Sort intersections (increasing x order)
(3) Fill in between pair of intersections

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 5


Data Structure

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 6


06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 7
06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 8
06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 9
void boundaryFill (int x, int y, int fillColor, int borderColor)
{
int interiorColor;
/* Set current color to fillColor, then perform the following operations. */
getPixel (x, y, interiorColor);
if ((interiorColor != borderColor) && (interiorColor != fillColor))
{
setPixel (x, y); // Set color of pixel to fillColor.
boundaryFill (x + 1, y , fillColor, borderColor);
boundaryFill (x - 1, y , fillColor, borderColor);
boundaryFill (x , y + 1, fillColor, borderColor);
boundaryFill (x , y - 1, fillColor, borderColor)
} }
06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 10
void floodFill (int x, int y, int fillColor, int interiorColor)
{
int color;
/* Set current color to fillColor, then perform the following operations. */
getPixel (x, y, color);
if (color = interiorColor)
{
setPixel (x, y); // Set color of pixel to fillColor.
floodFill (x + 1, y, fillColor, interiorColor);
floodFill (x - 1, y, fillColor, interiorColor);
floodFill (x, y + 1, fillColor, interiorColor);
floodFill (x, y - 1, fillColor, interiorColor)
} }

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 11


06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 12
8

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 13


06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 14
AF'

AB

FE'

CD
AB
CB
ED

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 15


2D Transformations

“Transformations are the operations applied to


geometrical description of an object to change its
position, orientation, or size are called geometric
transformations”.

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 16


Why Transformations ?

“Transformations are needed to manipulate


the initially created object and to display the
modified object without having to redraw it.”

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 17


• Translation

• Rotation

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 18


• Scaling
• Uniform Scaling

• Un-uniform Scaling

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 19


• Reflection

• Shear

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 20


Translation
• A translation moves all points
in an object along the same
straight-line path to new
positions.
• The path is represented by a P'(8,6)
vector, called the translation or
shift vector.
• We can write the components: ty=4
p'x = px + tx
p'y = py + ty P(2, 2) tx = 6
• or in matrix form:
P' = P + T
x' x tx
y' = y + ty

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 21


Rotation
• A rotation repositions
all points in an object
along a circular path in P
the plane centered at
the pivot point. 

• First, we’ll assume the P


pivot is at the origin.

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 22


Rotation
• Review Trigonometry
=> cos  = x/r , sin = y/r
• x = r. cos , y = r.sin 
P’(x’, y’)

=> cos (+ ) = x’/r



• x’ = r. cos (+ )
r
• x’ = r.coscos -r.sinsin y’ P(x,y)
• x’ = x.cos  – y.sin 
 r y

=>sin (+ ) = y’/r x’ x
y’ = r. sin (+ )
• y’ = r.cossin + r.sincos Identity of Trigonometry

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

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 23


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 
counterclockwise (+ve as our
example). y’
P(x,y)
• Rotation matrix
 r
cos  sin  y
R  
 sin cos  x
x’

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 24


Scaling
• Scaling changes the size of
an object and involves two
scale factors, Sx and Sy for
P’
the x- and y- coordinates
respectively.
• Scales are about the origin.
• We can write the
components:
P
p'x = sx • px
p'y = sy • py
or in matrix form:
P' = S • P
Scale matrix as:
sx 0
S
0 s y 

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 25


Scaling

• If the scale factors are in between


0 and 1:----
•  the points will be moved closer
to the origin P(2, 5)
•  the object will be smaller.
P’

• Example :
• P(2, 5), Sx = 0.5, Sy = 0.5

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 26



Scaling
If the scale factors are in between
0 and 1  the points will be
P’
moved closer to the origin  the
object will be smaller.
• Example :
• P(2, 5), Sx = 0.5, Sy = 0.5
P(2, 5)

• If the scale factors are larger than


1  the points will be moved P’

away from the origin  the


object will be larger.
• Example :
• P(2, 5), Sx = 2, Sy = 2

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 27


Scaling
P’

• If the scale factors are the same,


Sx = Sy  uniform scaling
• Only change in size (as previous
example)
• If Sx  Sy  differential scaling. P(1, 2)
• Change in size and shape
• Example : square  rectangle
• P(1, 3), Sx = 2, Sy = 5

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 28


General pivot point rotation
• Translate the object so that pivot-position is moved to the
coordinate origin
• Rotate the object about the coordinate origin
• Translate the object so that the pivot point is returned to its
original position
(xr,yr)

(b) (c) (d)


(a)
Translation of Rotation was Translation of the
Original object so that the
Position of object so that about origin
pivot point pivot point is
Object and returned to position
pivot point (xr,yr) is at
(xr,yr)
origin

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 29


General fixed point scaling
• Translate object so that the fixed point coincides with the
coordinate origin
• Scale the object with respect to the coordinate origin
• Use the inverse translation of step 1 to return the object to its
original position

(xf,yf)

(a) (b)
Translation of (c) (d)
Original
Position of object so that scaling was Translation of the
Object and fixed point about origin object so that the
Fixed point (xf,yf)is at Fixed point is
origin returned to position
(xf,yf)

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 30


1. Translate a triangle with vertices at original coordinates
(10,20), (10,10), (20,10) by tx=5, ty=10.
2. Scale a triangle with respect to the origin, with vertices at
original coordinates (10,20), (10,10), (20,10) by sx=2, sy=1.5,
3. Rotate a triangle about the origin with vertices at original
coordinates (10,20), (10,10), (20,10) by 30 degrees.
4. Rotate a triangle with vertices (10,20), (10,10), (20,10) about
the origin by 45 degrees and then translate it by tx=5, ty=10

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 31


COMPOSITE TRANSFORMATIONS
If we want to apply a series of transformations
T1, T2, T3 to a set of points, We can do it in two ways:

1) * We can calculate p'=T1*p, p''= T2*p', p'''=T3*p''

2) * Calculate T= T1*T2*T3, then p'''= T*p.

Method 2, saves large number of additions and


multiplications (computational time) – needs approximately 1/3 of as
many operations.
Therefore, we concatenate or compose the matrices
into one final transformation matrix, and then apply that to the
points.

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 32


Composite Transformations
(A) Translations
If two successive translation vectors (tx1,ty1) and (tx2,ty2) are applied to
a coordinate position P, the final transformed location P’ is calculated
as: -
P’=T(tx2,ty2) . {T(tx1,ty1) .P}
={T(tx2,ty2) . T(tx1,ty1)} .P
Where P and P’ are represented as homogeneous-coordinate column
vectors. We can verify this result by calculating the matrix product for
the two associative groupings. Also, the composite transformation
matrix for this sequence of transformations is: -
1 0 tx2 1 0 tx1 1 0 tx1+tx2

0 1 ty2 . 0 1 ty1 = 0 1 ty1+ty2

0 0 1 0 0 1 0 0 1

Or, T(tx2,ty2) . T(tx1,ty1) = T(tx1+tx2, ty1+ty2)


Which demonstrate that two successive translations are additive.

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 33


(B) Rotations
Two successive rotations applied to point P produce the transformed
position: -
P’= R(Ө2) . {R(Ө1 ) . P}
= {R(Ө2) . R(Ө1)} . P
By multiplication the two rotation matrices, we can verify that two
successive rotations are additive:
R(Ө2) . R(Ө1) = R (Ө1+ Ө2)
So that the final rotated coordinates can be calculated with the
composite rotation matrix as: -
P’ = R(Ө1+ Ө2) . P

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 34


(C) Scaling
Concatenating transformation matrices for two successive scaling
operations produces the following composite scaling matrix: -

Sx2 0 0 Sx1 0 0 Sx1 . Sx2 0 0


0 Sy2 0 . 0 Sy1 0
= 0 Sy1 .Sy2 0
0 0 1 0 0 1 0 0 1

Or, S(Sx2, Sy2 ) . S(Sx1, Sy1) = S (Sx1 . Sx2, Sy1 .Sy2 )

The resulting matrix in this case indicates that successive scaling


operations are multiplicative.

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 35


Rotation about an arbitrary point P in space
As we mentioned before, rotations are
applied about the origin. So to rotate about
any arbitrary point P in space, translate so that
P coincides with the origin, then rotate, then
translate back. Steps are:

• Translate by (-Px, -Py)

• Rotate

• Translate by (Px, Py)

06/25/2022 ELAIYARAJA P(COMPUTER 36


GRAPHICS)
Rotation about an arbitrary
point P in space

P1 
House at P1 Rotation by 

P1
Translation Translatio
of P1 to Origin n back to
06/25/2022 P1
ELAIYARAJA P(COMPUTER 37
GRAPHICS)
Rotation about an arbitrary point P in space

T = T3(Px, Py) * T2() * T1(-Px, -Py)

06/25/2022 ELAIYARAJA P(COMPUTER 38


GRAPHICS)
Scaling about an arbitrary point in Space

Again,
• * Translate P to the origin
• * Scale
• * Translate P back

•T = T1(Px, Py)* T2(Sx, Sy)*T3(-Px, -Py)

Sx 0 (Px*(1-Sx))
T=
0 Sy (Py*(1-Sy))

0 0 1

06/25/2022 ELAIYARAJA P(COMPUTER 39


GRAPHICS)
Other transformations
• Reflection is a transformation that produces a mirror image of
an object. It is obtained by rotating the object by 180 deg about
the reflection axis

1 Original position Reflection about the line y=0, the


X- axis , is accomplished with the
transformation matrix

1 0 0
2 3
0 -1 0
2’ 3’
0 0 1

1’
Reflected position

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 40


Reflection through an arbitrary line
Steps:
• Translate line to the origin

• Rotation about the origin

• Reflection matrix

• Reverse the rotation

• Translate line back

TGenRfl  R Trfl R T 1
r
Tr T
06/25/2022 ELAIYARAJA P(COMPUTER 41
GRAPHICS)
Commutivity of Transformations
If we scale, then translate to the origin, and then
translate back, is that equivalent to translate to origin,
scale, translate back?
When is the order of matrix multiplication
unimportant?
When does T1 * T2 = T2 * T1?
Cases where T1 * T2 = T2 * T1:

T1 T2
translation translation
scale scale
rotation rotation
scale(uniform) rotation
06/25/2022 ELAIYARAJA P(COMPUTER 42
GRAPHICS)
Order:
R-G-B

Scale, translate Translate, scale

Rotate, differential Differential scale,


scale rotate
06/25/2022 ELAIYARAJA P(COMPUTER 43
GRAPHICS)
Reflection

Original position Reflected position


2 2’ Reflection about the line x=0, the
Y- axis , is accomplished with the
1 1’
transformation matrix
3 3’

-1 0 0
0 1 0
0 0 1

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 44


Reflection
Reflection of an object relative to an axis perpendicular
to the xy plane and passing through the coordinate origin
Y-axis

-1 0 0
Reflected position 0 -1 0
3’
0 0 1
2’
1’ The above reflection matrix is
Origin X-axis the rotation matrix with
1 O (0,0) angle=180 degree.
2 This can be generalized to any
reflection point in the xy plane.
3 This reflection is the same as a
180 degree rotation in the xy
Original position plane using the reflection point
as the pivot point.

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 45


Reflection of an object w.r.t the straight
line y=x

0 1 0
Y-axis
Original position 1 0 0
3
0 0 1
2 1
1’

3’
Reflected position
2’
Origin O X-axis
(0,0)

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 46


Reflection of an object w.r.t the straight
Y-axis
line y=-x

0 -1 0
-1 0 0
0 0 1

X-axis
Origin 2
O (0,0) 3
Original position
1
1’
2’ Line Y = - X
3’
Reflected position

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 47


Reflection of an arbitrary axis y=mx+b
Original position
3

2 1

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 48


Original position Translation so that it passes through origin
3 Rotate so that it coincides with x-
Original position axis and reflect also about x-axis
3
2 1
2 1
Original position
2 3

1
Rotate back Translate back 1’
Original position
Original position 3 Reflected position
3
2’ 3’
2
2 1 1
1’ Reflected position
1’
3’ 3’

2’ Reflected position
2’

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 49


Shear Transformations

• Shear is a transformation that distorts the shape of an object such


that the transformed shape appears as if the object were composed
of internal layers that had been caused to slide over each other
• Two common shearing transformations are those that shift
coordinate x values and those that shift y values

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 50


Shears

Original Data y Shear x Shear

1 0 0 1 shx 0
shy 1 0 0 1 0
0 0 1 0 0 1

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 51


An X- direction Shear
For example, Shx=2

(2,1) (3,1)
(0,1) (1,1)

(0,0) (1,0) (0,0) (1,0)

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 52


An Y- direction Shear
For example, Shy=2

Y Y (1,3)

(1,2)

(0,1) (1,1) (0,1)

(1,0) X (0,0) X
(0,0)

06/25/2022 ELAIYARAJA P(COMPUTER GRAPHICS) 53


END OF MODULE 2

06/25/2022 ELAIYARAJA P(COMPUTER 54


GRAPHICS)

You might also like