viewing paste Unknown #51336 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#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;
}
 
Viewed 578 times, submitted by Guest.