Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

I017 CG Lab4-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER V

COURSE: Computer Graphics Practical Experiment: 4

Part A (To be referred by students)

SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp1)

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

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 4

Student Name Ritesh Kumar Student Sap ID:- 70412100036


Student Roll Number I017 Date of Conduction :/ / 2023
Class :- MBA.Tech IT/B.Tech IT VI sem

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:

import matplotlib.pyplot as plt


import numpy as np

def seed_fill(x, y, fill_color, boundary_color, screen):


if (x < 0 or x >= len(screen[0]) or y < 0 or y >= len(screen) or
screen[y][x] == fill_color or screen[y][x] == boundary_color):
return
screen[y][x] = fill_color
seed_fill(x + 1, y, fill_color, boundary_color, screen)
2
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 4

seed_fill(x - 1, y, fill_color, boundary_color, screen)


seed_fill(x, y + 1, fill_color, boundary_color, screen)
seed_fill(x, y - 1, fill_color, boundary_color, screen)

def create_plot(screen, colors):


height = len(screen)
width = len(screen[0])
data = np.zeros((height, width, 3), dtype=np.uint8)
for y in range(height):
for x in range(width):
data[y, x] = colors[screen[y][x]]
plt.imshow(data)
plt.axis('off')
plt.show()

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]
]

# Apply the Seed Fill/Edge Fill Approach


seed_fill(2, 2, 2, 1, canvas)

# Create and show the plot for Seed Fill


create_plot(canvas, {0: [255, 255, 255], 1: [0, 0, 0], 2: [0, 0, 255]})

Output:

3
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 4

2. Consider the same canvas for flood fill algorithm.


Objective:
Create a function in a suitable programming language (like Python or C++) that implements the Flood Fill
Algorithm. The function should take the canvas, a start point, and the new color as input, and return the
canvas with the contiguous area filled with the new color.

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:

import matplotlib.pyplot as plt


import numpy as np
4
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 4

def flood_fill(x, y, new_color, original_color, screen):


if (x < 0 or x >= len(screen[0]) or y < 0 or y >= len(screen) or
screen[y][x] != original_color):
return
screen[y][x] = new_color
flood_fill(x + 1, y, new_color, original_color, screen)
flood_fill(x - 1, y, new_color, original_color, screen)
flood_fill(x, y + 1, new_color, original_color, screen)
flood_fill(x, y - 1, new_color, original_color, screen)

def create_plot(screen, colors):


height = len(screen)
width = len(screen[0])
data = np.zeros((height, width, 3), dtype=np.uint8)
for y in range(height):
for x in range(width):
data[y, x] = colors[screen[y][x]]
plt.imshow(data)
plt.axis('off')
plt.show()

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]
]

# Apply the Flood Fill Algorithm


flood_fill(2, 2, 2, 0, canvas_flood)

# Create and show the plot for Flood Fill


create_plot(canvas_flood, {0: [255, 255, 255], 1: [0, 0, 0], 2: [255, 0, 0]})

Output:

5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 4

Conclusion:-

Seed Fill/Edge Fill Approach:

Pros:

 Simple and intuitive conceptually.


 Easy to implement.
 Can be useful for filling irregular shapes.

Cons:

 May suffer from stack overflow issues for large areas.


 Inefficient for complex shapes with intricate boundaries.
 Difficulties in handling concave polygons or disconnected regions.

Flood Fill Algorithm:

Pros:

 Efficient for filling large contiguous areas.

6
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 4

 Handles concave shapes and disconnected regions well.


 No risk of stack overflow issues due to its iterative nature.

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.

You might also like