#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#include
#include"math.h"
#include "glut.h"
#define Round(x) int(x+0.5)
int width,height,xc,yc,xi,yi;
struct Colorentry
{
GLubyte Red;
GLubyte Blue;
GLubyte Green;
Colorentry() {Red=Green=Blue=0; }
Colorentry(GLbyte R,GLbyte G,GLbyte B)
{
Red=R;
Green=G;
Blue=B;
}
int operator==(Colorentry &c)
{
return (Red==c.Red && Green==c.Green && Blue==c.Blue);
}
int operator!=(Colorentry &c)
{
return !(*this==c);
}
};
void PutPixel(int x,int y,Colorentry c)
{
glColor3ub(c.Red,c.Green,c.Blue);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void PutPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
Colorentry GetPixel(int x,int y)
{
GLbyte color[3];
float Pos[4];
glRasterPos2d(x,y);
glGetFloatv(GL_CURRENT_RASTER_POSITION,Pos);
glReadPixels(Pos[0],Pos[1],1,1,GL_RGB,GL_UNSIGNED_BYTE,color);
return Colorentry(color[0],color[1],color[2]);
}
void Fillleft(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor);
void Fillright(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor);
void Filltop(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor);
void Fillbottom(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor);
void Fillleft(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor)
{
Colorentry color=GetPixel(x,y);
if(color!=Fillcolor && color!= Boundarycolor)
{
PutPixel(x,y,Fillcolor);
Fillleft (x-1, y , Fillcolor, Boundarycolor);
Filltop (x , y+1, Fillcolor, Boundarycolor);
Fillbottom (x , y-1, Fillcolor, Boundarycolor);
}
}
void Fillright(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor)
{
Colorentry color=GetPixel(x,y);
if(color!=Fillcolor && color!= Boundarycolor)
{
PutPixel(x,y,Fillcolor);
Fillright (x+1, y , Fillcolor, Boundarycolor);
Filltop (x , y+1, Fillcolor, Boundarycolor);
Fillbottom (x , y-1, Fillcolor, Boundarycolor);
}
}
void Filltop(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor)
{
Colorentry color=GetPixel(x,y);
if(color!=Fillcolor && color!= Boundarycolor)
{
PutPixel(x,y,Fillcolor);
Fillleft (x-1, y , Fillcolor, Boundarycolor);
Fillright (x+1, y , Fillcolor, Boundarycolor);
Filltop (x , y+1, Fillcolor, Boundarycolor);
}
}
void Fillbottom(int x,int y,Colorentry Fillcolor,Colorentry Boundarycolor)
{
Colorentry color=GetPixel(x,y);
if(color!=Fillcolor && color!= Boundarycolor)
{
PutPixel(x,y,Fillcolor);
Fillleft (x-1, y , Fillcolor, Boundarycolor);
Fillright (x+1, y , Fillcolor, Boundarycolor);
Fillbottom (x , y-1, Fillcolor, Boundarycolor);
}
}
void BundaryFill(int x, int y, Colorentry Fillcolor, Colorentry Boundarycolor)
{
Colorentry color=GetPixel(x,y);
if(color != Fillcolor && color != Boundarycolor)
{
PutPixel(x,y,Fillcolor);
Fillleft (x-1, y , Fillcolor, Boundarycolor);
Fillright (x+1, y , Fillcolor, Boundarycolor);
Filltop (x , y+1, Fillcolor, Boundarycolor);
Fillbottom (x , y-1, Fillcolor, Boundarycolor);
}
}
void DrawRectangle_Rotate(int xc,int yc,int a, int b)
{
int dPoints[][2] = { {-a,b},{a,b},{a,-b},{-a,-b} };
int n = 4;
int x,y;
glBegin(GL_LINE_LOOP);
for(int i=0;i {
x = xc + dPoints[i][0];
y = yc + dPoints[i][1];
glVertex2f(x,y);
}
glEnd();
}
void test()
{
DrawRectangle_Rotate(xc,yc,30,20);
Colorentry a(255,255,0);
Colorentry b(255,0,0);
BundaryFill(xc,yc,a,b);
}
//-----------------------------------
void myInit()
{
width=glutGet(GLUT_WINDOW_WIDTH);
height=glutGet(GLUT_WINDOW_HEIGHT);
xc=width/2;
yc=height/2;
}
void mydraw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(255,0,0);
test();
glFlush();
glutSwapBuffers();
}
void onkey(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
}
}
void OnSize(int width,int height)
{
glViewport(0, 0 , (GLsizei)width,(GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,(GLdouble)width,0, (GLdouble)height);
}
void main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("hello");
myInit();
glutDisplayFunc(mydraw);
glutReshapeFunc(OnSize);
glutKeyboardFunc(onkey);
glutMainLoop();
}