CG Unit 2 Sol
CG Unit 2 Sol
Types of Polygons:
1. Regular Polygon: All sides and angles are equal. Examples include squares, equilateral
triangles, and regular hexagons.
2. Irregular Polygon: Sides and angles are not necessarily equal. For instance, a scalene
triangle.
3. Convex Polygon: All internal angles are less than 180°. A rectangle is an example.
4. Concave Polygon: At least one internal angle is more than 180°. Think of a shape that
appears "dented" inward.
6. Complex Polygon: Sides intersect each other beyond just at the vertices.
Q.2 Explain and compare seed fill and edge fill algorithm for
polygon
ANS:
Seed Fill, often known as the Flood Fill algorithm, starts from an initial 'seed' point and
proceeds to fill the neighboring regions based on specific criteria. It's similar to filling a region
with color in paint software. There are two common types:
Comparison:
1. Criterion Basis: While Seed Fill colors a pixel based on its neighbors, Edge Fill colors until it
meets a boundary.
2. Efficiency: Edge Fill can be more efficient if boundary details are known as it directly works
with boundaries rather than assessing neighboring pixels extensively.
3. Implementation: Seed Fill can be simpler to implement, especially for small regions or
basic applications. However, Edge Fill can offer more precision when dealing with complex
shapes with predefined boundaries.
Q.3 Explain the steps in Scan Line Algorithm for filling the
polygon
ANS: The Scan Line Polygon Fill Algorithm is a method to determine and fill the pixels within a
polygon's boundary based on a scan line concept. Here's a concise breakdown of its steps:
- For each side (edge) of the polygon, record the minimum Y (starting Y), maximum Y (ending
Y), the X-coordinate associated with the minimum Y, and the slope inverse (1/m).
2. Sorting:
- Sort the edge table by minimum Y. If two sides have the same minimum Y, sort by the X-
coordinate.
- Starting from the minimum Y (lowest scan line that intersects the polygon) to the maximum
Y, perform the following for each scan line:
- From the AET, remove edges for which the scan line has passed the maximum Y.
c. Sort AET:
d. Fill Pixels:
- Fill alternate pairs of intersections. If the X-coordinates in the AET are X1, X2, X3, X4 ,...,
then fill pixels between X1 and X2, skip between X2 and X3, fill between X3 and X4, and so on.
e. Update X:
- For each edge in the AET, update the X-coordinate by adding the slope inverse.
- Count how many times the ray intersects with the polygon edges.
- Check how many times the polygon winds around the test point.
- Test if the point lies inside any of these triangles. If so, it lies inside the polygon.
- Similar to the Ray Casting method but specifically counts upward or downward edge crossings.
Principle: Seed Fill is a recursive method that starts from a 'seed' pixel and colors adjacent
pixels based on specific conditions, essentially filling regions of connectivity.
Working:
Examine neighboring pixels. If they are within the boundary and not already filled,
color them and treat them as new seeds.
Usage: Suitable for irregular and non-uniform shaped polygons. It can be categorized as 4-
connected or 8-connected based on pixel connectivity considered.
Principle: Similar to Seed Fill, but instead of filling until a boundary is met, Flood Fill replaces a
specified interior color (target color) with a new fill color.
Working:
Termination occurs when pixels do not match the target color or have already been
visited.
Steps:
1. Encoding Endpoints: Each endpoint of the line is assigned a 4-bit code (called the outcode)
based on its position relative to the clipping window.
2. Initial Test: If both outcodes are 0000, the line is inside the window and accepted. If the
bitwise AND of the outcodes is not 0000, the line is completely outside and rejected.
3. Clipping: If not accepted or rejected, find an intersection point of the line with the clipping
boundary (using the outcode of one of the endpoints), and replace that endpoint with the
intersection point. Repeat until the line is either accepted or rejected.
Example:
Consider a line with endpoints A(2, 8) and B(12, 4) and a clipping window with boundaries x=4,
x=10, y=3, and y=7.
- For A(2, 8):
Bitwise AND is not 0000, so the line isn't immediately accepted or rejected.
On processing, the line will intersect the clipping window and a portion of it will be within the
window.
Steps:
1. Initialization: Start with one clipping boundary (e.g., left edge of the clipping rectangle). The
polygon vertices will be processed against this edge first.
3. Possible Cases:
a. Both ( V_1 ) and ( V_2 ) are inside: Add ( V_2 ) to the output list.
b. ( V_1 ) is inside and ( V_2 ) is outside: Find the intersection point ( I ) with the clipping
boundary and add ( I ) to the output list.
c. Both ( V_1 ) and ( V_2 ) are outside: Discard ( V_2 ) (i.e., do nothing).
d. ( V_1 ) is outside and ( V_2 ) is inside: Find the intersection point ( I ) with the clipping
boundary, add ( I ) and then ( V_2 ) to the output list.
4. Iterate: Use the output list from one clipping boundary as the input for the next boundary.
Repeat steps 2-3 for each of the four boundaries of the clipping rectangle.
5. Completion: After all boundaries have been processed, the vertices in the final output list
define the clipped polygon.
Steps:
1. Initialization: Choose a point inside the polygon, termed as the 'seed' point.
2. Pixel Examination: Check the neighboring pixels around the seed to determine if they should
be colored.
- If a neighboring pixel meets the fill criteria (e.g., it's not yet colored or it's not a boundary
pixel), color it.
- Once colored, this new pixel becomes another seed point, leading to a recursive or iterative
process.
4. Termination: The process continues until all contiguous, non-boundary pixels connected to
the initial seed point have been filled.
There are two variations based on how we examine neighbors:
Steps:
1. Initialization: Choose an initial pixel (the 'seed') within the polygon or region to be filled.
2. Examination: Check the color of the seed pixel and determine the target color to be
replaced.
- Examine neighboring pixels. If they match the target color, change their color and treat them
as new seed points.
5. Completion: The algorithm stops when no more neighboring pixels of the target color can be
found.
Steps:
1. Initialization: List the vertices of the subject polygon and the clipping window.
2. Intersection Points: Calculate intersection points of the subject polygon with the clipping
window. Add these intersection points to both the subject and clipper lists, maintaining the
order.
3. Classification: Classify each vertex of the subject polygon and the clipping window as either
"inside" or "outside" based on the other polygon.
- Starting from an "inside" vertex of the subject polygon, traverse its vertices in order. If an
intersection point is reached, switch to the clipping window's list and continue until returning to
the starting point. This forms one clipped polygon.
Example:
Imagine a subject polygon shaped like a "U" and a square clipping window where the "U"
straddles the top edge of the square. Thus, part of the "U" is inside the square, and part is
outside.
1. Intersection Points: The two points where the "U" intersects the top edge of the square are
calculated.
2. Classification: The vertices of the "U" above the square are classified as "outside", and those
inside or on the square are "inside".
3. Resultant Polygons: Starting from an "inside" vertex at the bottom of the "U", we trace its
shape. Upon reaching an intersection point at the top of the square, we switch to tracing the
square's perimeter. Once we reach the second intersection point on the square, we switch back
to the "U" and continue until we close the polygon. The result is a polygon that represents the
portion of the "U" inside the square.
Q.11 Write short note on image transformation with example
ANS: Image transformation refers to the process of modifying the appearance of an image in
terms of its position, orientation, or size. There are three primary types of 2D transformations:
translation, rotation, and scaling.
1. Translation: Involves moving an image from one location to another without changing its
orientation or size.
- Example: Shifting an image 10 units to the right and 5 units up would be a translation.
Mathematically, if a point is at (x, y), after translation, it could be at (x+10, y+5).
2. Rotation: Involves turning an image around a specific point, called the pivot or rotation
point.
- Example: Rotating an image 45° about its center would result in each point moving to a new
location while maintaining the same distance from the center.
3. Scaling: Changes the size of an image. It can be uniform (same factor for both height and
width) or non-uniform.
- Example: Scaling an image by a factor of 2 would double its size. If a point is at (x, y), after
uniform scaling, it would be at (2x, 2y).