CG Unit 2 Notes
CG Unit 2 Notes
Unit – II
Polygon: Windowing & Clipping
1. Polygons
Polygons are fundamental shapes in geometry and computer graphics, defined as a flat, two-
dimensional shape with straight sides that are fully connected, forming a closed loop. Polygons
can have any number of sides, but they must have at least three.
1. Vertices: The points where the sides of the polygon meet are called vertices.
2. Edges: The straight lines connecting the vertices are called edges or sides.
3. Interior Angles: The angles inside the polygon formed by the edges are called interior
angles.
1. Convex Polygons:
A convex polygon is a polygon in which all interior angles are less than 180 degrees. This
implies that if you draw a line between any two points inside the polygon, the line will never
exit the polygon. Essentially, a convex polygon has no indentations or "inward" curves.
2|Page
Any line segment drawn between two points inside the polygon lies entirely within the
polygon.
Examples include regular polygons like equilateral triangles, squares, and regular
hexagons.
2. Concave Polygons:
A concave polygon is a polygon that has at least one interior angle greater than 180 degrees.
This means that the polygon has at least one "indentation" or inward curve. In other words, a
concave polygon will have at least one line segment that, when drawn between two points
inside the polygon, will pass outside of the polygon.
There exists at least one line segment that can be drawn between two points inside the
polygon that passes outside of it.
The Even-Odd Method and the Winding Number Method are two algorithms used to
determine whether a given point lies inside or outside a polygon.
1. Even-Odd Method:
The Even-Odd Method, also known as the Ray Casting Method, is based on counting how
many times a ray extending from the point in question, intersects the edges of the polygon.
How It Works:
Count the number of times this ray intersects the edges of the polygon.
In the figure, the line segment from ‘A’ passes single edge and 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.
We must look at the other end points of the two segments of a polygon which meet at this
vertex. If these points lie on the same side of the constructed line AP, then the intersection point
counts as an even number of intersections. But if they lie on the opposite side of constructed
line AP, then the intersection points count as a single intersection.
4|Page
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.
There is another alternative method for defining a polygons’ interior point is called the winding
number method. Conceptually, one can stretch a piece of elastic between the point (P) to be
checked and the point on the polygon boundary.
How It Works:
For each edge of the polygon, determine how it "winds" around the point:
o If the edge starts below the line segment and finishes above, the edge has
direction number ‘−1’
o If the edge starts from above the line segment & finishes below the line, the
edge has direction number ‘1’.
Consider that an elastic is tied to point (P) to be checked, and the other end of the elastic is
sliding along the boundary of the polygon until it has made one complete circuit. Then we
check how many times the elastic has been wound around the point of intersection. If it is
wound at-least once, point is inside. If there is no net winding then point is outside.
In this method, instead of just counting the intersections, we give each boundary line crossed
a direction number, and we sum these directions numbers. The direction number indicates the
direction of the polygon edge that was drawn relative to the line segment we constructed for
the test.
Example: To test a point (xi, yi), let us consider a horizontal line segment y = yi which runs
from outside the polygon to (xi, yi). We find all the sides which crossed this line segment.
Now there are 2 ways for side to cross, the side could be drawn starting below end, cross it and
end above the line. In this case we can give direction numbers – 1, to the side or the edge could
start above the line & finish below it in this case, given a direction 1. The sum of the direction
numbers for the sides that cross the constructed horizontal line segment yield the “Winding
Number” for the point.
If the winding number is non-zero, the point is inside the polygon, else, the point is outside the
polygon.
In the above figure, the line segment crosses 4 edges having different direction numbers:
So, the point P is outside the Polygon. The edge has direction number −1 because it starts below
the line segment & finishes above. Similarly, edge has direction number +1 because it starts
from above the line segment & finishes below the line segment (See the directions in the
figure).
2. Polygon Filling
2.1 Flood fill / Seed fill
Flood Fill is a classic algorithm used in computer graphics for filling an area bounded by a
polygon or another region with a specific color. It is commonly used in paint programs and
graphic editors for operations like filling enclosed areas with a solid color or pattern.
Flood Fill works by starting from a seed point (a pixel inside the area to be filled) and then
spreading out to fill neighboring pixels that meet a specific criterion (such as having the same
color as the seed point).
o This method checks the four neighboring pixels (up, down, left, right) of the
current pixel.
o The algorithm recursively or iteratively fills the neighboring pixels if they
match the target color (the color to be replaced).
o This approach is simple but might leave gaps if the region to be filled has
diagonal connections.
Steps:
7|Page
o This method is similar to the 4-connected method but additionally checks the
diagonal neighbors.
Steps:
2.1.3 Advantages of 8-Connected Flood Fill over the 4-Connected Flood Fill
More Complete Fill for Irregular Shapes: This method is better suited for filling irregular
shapes, especially those with diagonal components, ensuring that no gaps are left unfilled.
Better Handling of Thin Lines and Corners: The 8-Connected Method can accurately fill
thin lines and sharp corners, which might be missed or only partially filled by the 4-Connected
Method.
Natural and Accurate Fill Appearance: The fill produced by the 8-Connected Method tends
to look more natural and complete, especially in complex regions, leading to a smoother and
more accurate representation of the filled area.
8|Page
Example:
Imagine filling a region using the 4-Connected Method, the algorithm would only fill the
central pixels, leaving the diagonally connected parts unfilled as shown in the figure below.
The 8-Connected Method, however, would fill the entire shape, as it takes diagonal connections
into account as well.
Boundary Fill is a polygon filling algorithm used in computer graphics to fill a region defined
by a specific boundary. The algorithm starts from a seed point inside the region and spreads
outwards, filling pixels until it reaches the boundary. The boundary is typically defined by a
specific color, and the algorithm stops filling when it encounters this boundary color.
1. Seed Point: The algorithm begins at a seed point, which is a pixel inside the area to be
filled.
2. Filling Process:
o The algorithm checks the color of the current pixel. If the pixel is not the
boundary color and not the fill color, it changes the pixel to the fill color.
3. Boundary Detection:
9|Page
o The filling process continues until the algorithm encounters the boundary color,
at which point it stops filling in that direction.
o This method checks and fills the four neighboring pixels (left, right, up, down)
of the current pixel.
Steps:
o This method checks and fills the eight neighboring pixels (left, right, up, down,
and the four diagonals).
o This approach ensures that all areas within the boundary, including diagonally
connected regions, are filled.
Steps:
Example: Imagine you have a region bounded by a black line, and the area inside this boundary
is white. You want to fill this white area with red.
10 | P a g e
The 4-connected method fails to fill this figure completely. This figure will be efficiently filled
using the 8-connected technique.
Scan-Line Fill is a widely used polygon filling algorithm in computer graphics, especially for
rendering filled polygons on raster displays. It operates by processing the polygon one
horizontal line (or scan line) at a time, determining which parts of the scan line lie inside the
polygon, and then filling those parts with the desired color.
11 | P a g e
The algorithm works by considering the intersections of the polygon's edges with each scan
line. Here's the step-by-step process:
1. Initialization:
o Determine the bounding box of the polygon (i.e., the minimum and maximum
x and y coordinates that enclose the polygon).
o Start with the lowest y-coordinate of the bounding box and process each scan
line up to the highest y-coordinate.
2. Edge Intersections:
o For each scan line, determine the intersection points between the scan line and
the edges of the polygon.
o This is typically done by solving the line equation for each edge of the polygon
to find where the scan line intersects the edge.
3. Sort Intersections:
o The intersection points on the scan line are sorted by their x-coordinates.
o After sorting, the algorithm fills the pixels between pairs of intersection points.
o If there are multiple pairs of intersections, alternate between filling and not
filling the sections, because only the even-numbered intervals (inside the
polygon) need to be filled.
o Shared vertices or edges that lie on the scan line should only be considered once.
Step 1: Find out the ymin and ymax from the given polygon.
Step 2: Scan Line intersects with each edge of the polygon from ymin to ymax. Name each
intersection point of the polygon. As per the figure shown above, they are named as p0, p1, p2,
p3 .
Step 3: Sort the intersection point in the increasing order of x coordinate i.e. (p0, p1), (p1, p2)
and (p2, p3).
Step 4: Fill all those pairs of coordinates that are inside polygons and ignore the alternate pairs.
Viewing transformations are a set of operations in computer graphics that map a scene from
world coordinates to a device's screen coordinates. These transformations define how a 3D
scene is projected onto a 2D viewing surface (like a computer screen), allowing us to view a
3D object from different perspectives. The process involves several key steps:
1. Modeling Transformation:
2. Viewing Transformation:
o Transforms the world coordinates to viewing coordinates. This aligns the scene
with the viewer's position and orientation.
3. Projection Transformation:
13 | P a g e
o Removes parts of the scene outside the view volume and normalizes the
coordinates within a standard range.
5. Viewport Transformation:
o Maps the normalized coordinates to the device's viewport, where they are finally
displayed.
General Terms:
World Coordinate: It is the Cartesian coordinate w.r.t which we define the diagram,
like xwmin, xwmax, ywmin, ywmax.
Device Coordinate: It is the screen coordinate where the objects are to be displayed,
like xvmin, xvmax, yvmin, yvmax.
3.2 2D Clipping
2D Clipping is a process in computer graphics that involves restricting the rendering of objects
or parts of objects (like lines, polygons, or text) to a specific rectangular region known as the
clipping window or clipping rectangle.
14 | P a g e
Any part of an object outside this clipping window is "clipped" away, meaning it is not
displayed.
The purpose of 2D clipping is to ensure that only the visible portions of objects are drawn on
the screen, which optimizes rendering and maintains focus on the relevant part of the scene.
Description:
In this algorithm, we are given 9 regions on the screen. Out of which one region is of the
window and the rest 8 regions are around it given by 4-digit binary. The division of the regions
are based on (xmax, ymax) and (xmin, ymin).
The central part is the viewing region or window, all the lines which lie within this region are
completely visible. A region code is always assigned to the endpoints of the given line.
Formula to check binary digits: TBRL which can be defined as top, bottom, right, and left
accordingly.
15 | P a g e
1. Assign Outcodes:
If both outcodes are 0000, the line segment is completely inside the clipping window,
and the line can be accepted without further processing.
If the logical AND operation between the two outcodes is not 0000, the line segment
is entirely outside the clipping window, and the line can be rejected.
If the line cannot be trivially accepted or rejected, the line intersects the clipping
window, and further processing is needed to find the intersection points.
Determine which bit is set in the outcode of the point outside the clipping window and
calculate the intersection of the line with the corresponding clipping boundary.
Replace the outside endpoint with the intersection point and update its outcode.
Repeat the process until the line is either trivially accepted or rejected.
16 | P a g e
Example: Let ABCD be the rectangular window with A(20, 20), B(90, 20), C(90, 70), and
D(20, 70). Find region codes for endpoints and use the Cohen-Sutherland algorithm to clip
the lines: (i) P1 P2 with P1 (10, 30), P 2 (80, 90).
P1 = 0001, P2 = 1000
Slope, 𝑚 = = = =
y = m(x − x1) + y1
y = m(xmin − x1) + y1
y = 38.57 = 39
1
𝑥= (𝑦 − 𝑦 ) + 𝑥
𝑚
17 | P a g e
1
𝑥= (𝑦 −𝑦 )+𝑥
𝑚
1 40
𝑥= (70 − 30) + 10 = + 10
0.857 0.857
𝑥 = 56.67
The algorithm processes the polygon vertices by clipping the polygon against each of the four
clipping window edges (left, right, bottom, and top) one by one. After processing against one
edge, the resulting polygon is used as input for the next edge.
There are four possible cases for any given edge of given polygon against current clipping
edge.
1. Both vertices are inside: Only the second vertex is added to the output list.
2. First vertex is outside while second one is inside: Both the point of intersection of
the edge with the clip boundary and the second vertex are added to the output list.
18 | P a g e
3. First vertex is inside while second one is outside: Only the point of intersection of
the edge with the clip boundary is added to the output list.
4. Both vertices are outside: No vertices are added to the output list.
Example: Let us consider a polygon and the clipping window as shown in figure ‘Before
Clipping’.
The vertices of the original polygon are V1, V2, V3, V4, V5. After clipping each boundary, the
new vertices are given in figure ‘After Clipping’.
After Top Clipping: V1', V2', V3, V3', V4' V4, V5, V1
After Bottom Clipping: V1', V2', V3, V3', V4' V4, V5, V5' V1
The Weiler-Atherton Polygon Clipping Algorithm is a versatile and robust algorithm used
for clipping polygons against a convex or concave clipping window.
Unlike simpler polygon clipping algorithms like Sutherland-Hodgman, which are limited to
convex clipping windows, the Weiler-Atherton algorithm can handle both convex and concave
clipping regions.
When the clipped polygons have two or more separate sections, then it is the concave polygon
handled by this algorithm. The vertex-processing procedures for window boundaries are
modified so that concave polygon is displayed.
19 | P a g e
Let the clipping window be initially called clip polygon and the polygon to be clipped the
subject polygon. We start with an arbitrary vertex of the subject polygon and trace around its
border in the clockwise direction until an intersection with the clip polygon is encountered:
1. If the edge enters the clip polygon, record the intersection point and continue to trace the
subject polygon.
2. If the edge leaves the clip polygon, record the intersection point and make a right turn to
follow the clip polygon in the same manner (i.e., treat the clip polygon as subject polygon and
the subject polygon as clip polygon and proceed as before).
Whenever our path of traversal forms a sub-polygon, we output the sub-polygon as part of the
overall result. We then continue to trace the rest of the original subject polygon from a recorded
intersection point that marks the beginning of a not-yet traced edge or portion of an edge. The
algorithm terminates when the entire border of the original subject polygon has been traced
exactly once.
Example:
20 | P a g e
The number in fig (a) indicates the order in which the edges and portion of edges are traced.
We begin at the starting vertex and continue along the same edge (from 1 to 2) of the subject
polygon as it enters the clip polygon.
As we move along the edge that is leaving the clip polygon, we make a right turn (from 4 to
5) onto the clip polygon, which is now considered the subject polygon.
Following the same logic leads to the next right turn (from 5 to 6) onto the current clip
polygon, this is the original subject polygon.
With the next step done (from 7 to 8) in the same way, we have a sub-polygon for output in
fig (b).
We then resume our traversal of the original subject polygon from the recorded intersection
point where we first changed our course.
Going from 9 to 10 to 11 produces no output.
After skipping the already traversed 6 and 7, we continue with 12 and 13 and come to an end.
The fig (b) is the final result.
o The algorithm becomes less efficient when dealing with a very large number of
line segments, especially if many segments are partially outside the clipping
window. Each partially outside segment requires intersection calculations,
which can increase computational overhead.
o In cases where the line segment is very close to the boundaries of the clipping
window but extends outside on both ends, the algorithm may need to perform
multiple intersection calculations. This can make the algorithm less efficient in
such edge cases compared to algorithms like Liang-Barsky, which can handle
such cases more efficiently using parametric line equations.
o The algorithm might face challenges with degenerate cases, such as when the
line segment is reduced to a single point, or when both endpoints of the line
segment coincide with the clipping boundary.
Region Codes (or Outcodes) are a critical component of the Cohen-Sutherland algorithm.
These are 4-bit binary codes assigned to each endpoint of the line segment based on its position
22 | P a g e
relative to the clipping window. The significance of region codes lies in their role in efficiently
determining whether a line segment is inside, outside, or partially within the clipping window.
1. Division of Space:
o The clipping space is divided into nine regions based on the clipping window:
inside, left, right, above, below, and the four corner regions (top-left, top-right,
bottom-left, bottom-right).
2. Bitwise Representation:
o Each bit in the 4-bit code represents a position relative to the clipping window:
o For example:
A point to the left and above the window has a region code of 1001.
o If both endpoints of the line segment have a region code of 0000 (indicating that
both points are inside the clipping window), the line segment can be trivially
accepted without further processing.
o If the bitwise logical AND of the region codes of the two endpoints is non-zero
(indicating that both endpoints share at least one region outside the clipping
window), the line segment can be trivially rejected.
4. Efficient Clipping:
o The algorithm uses the region codes to determine which boundary of the
clipping window to intersect with first when clipping partially visible line
23 | P a g e
o Region codes help in handling edge cases efficiently, such as when one endpoint
of the line is inside the window and the other is outside. The region code guides
the algorithm in finding the correct intersection point and updating the line
segment accordingly.