#include <iostream>
#include <cstdio>
#include <math.h>
#include <GL/glut.h>
#include <windows.h>
#include <string.h>
//const double PI = 3.1415;
//const int N = 100;
//const int XMAX = 24;
//const int WIDTH = 25;
//const int POINT_R = 10;
using namespace std;
#define PI 3.1415
#define N 100 //¶à±ßÐÎÔ²±ßÊý
#define XMAX 24 //ÿÐÐÁиñÊý
#define WIDTH 25 //ÿ¸ñ´óС
#define POINT_R 10 //µã°ë¾¶
int point[XMAX][XMAX] = {0}; //µãÊôÐÔ
void Init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, (double)XMAX * WIDTH, 0.0, (double)XMAX * WIDTH); //´°¿Ú
glPointSize(10.0);
}
void Draw(int x, int y, int z) { //ÎÒµÄxyÊǵã×ø±ê[0,24)
glClear(GL_COLOR_BUFFER_BIT);
if (z == 1) { //ÈôΪ±ß½ç
glColor3f(1, 0, 0);
}
else if (z == 2) { //ÈôΪÄÚ²¿
glColor3f(0, 1, 0);
}
else glColor3f(0, 0, 1);//·ÀÖ¹³öÏÖÉñÆæµÄÇé¿ö
glBegin(GL_POLYGON); //×öÔ²
for (int i = 0; i <= N; i++) {
glVertex2f(x + POINT_R * cos(2 * PI / N * i), y + POINT_R * sin(2 * PI / N * i));
}
glEnd();
glPopMatrix();
}
void playM() {
glColor3f(0.0, 0.0, 0.0); //ÉèÖÃÏßÉ«
glBegin(GL_LINES); //»ÆåÅÌ
for (int i = 0; i < XMAX * WIDTH; i += WIDTH) {
glVertex2f(i, 0);
glVertex2f(i, XMAX * WIDTH);
glVertex2f(0, i);
glVertex2f(XMAX * WIDTH, i);
}
glEnd();
for (int i = 0; i < XMAX; i++) {
for (int j = 0; j < XMAX; j++) {
if (point[i][j] == 1) Draw(i * 25 + 12, j * 25 + 12, 1); //»µã
else if (point[i][j] == 2) Draw(i * 25 + 12, j * 25 + 12, 2);
}
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
playM();
glFlush();
}
void dfs(int x, int y) { //°Ëͨ
point[x][y] = 2; display();
Sleep(300);
int dx[3] = { -1,0,1 };
int dy[3] = { -1,0,1 };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int nx = x + dx[i], ny = y + dy[j];
if ((nx>=0 && nx<XMAX && ny>=0 && ny<XMAX) && (point[nx][ny] == 0)) {
dfs(nx, ny);
}
}
}
}
void click(GLint button, GLint action, GLint _x, GLint _y) {
int x = _x / WIDTH;
int y = (XMAX* WIDTH - _y) / WIDTH;
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
if (point[x][y] != 1) point[x][y] = 1;
else point[x][y] = 0;
glutPostRedisplay();
}
else if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN) {
if (point[x][y] == 0) dfs(x, y);
}
}
int main(int argc, char** argv){
memset(point, 0, sizeof(point));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(300, 100);
glutInitWindowSize(XMAX * WIDTH, XMAX * WIDTH);
glutCreateWindow("result");
Init();
glutDisplayFunc(display); //»Øµ÷º¯Êý
glutMouseFunc(click); //Êó±ê¶¯×÷
glutMainLoop(); //³ÖÐøÏÔʾͼÏñ£¬µ±´°¿Ú¸Ä±äʱ»áÖØÐ»æÖÆÍ¼ÐÎ
return 0;
}