CG Exp 4
CG Exp 4
LAB Manual
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.04
A.1 Aim:
A.3 Outcome:
After successful completion of this experiment students will be able
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:
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
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)
Sort active edge list on x – Fill between alternate pairs of points (x,y) in order of sorted
active edges –
Else
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 :
#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();
"); scanf("%d",&n);
for(i=0;i<n;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");
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;
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(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.4 Conclusion:
In this Experiment we learned about scan line filling algorithm and fill
colour in various shapes
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:
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.
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.
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.
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>
*edge2 = temp;
int i, j;
void scanLineFill(int numEdges, struct Edge* edgeTable, int yMin, int yMax) {
int scanline;
{ int numActiveEdges = 0;
// Update the active edge list
activeEdgeList[numActiveEdges++] =
edgeTable[i]; }
sortAEL(activeEdgeList, numActiveEdges);
activeEdgeList[i].x += activeEdgeList[i].slopeInverse;
}
}
int main() {
edgeTable[i].ymax = verticesY[nextIndex];
edgeTable[i].x = verticesX[i];
} else {
edgeTable[i].ymax = verticesY[i];
edgeTable[i].x = verticesX[nextIndex];
closegraph();
return 0;