Assignment 4 (Polygon Filling)
Assignment 4 (Polygon Filling)
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)])
y += 1
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
# 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]
# 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.')
Output: -