0% found this document useful (0 votes)
88 views32 pages

C Programming Filled Area Primitives

The document discusses filled area primitives in C programming, detailing methods for coloring objects using filling algorithms like Floodfill, Boundary fill, and Scanline polygon fill. It explains polygon representation, inside-outside tests, and the algorithms for filling polygons, including their advantages and disadvantages. Additionally, it covers basic geometric transformations such as translation, rotation, and scaling, along with the concept of homogeneous coordinates for representing transformations.

Uploaded by

Bindhya Bibin
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)
88 views32 pages

C Programming Filled Area Primitives

The document discusses filled area primitives in C programming, detailing methods for coloring objects using filling algorithms like Floodfill, Boundary fill, and Scanline polygon fill. It explains polygon representation, inside-outside tests, and the algorithms for filling polygons, including their advantages and disadvantages. Additionally, it covers basic geometric transformations such as translation, rotation, and scaling, along with the concept of homogeneous coordinates for representing transformations.

Uploaded by

Bindhya Bibin
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

UNIT 2-Filled Area Primitives

Filled Area Primitives:

Note: For filling a given picture or object with color’s, we can do it in two ways in C programming. The two
ways are given below:
i. Using filling algorithms such as Floodfill algorithm, Boundary fill algorithm and scanline polygon fill
algorithm, we can color the objects.
ii. Using inbuilt graphics functions such as floodfill(), setfillstyle() we can fill the object with color’s
directly without using any filling algorithm.
Here we will see the filling algorithms

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.

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.
 One problem with this representation is that if we wish to translate the polygon, it is necessary to apply
the translation transformation to each vertex in order to obtain the translated polygon.

1
CSE Department
UNIT 2-Filled Area Primitives

Inside-Outside Tests
 When filling polygons we should decide whether a particular point is interior or exterior to a polygon.

 Two methods:
⚫ Odd-Even Rule/ Even-Odd Rule or Odd Parity Rule
⚫ Nonzero winding number rule

Odd-Even Rule
 A rule called the odd-parity (or the odd-even rule) is applied to test whether a point is interior or not.

Algorithm:

1. Construct a line segment from point to be examined to point outside of a polygon.


2. Count the number of intersections of line segment with polygon boundaries.
3. If Odd number of intersection, then Point lies inside of Polygon.
4. Else, Point lies outside of polygon.

This test fails in case line segment intersects at vertex point. To handle it, few modifications are made.

 If end points lie is at same side of constructed line segment, then even number of intersection is considered
for that intersection point.
 If end points lie at opposite side of it, then odd number of intersection is considered.

2
UNIT 2-Filled Area Primitives

Non-zero winding number rule


 A winding Number is calculated for given point with respect to polygon.
 If winding number is non-zero, then point lies inside the polygon. Else, it lies outside of polygon.

Calculation of Winding Number


 Give directions to all the edges of the polygon.
 Initializing the winding number to 0.
 Imagine a line drawn from any position P to a distant point beyond the coordinate extents of the object.
 Count the number of edges that cross the line in each direction.
o Add 1 to the winding number every time intersects a polygon edge that crosses the line from
RIGHT TO LEFT, and
o Subtract 1 every time intersects an edge that crosses from LEFT TO RIGHT.
 If the winding number is non-zero then point P is inside otherwise P is outside the polygon.

POLYGON FILLING ALGORITHMS


They are:
Three Algorithms for filling areas:

1) Scan Line Polygon Fill Algorithm


2) Boundary Fill Algorithm
3) Flood fill Algorithm

3
UNIT 2-Filled Area Primitives

Scan Line Polygon Fill Algorithm:


Scanline filling is basically filling up of polygons using horizontal lines or scanlines. The purpose of
the algorithm is to fill (color) the interior pixels of a polygon given only the vertices. The basic scan-line have 3
steps:
⚫ Find the intersections of the scan line with all edges of the polygon
⚫ Sort the intersections by increasing x coordinate
⚫ Fill in all pixels between pairs of intersections that lie interior to the polygon

4
UNIT 2-Filled Area Primitives

Determining Edge Intersections

𝑺𝒍𝒐𝒑𝒆, 𝒎 = (𝒚𝒌+𝟏 – 𝒚𝒌 ) / (𝒙𝒌+𝟏 – 𝒙𝒌 )


𝒚𝒌+𝟏 – 𝒚𝒌 = 𝟏
𝒙𝒌+𝟏 = 𝒙𝒌 + 𝟏/𝒎
Algorithm Steps:
1. The horizontal scanning of the polygon from its lowermost to its topmost vertex
2. Identify the edge intersections of scan line with polygon
3. Build the edge table
a. Each entry in the table for a particular scan line contains the maximum y value for that edge, the
x-intercept value (at the lower vertex) for the edge, and the inverse slope of the edge.

4. Determine whether any edges need to be splitted or not. If there is need to split, split the edges.
5. Add new edges and build modified edge table.
6. Build Active edge table for each scan line and fill the polygon based on intersection of scanline with
polygon edges.

Qus 1. Determine the content of the active edge table to fill the polygon with vertices A(2,4), B(4,6) and
C(4,1)for y=1 to 6

5
UNIT 2-Filled Area Primitives

6
UNIT 2-Filled Area Primitives

Increment x with inverse slope

7
UNIT 2-Filled Area Primitives

8
UNIT 2-Filled Area Primitives

Boundary Fill Algorithm:


 Start at a point inside a region and paint the interior outward toward the boundary.
 If the boundary is specified in a single color, the fill algorithm processed outward pixel by pixel
until the boundary color is encountered.
 A boundary-fill procedure accepts as input the coordinate of the interior point (x, y), a fill color,
and a boundary color.
Algorithm:
The following steps illustrate the idea of the recursive boundary-fill algorithm:
1. Start from an interior point.
2. If the current pixel is not already filled and if it is not an edge point, then set the pixel with the fill color, and
store its neighboring pixels (4 or 8-connected).
3. Store only neighboring pixel that is not already filled and is not an edge point.
4. Select the next pixel from the stack, and continue with step 2.

9
UNIT 2-Filled Area Primitives

In 4 connected approach, we can fill an In 8 connected approach, we can fill an


object in only 4 directions. We have 4 object in 8 directions. We have 8
possibilities for proceeding to next pixel possibilities for proceeding to next pixel
from current pixel. from current pixel.

 Implemented using stack datastructure.

Function for 4 connected approach:


fcolour= fillcolor
void boundary_fill(int x, int y, int fcolor, int bcolor)
bcolor=bordercolor
{
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{
putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor, bcolor);
boundary_fill(x - 1, y, fcolor, bcolor);
boundary_fill(x, y + 1, fcolor, bcolor);
boundary_fill(x, y - 1, fcolor, bcolor);
}
}
10
UNIT 2-Filled Area Primitives

4-connected (Example)

(a) (b) (c)

(d) (e) (f)

Drawback:
4 connected boundary fill produce partial fill
(g)

Function for 8 connected approach:

void boundary_fill(int x, int y, int fcolor, int bcolor)


{
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{
putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor, bcolor);
boundary_fill(x , y+1, fcolor, bcolor);
boundary_fill(x+1, y + 1, fcolor, bcolor);
boundary_fill(x-1, y - 1, fcolor, bcolor);
boundary_fill(x-1, y, fcolor, bcolor);
boundary_fill(x , y-1, fcolor, bcolor);
11
UNIT 2-Filled Area Primitives

boundary_fill(x-1, y + 1, fcolor, bcolor);


boundary_fill(x+1, y - 1, fcolor, bcolor);

}
}
Drawback:
It requires considerable stacking of neighboring points , So more efficient methods (Span Flood-Fill) are
generally employed.

Flood Fill Algorithm:


Flood fill algorithm is also known as a seed fill algorithm. This method is more suitable for filling multiple
colors boundary. It starts at a specified point and "floods" the area with a particular color or shade until it reaches
the boundaries of the area. This approach is called a flood-fill algorithm.

(a) Start from a specified interior pixel (x, y) and reassign all pixel values that are currently set to a given interior
color with the desired fill color.
(b) If the area has more than one interior color, first reassign pixel values so that all interior pixels have the same
color.
(c) Using either 4-connected or 8-connected approach, step through pixel positions until all interior pixels have
been repainted.

4-connected approach 8-connected approach

12
UNIT 2-Filled Area Primitives

4-connected Flood Fill algorithm:

floodfill4 (x, y, fillcolor, oldcolor)


if (getpixel (x, y)=oldcolor)
{
putpixel (x, y, fillcolor);
floodfill4 (x+1, y, fillcolor, oldcolor);
floodfill4 (x-1, y, fillcolor, oldcolor);
floodfill4 (x, y+1, fillcolor, oldcolor);
floodfill4 (x, y-1, fillcolor, oldcolor);
}
}

8-connected Flood Fill algorithm:

floodfill8 (x, y, fillcolor, oldcolor)


{
if (getpixel (x, y)=oldcolor)
{
putpixel (x, y, fillcolor);
floodfill8 (x+1,y,fillcolor, oldcolor);
floodfill8 (x-1,y, fillcolor, oldcolor);
floodfill8 (x,y+1, fillcolor, oldcolor);
floodfill8 (x,y-1, fillcolor, oldcolor);
floodfill8 (x + 1, y - 1, fillcolor, oldcolor);
floodfill8 (x + 1, y + 1, fillcolor, oldcolor);
floodfill8 (x - 1, y - 1, fillcolor, oldcolor);
floodfill8 (x - 1, y + 1, fillcolor, oldcolor);
}
}

Disadvantage:

1. Very slow algorithm


2. May be fail for large polygons
3. Initial pixel required more knowledge about surrounding pixels.

13
UNIT 2-Filled Area Primitives

Flood-fill Algorithm Boundary-fill Algorithm


 It can process the image containing more than one  It can only process the image containing
boundary colours. single boundary colour.
 Comparatively slower than the Boundary-fill  Faster than the Flood-fill algorithm.
algorithm.
 In Flood-fill algorithm, a random colour can be  In Boundary-fill algorithm, interior points
used to paint the interior portion then the old one is are painted by continuously searching for
replaced with a new one. the boundary colour.
 It requires huge amount of memory.  Memory consumption is relatively low

 Simple and efficient.  The complexity is high.

Two dimensional transformations

 Changes in orientation, size, and shape are accomplished with geometric transformations that
alter the coordinate descriptions of objects.
 The basic geometric transformations are translation, rotation, and scaling.
 Other transformations that are often applied to objects include reflection and shear.

Basic geometric transformations:


● Translation
● Rotation
● Scaling

Translation
● A translation is applied to an object by repositioning it along a straight-line path from one coordinate
location to another.
● We can translate a point in 2D by adding translation distances, (tx, ty) to the original coordinate
(X, Y) to move the point to a new position (X’, Y’).
x' = x + t x ,
y' = y + t y
 The translation distance pair (t x, t y) is called a translation vector or
shift vector.
UNIT 2-Filled Area Primitives

 Translation is a rigid-body transformation that moves objects without d eformation, i .e.,


every point on the object is translated by the same amount.
Rotation
● In rotation, we rotate the object at particular angle θ (theta) from its origin.
● To generate a rotation, we specify a rotation angle θ and the position (x r, yr) of the rotation point (or pivot
point) about which the object is to be rotated.
Using standard trigonometric
 The original coordinate of point P(X, Y) can be represented as −

In matrix form: P' = R(Ɵ) ∙ P

Where R(Ɵ) =

, anticlockwise rotation
The rotation angle can be positive or negative.
𝑃𝑜𝑠𝑖𝑡𝑖𝑣𝑒 , 𝑎𝑛𝑡𝑖𝑐𝑙𝑜𝑐𝑘𝑤𝑖𝑠𝑒 𝑟𝑜𝑡𝑎𝑡𝑖𝑜𝑛
𝜃={
𝑁𝑒𝑔𝑎𝑡𝑖𝑣𝑒 , 𝑐𝑙𝑜𝑐𝑘𝑤𝑖𝑠𝑒 𝑟𝑜𝑡𝑎𝑡𝑖𝑜𝑛

, clockwise rotation
Rotation about a fixed reference point
UNIT 2-Filled Area Primitives

● Rotations are rigid-body transformations that move objects without deformation.


● Every point on an object is rotated through the same angle.
Scaling
 A scaling transformation alters the size of an object.
 Let us assume that the original coordinates are (X, Y), the scaling factors are (SX, SY), and the produced
coordinates are (X’, Y’).

● Scaling factor Sx, scales objects in the x direction, while Sy scales in the y direction.

P' = S • P ,
The matrix represented form is

● If Sx & Sy >1, the size of the object will increase. Also objects move away from origin
● If Sx & Sy <1, the size of the object is reduced. Also objects move towards origin
● If Sx & Sy =1, the size of the object will remain unchanged. Also there will be no change in location.
● If Sx & Sy have same values, it is called uniform scaling.
● If Sx & Sy have different values, it is called differential scaling.

NB: Refer class note for worked out example.


UNIT 2-Filled Area Primitives

Homogeneous co-ordinates:
General transformation of a point:
P' = M1 • P + M2
P' and P - column matrix (2 x 1) of new and original coordinates
M1 - 2 x 2 matrix for scaling and rotation
M2 - column matrix (2 x 1) for translation
 For scaling or rotation, we set M2 as the additive identity.
 For translation, we set M1 is the multiplicative identity.

Homogeneous representation allows to represent all geometric transformation equations as concatenated matrix
multiplications
Any Cartesian point P(X, Y) can be converted to homogenous coordinates by P’ (Xh, Yh, h).
A point (x, y) can be re-written in homogeneous coordinates as (xh, yh, h).The homogeneous parameter h is a
non-zero values such that:
UNIT 2-Filled Area Primitives

Advantages of using homogenous coordinate:


we can perform all transformations using matrix/vector multiplications. This allows us to pre-multiply all the
matrices together. Combined transformation are easier to built and understand

Inverse transformations:

 Inverse Translation matrix

 Inverse Rotation

 Inverse Scaling matrix


UNIT 2-Filled Area Primitives

Other Transformations
Reflection
 Reflection is the mirror image of original object.
 In other words, we can say that it is a rotation operation with 180°.
 In reflection transformation, the size of the object does not change.
UNIT 2-Filled Area Primitives

Shear
A transformation that slants the shape of an object is called the shear transformation. It 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 .
There are two shear transformations X-Shear and Y-Shear. One shifts X coordinates values and other shifts Y
coordinate values. However; in both the cases only one coordinate changes its coordinates and other preserves
its values. Shearing is also termed as Skewing.
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes the vertical lines
to tilt right or left as shown in below figure.

The transformation matrix for X-Shear can be represented as −


X’ = X + Shx . Y
Y’ = Y
Matrix:

X shear relative to other reference line yref = -1


 Shifts the points in the y-direction proportional to the x-coordinate. But x-coordinate of points are
unaffected.
Eg. Square converted to a parallelogram
UNIT 2-Filled Area Primitives

Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates which causes the horizontal lines to
transform into lines which slopes up or down as shown in the following figure.

The Y-Shear can be represented in matrix from as −


Y' = Y + Shy . X
X’ = X
The shear matrix:

Y-direction shear relative to reference lines (x=xref )


Eg. Square converted to vertically shifted parallelogram
UNIT 2-Filled Area Primitives

Composite transformations
We can set up a matrix for any sequence of transformations as a composite transformation matrix by
calculating the matrix product of the individual transformations. Forming products of transformation matrices
is often referred to as a concatenation, or composition, of matrices.
Each successive transformation matrix pre-multiplies the product of the preceding transformation matrices.
If a transformation of the plane T1 is followed by a second plane transformation T2, then the result itself may
be represented by a single transformation T which is the composition of T1 and T2 .
Transformations can be any combination of
 Translation
 Scaling
 Shearing
 Rotation
 Reflection
The basic purpose of composing transformations is to gain efficiency by applying a single composed
transformation to a point, rather than applying a series of transformation, one after another.
Cases for composite transformation

Case -1:Two successive translations,


Case – 2:Two successive rotations,
Case – 3: Two successive scaling operations
1. Two successive translations are additive:
When two successive translations are applied to a point with translation vectors (tx1,ty1) and (tx2,ty2) , then

P' = T (t x2, t y2) . {T (t x1, t y1) . P}

= {T (t x2, t y2) . T (t x1, t y1)} . P}


UNIT 2-Filled Area Primitives

P’= {T2 (tx2, ty2).T1 (tx1, ty1)} . P


Thus we can conclude that two successive translations are additive

2. Two successive rotations are additive


When two successive rotations R1 (θ1) and R2 (θ2) are applied to a point P(x,y) in succession, with respect to
origin,

First R1 is applied on P as R1 (θ1). {P}

Then R2 is applied on the resultant position i.e. P’= { R2(θ2).R1(θ1) } . {P}

so that the final rotated coordinates of a point can be calculated with the composite rotation matrix as

P’ = R(θ1 + θ2) · P
Thus we can conclude that two successive rotations are additive

3. Two successive scalings are multiplicative

Let S1 (sx1,sy1) and S2 (sx2, sy2) be two rotations applied on a point P(x,y) in succession, with respect to origin.

First S1 is applied on P as S2 (sx1,sy1). {P}

Then S2 is applied on the resultant position i.e. P’= {S2 (sx2, sy2) . S1 (sx1,sy1) )}.{P}
UNIT 2-Filled Area Primitives

P’=S(sx2.sx1, sy2.sy1).{P}
Thus we can conclude that two successive scaling operations are multiplicative

Composite transformations ( Special Cases):


 Rotate about an arbitrary point – translate, rotate, translate
 Scale about an arbitrary point – translate, scale, translate
 Change coordinate systems – translate, rotate, scale
• But the order of operations important
EXAMPLE 1 : General Pivot Point Rotation:
● We can generate rotations about any selected pivot point (xr, yr) by performing the following
sequence of translate- rotate-translate operations:
1. Translate the object so that the pivot-point position is moved to the coordinate origin.
2. Rotate the object about the coordinate origin.
3. Translate the object so that the pivot point is returned to its original position.

P’= {T(xr, yr).R(θ).T(-xr,-yr)}.{P}


UNIT 2-Filled Area Primitives

EXAMPLE 2 : General fixed Point Scaling:


● Transformation sequence to produce scaling with respect to a selected fixed position (xf, yf) using a
scaling function that can only scale relative to the coordinate origin.
1. Translate object so that the fixed point coincides with the coordinate origin.
2. Scale the object with respect to the coordinate origin.
3. Use the inverse translation of step 1 to return the object to its original position.

P’= {T(xf,yf).S(sx,sy).T(-xf,-yf)}.{P}

Problems:
UNIT 2-Filled Area Primitives
UNIT 2-Filled Area Primitives

Three-Dimensional Geometric Transformation


3D computer graphics or three dimensional computer graphics are graphics that use a three- dimensional
representation of geometric data that is stored in the computer for the purpose of performing calculations
and rendering 2D images.

3D transformation is more complex than 2D transformation.


 3D Geometric Transformation
A 3D point P is represented in homogeneous coordinates by a 4-dimensional vector:

Types of 3D transformation:
UNIT 2-Filled Area Primitives

1. 3D Translation

 Moving a coordinate position with translation vector T = (tx , ty , tz )

 Shifting the position of a three-dimensional object using translation vector T.

An inverse of a three-dimensional translation matrix is obtained by negating the translation distances tx,
ty, and tz.

2. Three-Dimensional Rotation

 Positive rotations about a coordinate axis are counterclockwise.


UNIT 2-Filled Area Primitives

Rotation changes the orientation of an object around a specified axis (X, Y, or Z).

For rotation about x axis, x is unchanged.


UNIT 2-Filled Area Primitives

For rotation about y axis, y is unchanged.

3-D ROTATION TRANSFORMATION MATRIX


UNIT 2-Filled Area Primitives

#General 3D rotation (Rotation about any coordinate axis)


 Parallel to any of the co-axis:
When an object is to be rotated about an axis that is parallel to one of the co-ordinate axis, we need to
perform some series of transformation.
1. Translate the object so that the rotation axis coincides with the parallel co-ordinate axis. T(-a,-b,-c)
where, (a, b, c) is any point on the rotation axis.
2. Performed the specified rotation about the axis. Rx(θ )
3. Translate the object so that the rotation axis is moved to its original position. T(a, b, c).

Net transformation= T(a, b, c) . Rx(θ) . T(-a,-b,-c)

 Not parallel to any of the co-axis:


When an object is to be rotated about an axis that is not parallel to one of the co-ordinate axes, we need
to perform some series of transformation.

3. 3D Scaling

 Scaling changes the size of an object and involves the scale factors.

 The scaling parameters S x , S y and S z are assigned any positive values.


UNIT 2-Filled Area Primitives

 Scales the object about the origin, Here changes the size of the object along x,y and z-coordinate is
same

You might also like