CG PPT 2
CG PPT 2
Polygons, 2D Transformations
Introduction to Polygon
● “A Polygon can be described as the enclosed collection or group of the lines.”
● In a polygon, all lines are connected.
● Lines can be a combination of edges and vertices, which together form a polygon.
● A polygon refers to a two-dimensional architecture made up of a number of straight lines.
● There are no edges in circle
● So circle is not polygon
● Some Examples of the polygon:
○ Triangles
○ Pentagons
○ Hexagons
○ Quadrilaterals
● The polygon’s name defines how many sides the architecture contains.
Introduction to Polygon
● Types of Polygon
○ There are three basic types of polygon-
○ Concave Polygon:
■ At least one vertex points towards the interior
of the polygon
■ at least one angle should be greater than
180° (angle >180°)
■ At least one pair of sides when produced,
enter inside and intersect any side of the
polygon
4
Types of polygon
○ Convex Polygon:
■ All vertex points outwards
■ all the angles should be less than 180°
(angle<180°)
■ The sides when produced do not enter the
polygon
○ Complex Polygon:
■ a polygon which is neither convex nor
concave is referred as complex polygons.
■ The complex polygon includes any polygon
which intersects itself of the polygon which
overlaps itself.
■ The complex polygon has a boundary.
Types of polygon
● Boundary-fill Algorithm:
○ It is also known as the “Edge-fill algorithm.”
○ The boundary fill algorithm is used for area filling.
○ We can perform boundary fill where we want to
create an attractive painting.
○ In this, we can easily select the interior points.
○ If the object has a particular boundary in a single
color, then the algorithm travels each pixel until it
reaches the boundary.
Polygon Filling
○ In the Boundary-fill algorithm, we use the 4-connected and 8-connected methods.
○ By the use of a 4-connected or 8-connected method, we can set the new position of the pixels
until all the interior points have been filled.
Polygon Filling
○ In the Boundary-fill algorithm, we use
the 4-connected and 8-connected methods.
○ By the use of a 4-connected or 8-connected method, we can set
the new position of the pixels until all the interior points have been
filled.
Polygon Filling
○ 4 connected: Example
19
Polygon Filling
20
Polygon Filling
● Algorithm of Boundary-fill- 4 connected
Procedure boundary fill (x, y, fill color, boundary)
Step 1: Initialize the boundary of the region.
Step 2: Get the interior pixel (x, y). Now, define an Integer called
current pixel and assign it to (x, y).
Current = getpixel (x, y)
Step 3: If
(current pixel != boundary) and (current pixel != fill)
Then
Setpixel(x, y, fill);
Boundary fill (x+1, y, fill, boundary);
Boundary fill (x-1, y, fill, boundary);
Boundary fill (x, y+1, fill, boundary);
Boundary fill (x, y-1, fill, boundary);
Step 4: End; 21
22
Polygon Filling
23
Polygon Filling
24
Polygon Filling
25
Polygon Filling
26
Polygon Filling
● Algorithm of Boundary-fill- 8 connected
Procedure boundary fill (x, y, fill color, boundary)
Step 1: Initialize the boundary of the region.
Step 2: Get the interior pixel (x, y). Now, define an
Integer called current pixel and assign it to (x, y).
Current = getpixel (x, y)
Step 3: If
(current pixel != boundary) and (current pixel != fill)
Then
Setpixel(x, y, fill);
Boundary fill (x+1, y, fill, boundary);
Boundary fill (x-1, y, fill, boundary);
Boundary fill (x, y+1, fill, boundary); 27
Boundary fill (x, y-1, fill, boundary);
Boundary fill (x+1, y+1, fill, boundary);
Boundary fill (x-1, y+1, fill, boundary);
Boundary fill (x-1, y-1, fill, boundary);
Boundary fill (x+1, y-1, fill, boundary);
Step 4: End;
Polygon Filling
● Flood-fill Algorithm:
○ Flood-fill
algorithm helps to define a region in the
boundary, attached to a point in the multi-dimensional
array.
○ It is similar to the bucket tool used in the paint program.
○ The stack-based recursive function is used to implement
the algorithm.
○ In flood -fill algorithm, we replace all the associated pixels
of the selected color with a fill color.
○ We also check the pixels for a particular interior color, not
for boundary color.
Polygon Filling
● Algorithm of Flood-fill
Procedure flood_fill (p, q, fill_color, Old_color: Integer)
Var
Current: Integer
Current = getpixel (p, q)
If
(Current = Old_color)
Then
Start
setpixel (p, q, fill_color);
flood_fill (p, q+1, fill_color, Old_color);
flood_fill (p, q-1, fill_color, Old_color);
flood_fill (p+1, q, fill_color, Old_color);
flood_fill (p-1, q, fill_color, Old_color);
End;
Polygon Filling
● Scan-Line Algorithm
○ The Scan-Line Algorithm is an area filling
algorithm.
○ In this algorithm, we can fill the polygons
through horizontal lines or scan lines.
○ The scan-line intersects the edges of the
polygon, and the polygon is filled between
pairs of the intersection.
○ The main purpose of this algorithm is to fill
color in the interior pixels of the polygon.
2-D Transformations
Introduction
● The term Transformation is generally referred to as converting a graphic into another graphic
by applying some rules or algorithms.
● Sometimes an image or picture can be a combination of lines, rectangle, circle, and triangle.
● If we draw the basic and combination of pictures, then there should be a need to transform
these images.
● Now we can perform the following actions to transform the images-
○ We can change the position of an image.
○ We can increase or decrease the size of an image.
○ We can change the angle of the image.
● By using the above actions, we will find a new image; this process is called Transformation.
● We can use some algorithms to produce new pictures.
Introduction
● Types of Transformation
2D Transformations
● Two-Dimensional(2D) Transformation:
○ “When we translate, rotate, and scale object in the two-dimensional plane, then it is called
a Two-Dimensional Transformation.”
○ A two-dimensional plane consists of the x and y-axis.
○ The Two-Dimensional Transformation includes-
■ 2D Translation: “Translation is a mechanism used to move the object from one position
to another position on the screen.”
■ 2D Rotation: “Rotation is a process used to rotate the object from origin to a particular
angle.”
■ 2D Scaling: “Scaling is a process or technique used to resize the object in
two-dimensional plane.”
■ 2D Reflection: “Reflection is a mechanism or process in which we can rotate the object at
the angle of 180°”.
■ 2D Shearing: “Shearing is a process that is used to perform slanting on the object.” It is
also called “Skewing.”
2D Translation
● In Computer graphics, 2D Translation is a process of moving an object from one position to another in a two dimensional plane.
● Consider a point object O has to be moved from one position to another in a 2D plane.
Let-
This translation is achieved by adding the translation coordinates to the old coordinates of the object as-
Let-
Solution-
Given-
● Old corner coordinates of the square = A (0, 3), B(3, 3), C(3, 0), D(0, 0)
● Scaling factor along X axis = 2
● Scaling factor along Y axis = 3
2D Scaling Example continued…
2D Reflection
● Reflection is a kind of rotation where the angle of rotation is 180 degree.
● The reflected object is always formed on the other side of mirror.
● The size of reflected object is same as the size of original object
Types of Reflection:
1. Reflection about the x-axis
2. Reflection about the y-axis
3. Reflection about an axis perpendicular to xy plane and passing through the origin
4. Reflection about line y=x
2D Reflection For homogeneous coordinates, the above reflection matrix may be
represented as a 3 x 3 matrix
● Reflection is a kind of rotation where the angle of
rotation is 180 degree.
● The reflected object is always formed on the other
side of mirror.
● The size of reflected object is same as the size of
original object.
Reflection On X-Axis:
This reflection is achieved by using the following reflection
equations-
● Xnew = Xold
● Ynew = -Yold
● Xnew = -Xold
● Ynew = Yold
In this value of x and y both will be reversed. This is also called as half revolution about the origin.
2D Reflection
Reflection about line y=x:
Given- Let the new coordinates of corner C after reflection = (Xnew, Ynew).
● Old corner coordinates of the triangle = A (3, 4), B(6, 4), Applying the reflection equations, we have-Xnew = Xold = 5
C(5, 6)
● Reflection has to be taken on the X axis ● Ynew = -Yold = -6