#include <windows.h>
//#include <graphics.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <math.h>
#include <iostream.h>
#include <stdio.h>
#include <malloc.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glaux.lib")
#define NULL 0
#define MAXSIZE 10000
int fill_color=RGB(1.0,1.0,0.0);
int boundary_color=RGB(1.0,0.0,0.0);
struct node{
float x,y;
int color;
}a[MAXSIZE];
int top=0; //栈顶指针
float m=300.0f; //圆的半径及圆心
float n=300.0f;
float radiu=200.0f;
void draw(float x,float y) //画点
{
glColor3f(1.0,1.0,0.0); //与boundary_color一致
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glFlush();
}
void pushpixel(float x,float y)
{
int i;
for (i=0;i<top;i++)
if (x==a[top].x && y==a[top].y) return; //已在栈中
a[top].x=x;
a[top].y=y;
if (x*x+y*y==radiu*radiu) a[top].color=boundary_color; //边界点
else a[top].color=-1; //未被画过
top++;
}
struct node poppixel()
{
struct node p;
p.x=a[top].x;
p.y=a[top].y;
p.color=a[top].color;
top--;
return(p);
}
void seedfilling(float x,float y)
{
struct node p;
int c;
if ((x-m)*(x-m)+(y-n)*(y-n)<=radiu*radiu)
pushpixel(x,y);/*当前点压栈*/
while (top!=-1)
{
p=poppixel();
c=p.color;
if ((c!=boundary_color) && (c!=fill_color)) /*如果颜色为边界色则不填充*/
{
draw(p.x,p.y);/*当前点出栈,画点*/
if ((x+1-m)*(x-m)+(y-n)*(y-n)<=radiu*radiu) seedfilling(x+1,y);
if ((x-1-m)*(x-m)+(y-n)*(y-n)<=radiu*radiu) seedfilling(x-1,y);
if ((x-m)*(x-m)+(y+1-n)*(y+1-n)<=radiu*radiu) seedfilling(x,y+1);
if ((x-m)*(x-m)+(y-1-n)*(y-1-n)<=radiu*radiu) seedfilling(x,y-1);
}
}
}
void CALLBACK fillseed(void)
{
glClear(GL_COLOR_BUFFER_BIT);
seedfilling(m,n);
}
void main()
{
auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
auxInitPosition(50,50,700,600);
auxInitWindow("An Example of OpenGL");
auxMainLoop(fillseed);
}