0% found this document useful (0 votes)
8 views82 pages

CG PPT 2

The document provides an overview of polygons and 2D transformations, defining polygons as enclosed collections of lines and categorizing them into types such as concave, convex, and complex. It discusses methods for determining if a point lies inside a polygon, including the Even-Odd and Winding Number methods, as well as algorithms for polygon filling like Seed Fill and Scan-Line. Additionally, it covers 2D transformations including translation, rotation, scaling, reflection, and shearing, with examples and mathematical representations for each transformation type.

Uploaded by

blackcrusher7
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)
8 views82 pages

CG PPT 2

The document provides an overview of polygons and 2D transformations, defining polygons as enclosed collections of lines and categorizing them into types such as concave, convex, and complex. It discusses methods for determining if a point lies inside a polygon, including the Even-Odd and Winding Number methods, as well as algorithms for polygon filling like Seed Fill and Scan-Line. Additionally, it covers 2D transformations including translation, rotation, scaling, reflection, and shearing, with examples and mathematical representations for each transformation type.

Uploaded by

blackcrusher7
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/ 82

Unit- II

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

● Triangle: It has three sides. • Hexagon: It contains six sides.

● Pentagon: It has five sides.


• Quadrilaterals: It has four sides.
Types of 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

By Mrs. Pallavi Yevale, AI&DS,


6
DYPIEMR, Pune
Inside Test
● Inside Test Methods:
○ To determine a point lies inside a polygon or not there two methods :
■ Even-Odd method:
■ Winding number Method-Inside :
Inside Test

● Even odd method:


○ Constructing a line segment between the point (P)
to be examined and a known point outside the
polygon is the one way to determine a point lies
inside a polygon or not.
○ The number of times the line segment intersects
the polygon boundary is then counted.
○ The point (P) is an internal point if the number of
polygon edges intersected by this line is odd;
otherwise, the point is an external point.
○ In the figure, the line segment from ‘A’ crosses
single edge & hence point A is inside the polygon.
○ The point B is also inside the polygon as the line
segment from B crosses three (odd) edges.
○ But point C is outside the polygon it as the line
segment from C crosses two (even) edges.
○ But this even-odd test fails when the intersection
point is a vertex.
Inside Test

○ look at the other end points of the two segments of a polygon


which meet at this vertex.
○ If these points lies on the same side of the constructed line
A’P’, then the intersection point counts as an even number of
intersection.
○ But if they lie on the opposite side of constructed line AP, then
the intersection points counts as a single intersection.
○ As we can see the line segment A’P’ intersects at M which is
a vertex and L & Z are the other end points of the two
segments meeting at M.
○ L & Z lie on same side of the line segment A’P’, so the count
is taken as even.
Inside Test
Inside Test

● Winding Number Method :


○ Another method of finding whether a point is inside or outside
of the polygon.
○ In this every point has a winding number, and the interior
points of a two-dimensional object are defined to be those that
have a nonzero value for the winding number.
1. Give the directions to each edge in
clockwise/anticlockwise direction
2. if the edge is moving upward give the 1 and -1 for
downward edge/ or exactly oppositie
3. Initializing the winding number to 0.
4. Consider the point which we have to find that it is
inside the polygon or outside.
5. Draw the line from that point to the known outside point
6. Add the crossing edges number(1 or -1) to the winding
number
7. After addition if the number is nonzero then the point is
inside the polygon else it is out side the polygon.
Inside Test

● the line segment crosses 4 edges


having different direction numbers : 1,
-1, 1& -1 respectively, then :
Winding Number = 1 + (-1) + 1 + (-1)
=0
So the point P is outside the Polygon.
Inside Test
Polygon Filling
● Area filling is a method or process that helps us to fill an object, area,
or image.
● We can easily fill the polygon.
● The polygon filling is defined as filling or highlighting all the pixels.
● The pixels appear inside the polygon shape with any color other than
the background color.
● There are two algorithms or methods used to fill the polygon.
○ Seed Fill Algorithm
○ Scan Line Algorithm
Polygon Filling

● Seed Fill Algorithm:


○ In this method, we will select a seed or starting point inside the boundary. We can further
divide the seed fill into two parts.
■ Flood-fill Algorithm
■ Boundary-fill Algorithm
Polygon Filling

● 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

● Note- Getpixel () defines the color of specified pixel.


Setpixel () set the pixel with the specified color.
○ In the 4-connected method, it does not fill corner pixels. Because it only checks the adjacent position of the
pixel.
○ Solution Is 8- connected
Polygon Filling
○ 8 connected: Example

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-

● Initial coordinates of the object O = (Xold, Yold)


● New coordinates of the object O after translation = (Xnew, Ynew)
● Translation vector or Shift vector = (Tx, Ty)

Given a Translation vector (Tx, Ty)-

● Tx defines the distance the Xold coordinate has to be moved.


● Ty defines the distance the Yold coordinate has to be moved.

This translation is achieved by adding the translation coordinates to the old coordinates of the object as-

● Xnew = Xold + Tx (This denotes translation towards X axis)


● Ynew = Yold + Ty (This denotes translation towards Y axis)

Matrix form, the above translation equations may be represented as-


● The homogeneous coordinates representation of (X, Y) is (X, Y, 1).
● Through this representation, all the transformations can be performed using matrix / vector multiplications.

The above translation matrix may be represented as a 3 x 3 matrix as


2D Translation

○ Example– Given a Point with


coordinates (2, 4). Apply the
translation with distance 4
towards x-axis and 2 towards
the y-axis. Find the new
coordinates without changing
the radius?
○ Solution: P = (x0, y0) = (2,4)
■ Shift Vector = (Tx,Ty) = (4,
2)
■ Let us assume the new
coordinates of P = (x1, y1)
■ Now we are going to add
translation vector and
given coordinates, then
■ x1 = x0 + Tx = (2 + 4) = 6
■ y1 = y0 + Ty = (4 + 2) = 6
■ Thus, the new coordinates
= (6,6)
2D Rotation
• The Rotation of any object depends upon the two points.
• Rotation Point: It is also called the Pivot point.
• Rotation Angle: It is denoted by Theta (θ).
• We can rotate an object in two ways-
• Clockwise: An object rotates clockwise if the value of the Rotation angle is negative (-).
• Anti-Clockwise: An object rotates anti-clockwise if the value of the Rotation angle is
positive (+).
• We can apply Rotation on following objects-
• Straight Lines: Straight Line is rotated by the endpoints with the same angle and
redrawing the line between new endpoints.
• Curved Lines: Curved Lines are rotated by repositioning of all points and drawing of
the curve at new positions.
• Polygon: Polygon is rotated by shifting every vertex using the same rotational angle.
• Circle: It can be obtained by center position by the specified angle.
2D Rotation
2D Rotation
This rotation is achieved by using the following rotation equations-
● Xnew = Xold x cosθ – Yold x sinθ
● Ynew = Xold x sinθ + Yold x cosθ

In Matrix form, the above rotation equations may be represented as-


2D Rotation
For homogeneous coordinates, the above rotation matrix may be represented as a 3 x 3 matrix as-
2D Rotation Example 1
Problem: Given a line segment with starting point
as (0, 0) and ending point as (4, 4). Apply 30 degree
rotation anticlockwise direction on the line segment
and find out the new coordinates of the line.
Solution:
Given-
● Old ending coordinates of the line = (Xold,
Yold) = (4, 4)
● Rotation angle = θ = 30º

Let new ending coordinates of the line after rotation


= (Xnew, Ynew).
Example 1 Continued..
Thus, New ending coordinates of the line after rotation = (1.46, 5.46).
2D Rotation Example 2
Problem: Given a triangle with
corner coordinates (0, 0), (1, 0) and (1,
1). Rotate the triangle by 90 degree
anticlockwise direction and find out
the new coordinates.
Solution:
Given-
● Old corner coordinates of the
triangle = A (0, 0), B(1, 0),
C(1, 1)
● Rotation angle = θ = 90º
Example 2 Continued..
Thus, New ending coordinates of the triangle after rotation = (0,0),(0,1),(-1,1)
2D Scaling
In computer graphics, scaling is a process of modifying or altering the size of objects.

● Scaling may be used to increase or reduce the size of object.


● Scaling subjects the coordinate points of the original object to change.
● Scaling factor determines whether the object size is to be increased or reduced.
● If scaling factor > 1, then the object size is increased.
● If scaling factor < 1, then the object size is reduced.

Consider a point object O has to be scaled in a 2D plane.

Let-

● Initial coordinates of the object O = (Xold, Yold)


● Scaling factor for X-axis = Sx
● Scaling factor for Y-axis = Sy
● New coordinates of the object O after scaling = (Xnew, Ynew)
2D Scaling For Homogeneous coordinates, the above scaling
This scaling is achieved by using the following matrix may be represented as a 3 x 3 matrix as-
scaling equations-
● Xnew = Xold x Sx
● Ynew = Yold x Sy
In Matrix form, the above scaling equations
may be represented as-
2D Scaling Example
Problem-01:
Given a square object with coordinate points A(0, 3), B(3, 3), C(3, 0), D(0, 0). Apply the scaling
parameter 2 towards X axis and 3 towards Y axis and obtain the new coordinates of the object.

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

In Matrix form, the above reflection equations may be


represented as-
2D Reflection For homogeneous coordinates, the above reflection matrix may be
represented as a 3 x 3 matrix as-
Reflection On Y-Axis:

This reflection is achieved by using the following reflection


equations-

● Xnew = -Xold
● Ynew = Yold

In Matrix form, the above reflection equations may be


represented as-
2D Reflection
Reflection about an axis perpendicular to xy plane and passing through origin:

In the matrix of this transformation is given below

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:

The object may be reflected about line y = x with the


help of following transformation matrix

First of all, the object is rotated at 45°. The direction of


rotation is clockwise. After it reflection is done concerning
x-axis. The last step is the rotation of y=x back to its original
position that is counterclockwise at 45°.
PRACTICE PROBLEMS BASED ON 2D REFLECTION

Problem-01: ● Xnew = Xold = 6


● Ynew = -Yold = -4
Given a triangle with coordinate points A(3, 4), B(6, 4), C(5, 6).
Apply the reflection on the X axis and obtain the new coordinates of Thus, New coordinates of corner B after reflection = (6, -4).
the object.
Solution- For Coordinates C(5, 6)

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

Thus, New coordinates of corner C after reflection = (5, -6).


For Coordinates A(3, 4)
Thus, New coordinates of the triangle after reflection = A (3, -4), B(6, -4),
Let the new coordinates of corner A after reflection = (Xnew, Ynew).
C(5, -6).
Applying the reflection equations, we have-
● Xnew = Xold = 3
● Ynew = -Yold = -4
Thus, New coordinates of corner A after reflection = (3, -4).

For Coordinates B(6, 4)


Let the new coordinates of corner B after reflection = (Xnew, Ynew).
Applying the reflection equations, we have-
2D Reflection Example Continued..
2D Shearing

You might also like