0% found this document useful (0 votes)
22 views77 pages

M2 CST304 Ktunotes - in

Module 2 covers filled area primitives and transformations in 2D graphics, including polygon filling techniques such as scan line, boundary fill, and flood fill algorithms. It also discusses 2D transformations like translation, rotation, scaling, reflection, and shearing, along with their matrix representations and homogeneous coordinates. The document provides algorithms for filling polygons and performing transformations efficiently using matrix operations.

Uploaded by

pkabhi444
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)
22 views77 pages

M2 CST304 Ktunotes - in

Module 2 covers filled area primitives and transformations in 2D graphics, including polygon filling techniques such as scan line, boundary fill, and flood fill algorithms. It also discusses 2D transformations like translation, rotation, scaling, reflection, and shearing, along with their matrix representations and homogeneous coordinates. The document provides algorithms for filling polygons and performing transformations efficiently using matrix operations.

Uploaded by

pkabhi444
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/ 77

Module 2

Module – 2 (Filled Area Primitives and transformations)


Filled Area Primitives- Scan line polygon filling, Boundary filling and
flood filling. Two dimensional transformations-Translation, Rotation,
Scaling, Reflection and Shearing, Composite transformations, Matrix
representations and homogeneous coordinates. Basic 3D
transformations.
Polygon Fill Algorithms
Polygon Representation
The polygon can be represented by listing its n
vertices in an ordered list.
P = {(x1, y1), (x2, y2), ……., (xn, yn)}.
The polygon can be displayed by drawing a line
between (x1, y1), and (x2, y2), then a line between
(x2, y2), and (x3, y3), and so on until the end vertex.
In order to close up the polygon, a line between (xn,
yn), and (x1, y1) must be drawn.

3
Polygon Fill Algorithm

• Different types of Polygons


• Simple Convex
• Simple Concave
• Non-simple : self-intersecting
• With holes

Convex Concave Self-intersecting


Polygon Filling
Types of filling
• Solid-fill
All the pixels inside the polygon’s boundary are illuminated.

• Pattern-fill
The polygon is filled with an arbitrary predefined pattern.

5
Types of Polygon fill algorithm
• 1.Scan line algorithm
• 2. Boundary Fill algorithm
• 3. Flood fill algorithm
The Scan-Line Polygon Fill Algorithm
The scan-line polygon-filling algorithm involves
• the horizontal scanning of the polygon from its
lowermost to its topmost vertex,
• identifying which edges intersect the scan-line,
• and finally drawing the interior horizontal lines with
the specified fill color. process.

7
Example

• Consider the following polygon:


Example

• For each scan line that crosses the polygon, the edge intersections are
sorted from left to right, and then the pixel positions between, and
including, each intersection pair are set to the specified fill color.

• In the previous Figure, the four pixel intersection positions with the
polygon boundaries define two stretches of interior pixels.
Polygon Fill Algorithm

• Consider the next Figure.


• It shows two scan lines that cross a polygon fill area and intersect a
vertex.
• Scan line y’ intersects an even number of edges, and the two pairs of
intersection points along this scan line correctly identify the interior
pixel spans.
• But scan line y intersects five polygon edges.
Polygon Fill Algorithm
Polygon Fill Algorithm

• To identify the interior pixels for scan line y, we must count the vertex
intersection as only one point.

• Thus, as we process scan lines, we need to distinguish between these


two cases.
Boundary Fill Algorithm
Introduction :
• Boundary Fill Algorithm starts at a pixel inside the
polygon to be filled and paints the interior
proceeding outwards towards the boundary.
• This algorithm works only if the color with which
the region has to be filled and the color of the
boundary of the region are different.
• If the boundary is of one single color, this approach
proceeds outwards pixel by pixel until it hits the
boundary of the region.
Boundary Fill Algorithm
Boundary Fill Algorithm is recursive in nature
• It takes an interior point(x, y), a fill color, and a
boundary color as the input.
• The algorithm starts by checking the color of (x, y). If
it’s color is not equal to the fill color and the boundary
color, then it is painted with the fill color and the
function is called for all the neighbors of (x, y).
• If a point is found to be of fill color or of boundary
color, the function does not call its neighbors and
returns.
• This process continues until all points up to the
boundary color for the region have been tested.
• The boundary fill algorithm can be implemented by 4-
connected pixels or 8-connected pixels.
Boundary Fill Algorithm
4-connected pixels :
• After painting a pixel, the function is called for four neighboring points. These are the
pixel positions that are right, left, above, and below the current pixel. Areas filled by
this method are called 4-connected.
• Algorithm:
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
Boundary Fill Algorithm
8-connected pixels :
More complex figures are filled using this approach. The pixels to be tested are the 8 neighboring pixels,
the pixel on the right, left, above, below and the 4 diagonal pixels. Areas filled by this method are called 8-
connected.
Algorithm :
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}
Boundary Fill Algorithm
4-connected pixels Vs 8-connected pixels :
Let us take a figure with the boundary color as GREEN and the fill
color as RED. The 4-connected method fails to fill this figure
completely. This figure will be efficiently filled using the 8-connected
technique.
Flood fill Vs Boundary fill :
Though both Flood fill and Boundary fill
algorithms color a given figure with a chosen
color, they differ in one aspect. In Flood fill, all
the connected pixels of a selected color get
replaced by a fill color. On the other hand, in
Boundary fill, the program stops when a given
color boundary is found.
Flood Fill Algorithm
• Sometimes we come across an object where we want to fill the
area and its boundary with different colors. We can paint such
objects with a specified interior color instead of searching for
particular boundary color as in boundary filling algorithm.

Instead of relying on the boundary of the object, it relies


on the fill color. In other words, it replaces the interior
color of the object with the fill color. When no more pixels
of the original interior color exist, the algorithm is
completed.

• Once again, this algorithm relies on the Four-connect or Eight-


connect method of filling in the pixels. But instead of looking for
the boundary color, it is looking for all adjacent pixels that are a
part of the interior.
Flood Fill Algorithm

• Algorithm (4-connect):
floodfill4 (x, y,fill_ color, old_color: integer)
{
If (getpixel (x, y)=old_color)
{
setpixel (x, y, fill_color);
fill (x+1, y, fill_color, old_color);
fill (x-1, y, fill_color, old_color);
fill (x, y+1, fill_color, old_color);
fill (x, y-1, fill_color, old_color);
}
}
Flood Fill Algorithm
• Algorithm:
floodfill8 (x, y,fill_color, old_color: integer)
{
If (getpixel (x, y)=old_color)
{
setpixel (x, y, fill_color);
floodfill(x+1,y,old,newcol);
floodfill(x-1,y,old,newcol);
floodfill(x,y+1,old,newcol);
floodfill(x,y-1,old,newcol);
floodfill(x+1,y+1,old,newcol);
floodfill(x-1,y+1,old,newcol);
floodfill(x+1,y-1,old,newcol);
floodfill(x-1,y-1,old,newcol);
}
2D Transformations
2D Transformations

P  ( x, y ) P  (3.7,4.1)
Given:
T  (t x , t y ) T  (7.1,8.2)

We want:
x'  3.7  7.1
x'  x  t x
y '  4.1  8.2
y'  y  t y
Matrix form:  x'  3.7   7.1
 x '   x  t x   y '    4.1  8.2
     
 y '   y   t 
     y x'  3.4
P'  P  T y '  4.1
2D Transformations
2D Transformations
Rotation:
2D Transformations
Rotation:
2D Transformations

Rotation:
P  ( x, y ) x'  r cos  cos  r sin  sin 
R  ( ) y '  r cos  sin   r sin  cos
x  r cos  x'  x cos  y sin 
y  r sin  y '  x sin   y cos
x'  r cos(   )  x' cos  sin    x 
y '  r cos(   )  y '   sin  cos   y 
  
P'  R  P
2D Transformations
2D Transformations - Scaling
P  ( x, y ) P  (1.4,2.2)
• Given:
S  (sx , s y ) S  (3,3)

x'  s x x x'  3 *1.4


• We want: y'  s y y y '  3 * 2.2
 x' 3 0 1.4 
 y '  0 3 2.2
 x'  s x 0  x     
 y '   0 
s y   y  x'  4.2
• Matrix form:   
P'  S  P y '  6.6
Matrix Representations and
Homogeneous Coordinates

 Many graphics applications involve


sequences of geometric transformations
 Animations
 Design and picture construction applications
 We will now consider matrix
representations of these operations
 Sequences of transformations can be
efficiently processed using matrices
Matrix Representations and
Homogeneous Coordinates

 To produce a sequence of operations,


such as scaling followed by rotation then
translation, we could calculate the
transformed coordinates one step at a time

 A more efficient approach is to combine


transformations, without calculating
intermediate coordinate values
Matrix Representations and
Homogeneous Coordinates

 Multiplicative and translational terms for


a 2D geometric transformation can be
combined into a single matrix if we
expand the representations to 3 by 3
matrices
 We can use the third column for translation
terms, and all transformation equations can
be expressed as matrix multiplications
Matrix Representations and
Homogeneous Coordinates

 Expand each 2D coordinate (x,y) to


three element representation (xh,yh,h)
called homogeneous coordinates
 h is the homogeneous parameter such
that x = xh/h, y = yh/h,
 A convenient choice is to choose h = 1
Matrix Representations and
Homogeneous Coordinates

 2D Translation Matrix

 x' 1 0 tx   x
 y '  0 1 t y    y 
  
 1  0 0 1   1 

or, P’ = T(tx,ty)·P
Matrix Representations and
Homogeneous Coordinates

 2D Rotation Matrix

 x' cos   sin  0  x 


 y '   sin  cos  0   y 
     
 1   0 0 1  1 

or, P’ = R(θ)·P
Matrix Representations and
Homogeneous Coordinates

 2D Scaling Matrix
 x'  s x 0 0  x 
 y '   0 sy 0   y 
  
 1   0 0 1  1 

or, P’ = S(sx,sy)·P
2D Composite Transformations

 Composite 2D Translations
 If two successive translation are applied to a point P,
then the final transformed location P' is calculated as

P '  T(t x2 , t y2 )  T(t x1 , t y1 )  P  T(t x1  t x2 , t y1  t y2 )  P

1 0 t 2 x  1 0 t1x  1 0 t1x  t 2 x 
0 1 t   0 1 t   0 1 t  t 
 2y   1y   1y 2y 

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


2D Composite Transformations

 Composite 2D Rotations

P'  R (1   2 )  P

cos  2  sin  2 0 cos 1  sin 1 0 cos(1   2 )  sin(1   2 ) 0


 sin  cos  0    sin  cos  0    sin(   ) cos(   ) 0
 2 2   1 1   1 2 1 2 
 0 0 1  0 0 1  0 0 1
2D Composite Transformations

 Composite 2D Scaling

S (sx2 , s y2 )  S (sx1 , s y1 )  S (sx1  sx2 , s y1  s y2 )

sx 2 0 0  s x1 0 0  s x1  s x 2 0 0
0 sy2 0   0 s y1 0   0 s y1  s y 2 0

 0 0 1  0 0 1  0 0 1
General Pivot Point Rotation

 Steps:
1. Translate the object so that the pivot point is
moved to the coordinate origin.
2. Rotate the object about the origin.
3. Translate the object so that the pivot point is
returned to its original position.
General Pivot Point Rotation
General Pivot Point Rotation

 General 2D Pivot-Point Rotation

1 0 xr  cos   sin  0 1 0  xr 
0 1 yr    sin  cos  0  0 1  yr 

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

cos   sin  xr (1  cos )  yr sin 


  sin  cos  yr (1  cos )  xr sin  
 0 0 1 
Problem:

Given a triangle ABC A(4,6) B(2,2)C(6,2). Rotate it by 90 degree


anticlockwise about the point (3,3).

Solution:

We have xr=3 yr=3

R(Ɵ)=90˚

1.Translate to origin T(-3,-3).


2. Then Rotate anticlockwise.
3.Translate back to original position T(3,3)
cos   sin  xr (1  cos )  yr sin 
o'   sin  cos  yr (1  cos )  xr sin   o
 0 0 1 
So as we have substituted all the values in the corresponding matrices. We
will arrange them in sequence. Always keep in mind matrix multiplication
is associative but not commutative. Arrange them from right to left and
object matrix being the rightmost.
General fixed Point Scaling
 Steps:
1. Translate the object so that the fixed point
coincides with the coordinate origin.
2. Scale the object about the origin.
3. Translate the object so that the pivot point is
returned to its original position.

(xr, yr) (xr, yr)


General fixed Point Scaling

• General 2D Fixed-Point Scaling:

1 0 𝑥𝑓 𝑠𝑥 0 0 1 0 − 𝑥𝑓 𝑠𝑥 0 𝑥𝑓 (1 − 𝑠𝑥 )
0 1 𝑦𝑓 . 0 𝑠𝑦 0 . 0 1 − 𝑦𝑓 = 0 𝑠𝑦 𝑦𝑓 (1 − 𝑠𝑦 )
001 0 0 1 00 1 0 0 1
Other 2D Transformations

 Reflection
 Transformation that produces a mirror image of an
object
Other 2D Transformations

 Reflection
 Image is generated relative to an axis of reflection
by rotating the object 180° about the reflection
axis
 Reflection about the line y=0 (the x axis) (previous
slide)
1 0 0
0  1 0 
 
0 0 1
Other 2D Transformations
 Reflection
 Reflection about the line x=0 (the y axis)

  1 0 0
 0 1 0
 
 0 0 1
Other 2D Transformations

 Reflection about the origin

−1 0 0
0 −1 0
0 0 1
Other 2D Transformations
 Shear
 Transformation that distorts the shape of an object
such that the transformed shape appears as the
object was pushed to slide in one (x or y)
direction.
y y

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

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


x (0,0) x
shx=2
Other 2D Transformations
 Shear
 An x-direction shear relative to the x axis
1 shx 0 x'  x  shx  y
=  
x’ x
y’
 0 1 0 y y'  y
1 1
0 0 1
 An y-direction shear relative to the y axis

 1 0 0
x’  sh  x y '  y  shy  x
y’
=
 y 1 0 y
1 x'  x
1
 0 0 1
Shearing in X-Y directions: Here layers will be slided in both x
as well as y direction. The sliding will be in horizontal as well as
vertical direction. The shape of the object will be distorted. The
matrix of shear in both directions is given by:
Other 2D Transformations
 Shear
 x-direction shear relative to other reference lines
1 shx  shx * yref 
x
 xl 0 1 0 
 
= y
 yl
 1 0 0 1  1

x'  x  shx *  y  yref 


y'  y
x'  x  shx *  y  yref 
1 shx  shx * yref 
Example 0 1
 0 

0 0 1  y'  y

A unit square (a) is transformed to a shifted parallelogram


(b) with shx = 0.5 and yref = −1 in the shear matrix
Other 2D Transformations

 Shear
 y-direction shear relative to the line x = xref
 1 0 0 
 sh 
1  shy * xref 
 y
 0 0 1 

x'  x
y '  x  shy * x  xref 
Other 2D Transformations –
Shear - Example

A unit square (a) is turned into a shifted parallelogram


(b) with parameter values shy = 0.5 and xref = −1 in the y -direction shearing
transformation
Basic 3D Transformations
Recap – 2D Transformations
o Translation: 1 0 t x 
0 1 t 
 y

0 0 1 
 s x 0 0
o Scaling:  0 s 0
 y 
 0 0 1

cos  sin  0
 sin  cos 0
o Rotation:  
 0 0 1
Basic 3D Transformations

 When the transformation takes place on a 3D plane .it


is called 3D transformation.
 Generalize from 2D by including z coordinate
Straight forward for translation and scale, rotation more
difficult

a b c tx 
Homogeneous coordinates: 4 components d e f t y 
g h i tz 
Transformation matrices: 4×4 elements  
0 0 0 1
Basic 3D Transformations - Translation
 Moving of object is called Translation.
 In 3 dimensional homogeneous coordinate
representation , a point is transformed from position P =
( x, y , z) to P’=( x’, y’, z’)
 This can be written as:-
Using P’ = T . P
 x  1 0 0 tx   x
 y   0 1 0 t y   y 
  
 z   0 0 1 tz   z 
     
 1  0 0 0 1  1 
Basic 3D Transformations - Rotation
Where an object is to be rotated about an axis that is parallel to
one of the coordinate axis, we can obtain the desired
rotation with the following transformation sequence.
Coordinate axis rotation
Z- axis Rotation
Y-axis Rotation
X-axis Rotation
Basic 3D Transformations - Rotation

The equation for X-axis Rotation

x’ = x
y’ = y cosθ – z sinθ
z’ = y sinθ + z cosθ

 x' 1 0 0 0  x 
 y ' 0 cos   sin  0  y 
   
 z '  0 sin  cos  0  z 
     
 1  0 0 0 1  1 
Basic 3D Transformations - Rotation

The equation for Y-axis Rotaion

x’ = x cosθ + z sinθ
y’ = y
z’ = z cosθ - x sinθ

 x'  cos  0 sin  0  x 


 y '  0 1 0 0  y 
   
 z '   sin  0 cos  0  z 
     
1  0 0 0 1  1 
Basic 3D Transformations - Rotation

The equation for Z-axis rotation

x’ = x cosθ – y sinθ
y’ = x sinθ + y cosθ
z’ = z

 x' cos   sin  0 0  x 


 y '  sin  cos  0 0  y 
  
 z'  0 0 1 0  z 
     
1  0 0 0 1  1 
Basic 3D Transformations - Scaling

 Changes the size of the object and repositions the object


relative to the coordinate origin.
 x   s x 0 0 0  x 
 y   0 s  
0 0  y  
  y

 z   0 0 s z 0  z 
     
1 0 0 0 1  1 

 The equations for scaling


x’ = x . sx
y’ = y . sy
z’ = z . sz
Basic 3D Transformations - Reflection

Reflection about x-axis:


x’=x y’=-y z’=-z

1 0 0 0
0 -1 0 0
0 0 -1 0
0 0 0 1
Reflection about y-axis:
y’=y x’=-x z’=-z
-1 0 0 0
0 1 0 0
0 0 -1 0
0 0 0 1

You might also like