Created
April 9, 2017 12:28
-
-
Save splitline/dbefe22a5c7f95548e9b1b315bc05453 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <time.h> | |
#include <GLUT/glut.h> | |
int g[100][100], used[100][100]; | |
int dx[] = {0,0,1,-1}; | |
int dy[] = {1,-1,0,0}; | |
int n=10; // 迷宮大小 | |
float d=2*n+1, block=1/d; | |
bool ret[100][100]; | |
//maze algorithm ref. http://mypaper.pchome.com.tw/zerojudge/post/1324488901 | |
void dfs(int x, int y, int px, int py) { | |
if(x < 0 || y < 0 || x >= n || y >= n) return; | |
if(used[x][y]) return; | |
used[x][y] = 1; | |
if(px >= 0) | |
ret[((x*2+1)+(px*2+1))/2][((y*2+1)+(py*2+1))/2] = 1; | |
ret[x*2+1][y*2+1] = 1; | |
int cnt = 0, dir = 0; | |
if(x == n-1 && y == n-1) return; | |
while(cnt < 10) { | |
dir = rand()%4; | |
dfs(x+dx[dir], y+dy[dir], x, y); | |
cnt++; | |
} | |
} | |
void init(void){ | |
glClearColor(0.0,0.0,0.0,0.0); | |
glShadeModel(GL_FLAT); | |
glLineWidth(2.0); | |
glLineStipple(1,0xCCFC); | |
glEnable(GL_LINE_STIPPLE); | |
} | |
void drawMaze(){ | |
for(float i = 0; i < d; i++) { | |
for(float j = 0; j < d; j++) { | |
if(ret[(int)i][(int)j]){ | |
glColor3f(1.0f,1.0f,1.0f); | |
glBegin(GL_POLYGON); | |
GLfloat point1[] = {i/d,j/d,0.0}; | |
GLfloat point2[] = {i/d+block,j/d,0.0}; | |
GLfloat point3[] = {i/d+block,j/d+block,0.0}; | |
GLfloat point4[] = {i/d,j/d+block,0.0}; | |
glVertex3fv(point1); | |
glVertex3fv(point2); | |
glVertex3fv(point3); | |
glVertex3fv(point4); | |
glEnd(); | |
} | |
} | |
} | |
} | |
void display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT); | |
drawMaze(); | |
glutSwapBuffers(); | |
} | |
void reshape(int w,int h) { | |
glViewport(0,0,(GLsizei)w,(GLsizei)h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluOrtho2D(0.0f,1.0f,0.0f,1.0f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
} | |
int main(int argc,char**argv) | |
{ | |
srand(time(NULL)); | |
memset(g, 0, sizeof(g)); | |
memset(used, 0, sizeof(used)); | |
memset(ret, 0, sizeof(ret)); | |
dfs(0,0, -1,-1); | |
glutInit(&argc,argv); | |
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); | |
glutInitWindowPosition(400,100); | |
glutInitWindowSize(500,500); | |
glutCreateWindow("Superrrrr Maze"); | |
init(); | |
glutDisplayFunc(display); | |
glutReshapeFunc(reshape); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment