0% found this document useful (0 votes)
7 views4 pages

EXPERIMENT NO- 6

Cg polygon fill experiment

Uploaded by

shivshinde4593
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)
7 views4 pages

EXPERIMENT NO- 6

Cg polygon fill experiment

Uploaded by

shivshinde4593
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/ 4

EXPERIMENT NO- 6

AIM: To implement the Scan line polygon fill algorithm for coloring a given object.

RESOURCES REQUIRED:
H/W Requirements: RAM 128MB, Printers and Cartridges, Print Out Stationary
S/W Requirements: Turbo C / C++ compiler that supports graphics.h package.

THEORY:
Polygon is an ordered list of vertices as shown in the following figure. For filling
polygons with
particular colors, you need to determine the pixels falling on the border of the
polygon and those
which fall inside the polygon. In this chapter, we will see how we can fill polygons
using different
techniques.
This algorithm works by intersecting scanlines with polygon edges and fills the
polygon between
pairs of intersections. The following steps depict how this algorithm works.
Scan Line Algorithm:
Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax.
Name each
intersection point of the polygon. As per the figure shown above, they are named as
p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e.
p0,p1, p1,p2
and p2,p3
Step 4 − Fill all those pairs of coordinates that are inside polygons and ignore the
alternate pairs.

CONCLUSION: Scan line polygon fill algorithm is implemented.

Aim: Implement scan line polygon filling algorithm.


#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void 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 coordinates 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:\\TurboC3\\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(3);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy