Professional Documents
Culture Documents
cpp 1
1 #include<GL/glut.h>
2 #define BLACK 0
3 float x1,x2,x3,x4,y1,y2,y3,y4;
4 void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
5 {
6 float mx,x,temp;
7 int i;
8 if((y2-y1)<0)
9 {
10 temp=y1; y1=y2; y2=temp;
11 temp=x1; x1=x2; x2=temp;
12 }
13 if((y2-y1)!=0)
14 mx=(x2-x1)/(y2-y1);
15 else
16 mx=x2-x1;
17 x=x1;
18 for(i=y1;i<=y2;i++)
19 {
20 if(x<(float)le[i])
21 le[i]=(int)x;
22 if(x>(float)re[i])
23 re[i]=(int)x;
24 x+=mx;
25 }
26 }
27 void draw_pixel(int x,int y,int value)
28 {
29 glBegin(GL_POINTS);
30 glVertex2i(x,y);
31 glEnd();
32 }
33 void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)
34 {
35 int le[500],re[500];
36 int i,y;
37 for(i=0;i<500;i++)
38 {
39 le[i]=500;
40 re[i]=0;
41 }
42 edgedetect(x1,y1,x2,y2,le,re);
43 edgedetect(x2,y2,x3,y3,le,re);
44 edgedetect(x3,y3,x4,y4,le,re);
45 edgedetect(x4,y4,x1,y1,le,re);
46 for(y=0;y<500;y++)
47 {
48 if(le[y]<=re[y])
49 {
50 for(i=(int) le[y]; i<(int) re[y]; i++)
51 draw_pixel(i,y,BLACK);
52 glFlush();
53 }
54 }
55 }
56 void display()
57 {
58 x1=200.0,y1=200.0,x2=100.0,y2=300.0,x3=200.0,y3=400.0,x4=300.0,y4=300.0;
59 glClear(GL_COLOR_BUFFER_BIT);
60 glBegin(GL_LINE_LOOP);
61 glVertex2f(x1,y1);
62 glVertex2f(x2,y2);
63 glVertex2f(x3,y3);
64 glVertex2f(x4,y4);
65 glEnd();
66 scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
67 glFlush();
68 }
69 void myinit()
70 {
71 glClearColor(1.0,1.0,1.0,1.0);
72 glPointSize(1.0);
73 glMatrixMode(GL_PROJECTION);
74 glLoadIdentity();
C:\Users\madar\Documents\Visual Studio 2010\Projects\prgm9\prgm9\prg9.cpp 2
75 gluOrtho2D(0.0,499.0,0.0,499.0);
76 }
77 void menu(int id)
78 {
79 switch(id)
80 {
81 case 0: glColor3f(1.0,0.0,0.0);
82 break;
83 case 1: glColor3f(0.0,1.0,0.0);
84 break;
85 case 2: glColor3f(0.0,0.0,1.0);
86 break;
87 }
88 glutPostRedisplay();
89 }
90 int main(int argc,char **argv)
91 {
92 glutInit(&argc,argv);
93 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
94 glutInitWindowSize(500,500);
95 glutInitWindowPosition(0,0);
96 glutCreateWindow("Scan Fill");
97 glutDisplayFunc(display);
98 glutCreateMenu(menu);
99 glutAddMenuEntry("red",0);
100 glutAddMenuEntry("green", 1);
101 glutAddMenuEntry("blue", 2);
102 glutAttachMenu(GLUT_LEFT_BUTTON);
103 myinit();
104 glutMainLoop();
105 return 0;
106 }