I017 CG Lab4-1
I017 CG Lab4-1
I017 CG Lab4-1
Topic covered: Polygon filling algorithm (seed fill/Edge Fill approach) and (Flood fill approach).
Learning Objective: Learner would be able to
1. To understand the different method to fill the polygon.
2. To recognize the calculation, perform for filling the polygon with different technique.
3. Both methods are crucial for creating filled graphical primitives in computer graphics applications,
such as in games, simulations, and graphical user interfaces..
Prerequisites:-
- python
Outcomes:-
- Student will explore the method to fill the polygon with two different method.
1|P a g e
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Aim:- To learn the algorithm to Seed Fill/Edge Fill Approach and Flood Fill Approach.
Assignment 4
1. Problem Statement: You have a 10x10 pixel canvas where each pixel can either be colored (1) or not
colored (0). The canvas has an irregular polygon shape drawn on it, represented by colored pixels (1). Your
task is to fill this polygon using the Seed Fill/Edge Fill Approach.
Canvas = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
Objective:
Write a function in a programming language of your choice (e.g., Python, C++) that applies the Seed Fill/Edge Fill
Approach to fill this polygon. The function should take the initial canvas and a seed point within the polygon as
input and return the canvas with the polygon filled.
Example Seed Point: (2, 2) - This point lies within the polygon that needs to be filled.
Expected Output:
The canvas should show the polygon filled with a different color (e.g., replace 0 with 2 inside the polygon).
This problem can be approached by recursively coloring adjacent pixels from the seed point until reaching the
edges of the polygon. The challenge lies in correctly identifying the boundaries of the polygon and ensuring that
the fill does not 'leak' outside the intended area.
Code:
canvas = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
Output:
3
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Example Start Point and New Color: (2, 2) as start point, and 2 as the new color.
Expected Output:
The canvas should display the area connected to the start point filled with the new color (2).
This algorithm works by starting at a given point (the seed), checking the color of the adjacent pixels, and
changing them if they match the original color of the seed. It continues to apply this process recursively,
spreading outwards until it reaches the boundary of the area.
Code:
canvas_flood = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
Output:
5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Conclusion:-
Pros:
Cons:
Pros:
6
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Cons:
Recursive implementation may lead to many function calls, impacting memory usage.
May not perform well for irregular or complex shapes.
Comparison:
The Seed Fill/Edge Fill Approach is more time-consuming for large areas or complex shapes due to its recursive
nature, which can lead to stack overflow issues. It may not handle concave shapes or disconnected regions
efficiently.
The Flood Fill Algorithm, on the other hand, is generally more efficient for filling large contiguous areas and is
better suited for irregular shapes. While it can also be implemented recursively, iterative versions of the algorithm
can be used to mitigate memory concerns.
In conclusion, for the given problem statement involving irregular polygons, the Flood Fill Algorithm is likely a more
suitable choice due to its efficiency in handling large contiguous areas and its ability to manage concave shapes
and disconnected regions.