0% found this document useful (0 votes)
39 views5 pages

Assignment 4 (Polygon Filling)

The document is an assignment submission for a computer graphics lab course. It contains two questions asking the student to use scan line polygon filling algorithms to fill shapes. For the first question, the student is asked to fill a 200x150 rectangle in red, which they do by implementing the scan line algorithm. For the second question, the student is asked to fill a 5-vertex polygon with given coordinates in green, which they also do by implementing the scan line algorithm. Code and output images are provided for both questions.

Uploaded by

Piyali kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

Assignment 4 (Polygon Filling)

The document is an assignment submission for a computer graphics lab course. It contains two questions asking the student to use scan line polygon filling algorithms to fill shapes. For the first question, the student is asked to fill a 200x150 rectangle in red, which they do by implementing the scan line algorithm. For the second question, the student is asked to fill a 5-vertex polygon with given coordinates in green, which they also do by implementing the scan line algorithm. Code and output images are provided for both questions.

Uploaded by

Piyali kar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

School: SCOPE Semester: WIN SEM 2022-23

Subject: Computer Graphics (Lab) Subject Code: CSE2006


Assignment 4

NAME: Piyali Kar


Registration No: 20BCE7124

1. Fill a rectangle (width= 200, height= 150) with red colour using Scan Line
Polygon Filling Algorithms.

Code: -

import numpy as np
import matplotlib.pyplot as plt

# rectangle coordinates
x1, y1 = 100, 50
x2, y2 = 300, 200

# empty image
img = np.zeros((300, 400))

# edge table
ET = [[] for i in range(300)]
ET[y1].append([x1, (x2-x1)/(y2-y1)])
ET[y2].append([x1, (x2-x1)/(y2-y1)])
for y in range(y1+1, y2):
    x = x1 + (y-y1)*(x2-x1)/(y2-y1)
    ET[y].append([x, (x2-x1)/(y2-y1)])

# creating active edge table and scan line y


AET = []
y = y1
# scan line algorithm
while len(AET) > 0 or len(ET[y]) > 0:
    # adding new edges to AET
    for edge in ET[y]:
        AET.append(edge)
    AET.sort()

    # filling pixels between edges


    for i in range(0, len(AET), 2):
        x1 = int(round(AET[i][0]))
        x2 = int(round(AET[i+1][0]))
        img[y, x1:x2+1] = 1

    # removing edges from AET


    for i, edge in enumerate(AET):
        if int(round(edge[0])) == x2:
            AET.pop(i)
            break

    # update x for remaining edges in AET


    for i, edge in enumerate(AET):
        AET[i][0] += edge[1]

    y += 1

# Display the output


plt.imshow(img, cmap='Reds')
plt.text(320, 280, "Piyali Kar\n20BCE7124", fontsize=12, color='red',
va='bottom', ha='right')
plt.show()
Output: -

2. Consider a polygon of 5 vertices with coordinates [30, 30] , [30 150] , [120, 60]
, [120, 120] and [210, 30]. Fill the polygon with green colour using Scan Line
Polygon Filling Algorithms.

Expected Output:

Code: -
import matplotlib.pyplot as plt

# Defining the vertices of the polygon


vertices = [[30, 30], [30, 150], [120, 60], [120, 120], [210, 30]]

# Find the minimum and maximum y coordinates of the polygon


y_min = min([vertex[1] for vertex in vertices])
y_max = max([vertex[1] for vertex in vertices])

# Define the list of edges of the polygon


edges = []
for i in range(len(vertices)):
    x1, y1 = vertices[i]
    x2, y2 = vertices[(i + 1) % len(vertices)]
    if y1 < y2:
        edges.append((y1, x1, y2, x2))
    else:
        edges.append((y2, x2, y1, x1))

# Initialize the list of active edges


active_edges = []

# Iterate over the scan lines


for y in range(y_min, y_max + 1):

    # Add edges that start on this scan line to the active edge list
    for edge in edges:
        if edge[0] == y:
            active_edges.append(edge)

    # Remove edges that end on this scan line from the active edge list
    active_edges = [edge for edge in active_edges if edge[2] != y]

    # Sort the active edge list by x coordinate


    active_edges.sort(key=lambda edge: edge[1])

    # Fill the pixels between pairs of edges in the active edge list
    for i in range(0, len(active_edges), 2):
        x1 = active_edges[i][1]
        x2 = active_edges[i + 1][1]
        for x in range(x1, x2 + 1):
            plt.plot(x, y, 'g.')

# Set the axis limits and show the plot


plt.axis([0, 250, 0, 200])
plt.text(220, 190, "Piyali Kar\n20BCE7124", fontsize=12, color='red',
va='centre', ha='right')
plt.show()

Output: -

You might also like