0% found this document useful (0 votes)
20 views

CG Exp 4

an experiment

Uploaded by

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

CG Exp 4

an experiment

Uploaded by

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

Terna Engineering College

Computer Engineering Department


Program: Sem-III

Course: Computer Graphics

Faculty: Prof. Sushant Satish Sawant

LAB Manual

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.04
A.1 Aim:

Implement Scan line Polygon Fill


A.2 Prerequisite:
1. C Language

A.3 Outcome:
After successful completion of this experiment students will be able

to Implement various output and filled area primitive algorithms

A.4 Theory:

Basic Idea:

The basic idea is to collect all of the edges (except horizontal edges) that compose the
polygon, fill in the figure scan line by scan line using the edges as starting and
stopping points.

Main Components:

To successfully fill in a polygon three main components will be used: Edge Buckets, an
Edge Table and an Active List. These components will contain an edge’s information,
hold all of the edges that compose the figure and maintain the current edges being
used to fill in the polygon, respectively.

Edge Buckets

The edge bucket is a structure that holds information about the polygon’s edges. The
edge bucket looks different pending on the implementation of the algorithm, in my
case it looks like this:

Edge Bucket representation

Here is a breakdown of what each member represent in an edge bucket:

 yMax: Maximum Y position of the edge


  yMin: Minimum Y position of the edge
 x: The current x position along the scan line, initially starting at the same point as
the yMin of the edge
 sign: The sign of the edge’s slope ( either -1 or 1)
 dX: The absolute delta x (difference) between the edge’s vertex points 
  dY: The absolute delta y (difference) between the edge’s vertex points 
 sum: Initiated to zero. Used as the scan lines are being filled to x to the
next position

Edge Table (ET)

The ET is a list that contains all of the edges that make up the figure.

Important ET notes:

 When creating edges, the vertices of the edge need to be ordered from left to
 right
 The edges are maintained in increasing yMin order
  The edges are removed from the ET once the Active List is done processing them
 The algorithm is done filling the polygon once all of the edges are removed
from the ET
Active List (AL)

The AL contains the edges that are being processed/used to fill the polygon. Every
edge in the AL has a pairing buddy edge, because when filling a scan line, pixels are
filled starting from one edge until the buddy edge is encountered.

Important AL notes:

 Edges are pushed into the AL from the Edge Table once an edge’s yMin is
 equal to the current scan line being processed
 Edges will always be in the AL in pairs
  Edges in the AL are maintained in increasing x order.
 The AL will be re-sorted after every pass

Steps:

Now that we have the main components covered, lets go over the SLPF algorithm in
more detailed steps.

~General assumptions~

 The user has access to a method that can set the color of individual pixels.
 You will see that I simply call the setPixel() method whenever I fill in a pixel.
 The vertices of the given shape are listed in order around the circumference of
 the polygon
 This one is important, otherwise the polygons will not be drawn properly

Here are the steps:

1. Create ET
1. Process the vertices list in pairs, start with [numOfVertices-1] and [0].
2. For each vertex pair, create an edge bucket
2. Sort ET by yMin
3. Process the ET
1. Start on the scan line equal to theyMin of the first edge in the ET
2. While the ET contains edges
1. Check if any edges in the AL need to be removes (when yMax == current scan
line)
1. If an edge is removed from the AL, remove the associated the Edge Bucket
from the Edge Table.
2. If any edges have a yMin == current scan line, add them to the AL
3. Sort the edges in AL by X
4. Fill in the scan line between pairs of edges in AL
5. Increment current scan line
6. Increment all the X's in the AL edges based on their slope
1. If the edge's slope is vertical, the bucket's x member is NOT incremented.

A.5 Procedure:
Scan Line Polygon Fill Algorithm:

1. Set up edge table from vertex list; determine range of scanlines spanning polygon
(miny, maxy)

2. Preprocess edges with nonlocal max/min endpoints

3. Set up activation table (bin sort)

4. For each scanline spanned by polygon: –

Add new active edges to AEL using activation table –

Sort active edge list on x – Fill between alternate pairs of points (x,y) in order of sorted
active edges –

For each edge e in active edge list: If (y != ymax[e])

Compute & store new x (x+=1/m)

Else

Delete edge e from the active edge list

PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Roll No. 60 Name: Quazi Md Moinuddin
Class :SE C Batch : C4
Date of Experiment:06/10/23 Date of Submission 06/10/23
Grade :

B.1 Document created by the student:


#include <stdio.h>

#include <conio.h>

#include <graphics.h>

main()

int n,i,j,k,gd,gm,dy,dx;

int x,y,temp;

int a[20][2],xi[20];

float slope[20];

clrscr();

printf("\n\n\tEnter the no. of edges of polygon :

"); scanf("%d",&n);

printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++)

printf("\tX%d Y%d : ",i,i);

scanf("%d %d",&a[i][0],&a[i][1]);

a[n][0]=a[0][0];

a[n][1]=a[0][1];
detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:\\tc\\bgi");

/*- draw polygon -*/

for(i=0;i<n;i++)

line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);

getch();

for(i=0;i<n;i++)

dy=a[i+1][1]-a[i][1];

dx=a[i+1][0]-a[i][0];

if(dy==0) slope[i]=1.0;

if(dx==0) slope[i]=0.0;

if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/

slope[i]=(float) dx/dy;

for(y=0;y< 480;y++)

k=0;

for(i=0;i<n;i++)

if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))

xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));

k++;

for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/

for(i=0;i<k-1;i++)

if(xi[i]>xi[i+1])

temp=xi[i];

xi[i]=xi[i+1];

xi[i+1]=temp;

setcolor(35);

for(i=0;i<k;i+=2)

line(xi[i],y,xi[i+1]+1,y);

getch();

}
Ouput

B.3 Observations and learning:

In this experiment , we used Scanline filling algorithm to fill


different shape like triangle. This work with lines(one at a time )

B.4 Conclusion:

In this Experiment we learned about scan line filling algorithm and fill
colour in various shapes

B.5 Question of Curiosity


Q.1] What are advantages and disadvantages of scan line fill algorithm?

Ans-The scan line fill algorithm is a computer graphics technique used for filling
polygons with colors or patterns. Here are some advantages and disadvantages of this
algorithm:

Advantages:

1. Efficiency: The scan line algorithm is efficient for filling complex shapes because it
only processes the edges of the polygon and scans horizontally across each scan line,
making it faster than some other filling algorithms.

2. Handles Concave and Convex Polygons: It can fill both concave and convex polygons
without any special handling, which simplifies the implementation.

Disadvantages:

1. Memory Usage: The algorithm requires additional memory to maintain data


structures like the edge table and the active edge list, which can be a disadvantage for
large polygons or systems with limited memory.

2. Complexity: Implementing the scan line algorithm correctly can be relatively complex
compared to some other algorithms, like the flood-fill algorithm.

3. Not Suitable for All Shapes: While it's versatile, the scan line algorithm may not be
the best choice for irregular or highly complex shapes, as it can result in inefficient
processing and increased complexity in handling edge cases.

Q.2] What is an Active Edge List in the Scan Line Algorithm?

Ans-In the scan line fill algorithm, the Active Edge List (AEL) is a data structure used to
keep track of the edges of a polygon that intersect the current scan line being processed.
The AEL is crucial for efficiently filling the polygon by determining which portions of
the scan line need to be colored.

How the Active Edge List works:

1. Initialization: When the scan line algorithm starts processing a new scan line, the AEL
is typically initialized as an empty list.
2. Edge Entry: As the algorithm scans horizontally across the polygon, it encounters
edges that intersect the current scan line. When an edge intersects the scan line, it is
added to the AEL with information about its slope, X-intercept, and other relevant
attributes.

3. Edge Removal: Edges are removed from the AEL when they are no longer
intersecting the current scan line. This occurs when the edge's endpoint has been
reached.

4. Sorting: The AEL is often sorted based on the X-coordinate of the intersection point of
each edge with the scan line. This ensures that the edges are processed in the correct
order as the algorithm moves from left to right along the scan line.

5. Filling: As the AEL is maintained and sorted, the algorithm can efficiently determine
which portions of the scan line need to be filled with the specified color or pattern. It
tracks pairs of edges in the AEL and fills the spaces between them on the current scan
line.

By using the Active Edge List, the scan line algorithm can handle complex polygons
with multiple intersecting edges and efficiently determine which pixels to color, making
it a powerful method for polygon filling in computer graphics.

Q.3] Implement Scan Line Algorithm Using C language.

Ans-Implementing the scan line algorithm for polygon filling in C involves several
steps. Below is a simplified example of how you can implement it. This code assumes
you have a basic understanding of C programming and graphics libraries like OpenGL
or SDL for drawing.

#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

// Define a structure to represent an

edge struct Edge {

int ymax; // Maximum Y-coordinate of the edge

float x; // Current X-coordinate of the edge float

slopeInverse; // Inverse slope of the edge


};

// Function to swap two edges

void swap(struct Edge* edge1, struct Edge* edge2) {

struct Edge temp = *edge1; *edge1 = *edge2;

*edge2 = temp;

// Function to sort the active edge list based on X-

coordinate void sortAEL(struct Edge* ael, int numEdges) {

int i, j;

for (i = 0; i < numEdges - 1; i++) {

for (j = 0; j < numEdges - i - 1; j++)

{ if (ael[j].x > ael[j + 1].x) {

swap(&ael[j], &ael[j + 1]);

// Function to fill the polygon using scan line algorithm

void scanLineFill(int numEdges, struct Edge* edgeTable, int yMin, int yMax) {

struct Edge activeEdgeList[numEdges];

int scanline;

for (scanline = yMin; scanline < yMax; scanline++)

{ int numActiveEdges = 0;
// Update the active edge list

for (int i = 0; i < numEdges; i++) { if

(edgeTable[i].ymax > scanline) {

activeEdgeList[numActiveEdges++] =

edgeTable[i]; }

// Sort the active edge list by X-coordinate

sortAEL(activeEdgeList, numActiveEdges);

// Fill the scanline between pairs of edges in the active edge

list for (int i = 0; i < numActiveEdges; i += 2) {

int xStart = activeEdgeList[i].x; int

xEnd = activeEdgeList[i + 1].x;

// Fill the pixels between xStart and xEnd on the current

scanline for (int x = xStart; x < xEnd; x++) {

putpixel(x, scanline, WHITE); // Change WHITE to your desired color

// Update the X-coordinates of the remaining edges in the active edge

list for (int i = 0; i < numActiveEdges; i++) {

activeEdgeList[i].x += activeEdgeList[i].slopeInverse;

}
}

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

// Define the vertices of the

polygon int numVertices = 4;

int verticesX[] = {100, 200, 300, 200};

int verticesY[] = {100, 200, 100, 50};

// Build the edge table

int numEdges = numVertices;

struct Edge edgeTable[numEdges];

for (int i = 0; i < numEdges; i++) {

int nextIndex = (i + 1) % numEdges;

if (verticesY[i] < verticesY[nextIndex]) {

edgeTable[i].ymax = verticesY[nextIndex];

edgeTable[i].x = verticesX[i];

} else {

edgeTable[i].ymax = verticesY[i];

edgeTable[i].x = verticesX[nextIndex];

edgeTable[i].slopeInverse = (float)(verticesX[nextIndex] - verticesX[i]) / (verticesY[nextIndex] -


verticesY[i]);
}

int yMin = 50; // Minimum Y-coordinate of the polygon

int yMax = 200; // Maximum Y-coordinate of the polygon

// Call the scanLineFill function to fill the polygon

scanLineFill(numEdges, edgeTable, yMin, yMax);

delay(5000); // Delay to display the graphics

closegraph();

return 0;

You might also like