16

Click here to load reader

10CSL67 CG LAB PROGRAM 9

Embed Size (px)

Citation preview

Page 1: 10CSL67 CG LAB PROGRAM 9

PROGRAM 9

Program to fill any given polygon using scan-line area

filling algorithms. (Use appropriate data structures.)

Page 2: 10CSL67 CG LAB PROGRAM 9

Scanline Fill Algorithm

• Intersect scanline with polygon edges.

• Fill between pairs of intersections

Basic Structure:

For y=Ymin to Ymax

1) intersect scanline with each edge

2) sort intersections by increasing X

3) fill pairwise ( int0-> int1, int2->int3, ... )

Page 3: 10CSL67 CG LAB PROGRAM 9

Scanline Fill Algorithm Example

+X

+Y

P1

5, 10

P2

20, 15

P3

15, 25

P4

0, 20

Page 4: 10CSL67 CG LAB PROGRAM 9

Scanline Fill Algorithm Example

+X

+Y

P1

5, 10

P2

20, 15

P3

15, 25

P4

0, 20

Line No.

Points Y co-ordinates Le Re

L1 P1 - P2 10 - 15 5 20

L2 P2 - P3 15 - 25 20 15

L3 P3 - P4 25 - 20 swap 20 - 25 0 15

L4 P4 - P1 20 - 10 swap 10 - 20 5 0

Page 5: 10CSL67 CG LAB PROGRAM 9

Scanline Fill Algorithm Example

+X

+Y

P1 5, 10

P2 20, 15

P3 15, 25

P4 0, 20

Line No.

Points Y co-ordinates

Le Re

L1 P1 - P2 10 - 15 Le[10]=5 Le[15]=20 Re[10]=5 Re[15]=20

L2 P2 - P3 15 - 25 Le[15]=20 Le[25]=15 Re[15]=20 Re[25]=15

L3 P3 - P4 20 – 25 Le[20]=0 Le[25]=15 Re[20]=17.5 Re[25]=15

L4 P4 - P1 10 – 20 Le[10]=5 Le[20]=0 Re[10]=5 Re[20]=17.5

Page 6: 10CSL67 CG LAB PROGRAM 9

#include <GL/glut.h>float x1, x2, x3, x4, y1, y2, y3, y4;void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re){

float mx, x, temp;int i;if((y2-y1)<0) // if(y2<y1){ temp=y1; y1=y2; y2=temp; // swap y1 & y2

temp=x1; x1=x2; x2=temp; // swap x1 & x2

}if((y2-y1)!=0) // if(y2!=y1)mx=(x2-x1)/(y2-y1);else mx=x2-x1;x=x1;

Page 7: 10CSL67 CG LAB PROGRAM 9

for(i=y1;i<=y2;i++){

if(x<(float)le[i])le[i]=(int)x;if(x>(float)re[i])re[i]=(int)x;x+=mx;

}}void draw_pixel(int x,int y){

glColor3f(1.0,0.0,0.0);glBegin(GL_POINTS);glVertex2i(x,y);glEnd();glFlush(); // filling points with delay

}

Page 8: 10CSL67 CG LAB PROGRAM 9

void scanfill (float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)

{int le[500],re[500],i,y;for(i=0;i<500;i++){

le[i]=500;re[i]=0;

}edgedetect(x1,y1,x2,y2,le,re);edgedetect(x2,y2,x3,y3,le,re);edgedetect(x3,y3,x4,y4,le,re);edgedetect(x4,y4,x1,y1,le,re);

+X

+Y

P1 5, 10

P2 20, 15

P3 15, 25

P4 0, 20

Page 9: 10CSL67 CG LAB PROGRAM 9

for(y=0;y<500;y++){

if(le[y]<=re[y])for(i=(int)le[y];i<(int)re[y];i++)draw_pixel(i,y);

}}void display(){ x1=300.0; y1=200.0;

x2=100.0; y2=300.0;x3=200.0; y3=400.0;x4=300.0; y4=300.0;glClear(GL_COLOR_BUFFER_BIT);glColor3f(0.0, 0.0, 1.0);glBegin(GL_LINE_LOOP);glVertex2f(x1,y1); glVertex2f(x2,y2);glVertex2f(x3,y3); glVertex2f(x4,y4);glEnd();

P1 300, 200

P2 100, 300

P3 200, 400

P4 300, 300

Page 10: 10CSL67 CG LAB PROGRAM 9

scanfill(x1,y1,x2,y2,x3,y3,x4,y4);glFlush();

}

void myinit(){

glClearColor(1.0,1.0,1.0,1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,499.0,0.0,499.0);

}

P1 300, 200

P2 100, 300

P3 200, 400

P4 300, 300

Page 11: 10CSL67 CG LAB PROGRAM 9

void main(int argc, char** argv){

glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(300,300);glutCreateWindow("Filling a Polygon using Scan-line Algorithm");

glutDisplayFunc(display);myinit();glutMainLoop();

}

Page 12: 10CSL67 CG LAB PROGRAM 9

Activity 9

1. Change the shape of the polygon (know the difference between concave and convex polygon).

Convex polygon• A convex polygon is a multi-angled shape with straight

sides in which all of the angles are < 1800.• A line drawn through a convex polygon will intersect the

sides of the polygon exactly twice.

Page 13: 10CSL67 CG LAB PROGRAM 9

void display(){x1=100.0; y1=100.0;x2=50.0; y2=200.0;x3=150.0; y3=300.0;x4=250.0; y4=200.0;x5=200; y5=100;--------glBegin(GL_LINE_LOOP);glVertex2f(x1,y1);glVertex2f(x2,y2);glVertex2f(x3,y3);glVertex2f(x4,y4);glVertex2f(x5,y5);glEnd();scanfill(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);glFlush();}

Page 14: 10CSL67 CG LAB PROGRAM 9

void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4,float x5, float y5){

--------edgedetect(x1,y1,x2,y2,le,re);edgedetect(x2,y2,x3,y3,le,re);edgedetect(x3,y3,x4,y4,le,re);edgedetect(x4,y4,x5,y5,le,re);edgedetect(x5,y5,x1,y1,le,re);--------

}

Page 15: 10CSL67 CG LAB PROGRAM 9

Concave polygon• A concave polygon is a multi-angled shape with straight

sides in which all of the angles are > 1800. • A line drawn through a convex polygon will intersect

the sides of the polygon exactly twice.• Concave polygons are polygons for which atleast one

line segment joining any two points in the interior does not lie completely within the figure.

Page 16: 10CSL67 CG LAB PROGRAM 9

Try this ……x1=100.0; y1=100.0;x2=50.0; y2=200.0;x3=150.0; y3=150.0;x4=250.0; y4=200.0;x5=200; y5=100;

Scanline fill algorithm works only for convex polygons,hence the sides making 1800 are ignored till thepolygon becomes convex and then it is filled with thepixels.

x1, y1

x2, y2

x3, y3

x4, y4

x5, y5