Created
December 18, 2009 02:08
-
-
Save onodes/259219 to your computer and use it in GitHub Desktop.
This file contains 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<GLUT/glut.h> | |
#include<stdio.h> | |
#include<time.h> | |
#include<math.h> | |
#define WIDTH 1024 | |
#define HEIGHT 768 | |
#define PART 100 | |
#define MONITW 362.0 | |
#define MONITH 271.0 | |
#define D 1000.0 | |
void square(); | |
void circle(); | |
void circle_loop(); | |
float rnd; | |
float deg2pixh(float degree); | |
float deg2pixw(float degree); | |
int select(int x, int y); | |
int square_width; | |
int square_height; | |
int rest_width; | |
int rest_height; | |
int thx; | |
int thy; | |
int col[9]; | |
void initialize(){ | |
int i; | |
square_width = deg2pixw(5.0); | |
square_height = deg2pixh(5.0); | |
rest_width = (WIDTH-square_width)/2.0; | |
rest_height = (HEIGHT-square_height)/2.0; | |
thx = (int)(square_width/4); | |
thy = (int)(square_height/4); | |
for(i=1;i<=8;i++){ | |
col[i] = 0; | |
} | |
} | |
void color(int col){ | |
switch(col){ | |
case 0: | |
glColor3f(0.0,0.0,0.0); | |
break; | |
case 1: | |
glColor3f(1.0,0.0,0.0); | |
break; | |
case 2: | |
glColor3f(0.0,1.0,0.0); | |
break; | |
case 3: | |
glColor3f(0.0,0.0,1.0); | |
break; | |
case 4: | |
glColor3f(1.0,1.0,0.0); | |
break; | |
case 5: | |
glColor3f(1.0,1.0,1.0); | |
break; | |
default: | |
glColor3f(0.0,0.0,0.0); | |
break; | |
} | |
glFlush(); | |
} | |
int select(int x, int y){ | |
if(rest_height<=y && rest_width<=x && rest_height+square_height >= y && rest_width+square_width>= x){ | |
if(rest_height<=y && rest_height+square_height/3 >=y){ | |
printf("point1"); | |
if(rest_width<=x && rest_width+square_width/3 >=x){ | |
return 1; | |
} | |
else if(((rest_width+square_width)>=x) && (rest_width+(square_width*(2.0/3.0))<=x)) { | |
return 3; | |
} | |
else{ | |
return 2; | |
} | |
} | |
else if(rest_height+square_height/3 <= y && rest_height+square_height*(2.0/3.0) >=y){ | |
printf("point2"); | |
if(rest_width<=x && rest_width+square_width/3 >=x){ | |
return 4; | |
} | |
else if((rest_width+square_width)>=x && rest_width+square_width*(2.0/3.0)<=x ){ | |
return 5; | |
} | |
else{ | |
return 0; | |
} | |
} | |
else if(rest_height+square_height>= y && rest_height+square_height*(2.0/3.0) <=y){ | |
printf("point3"); | |
if(rest_width<=x && rest_width+square_width/3 >=x){ | |
return 6; | |
} | |
else if((rest_width+square_width)>=x && rest_width+square_width*(2.0/3.0)<=x){ | |
return 8; | |
} | |
else{ | |
return 7; | |
} | |
} | |
} | |
else{ | |
return 0; | |
} | |
} | |
void circle2(int num){ | |
int i,j; | |
switch(num){ | |
case 1: | |
i=1;j=1; | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 2: | |
i=2;j=1; | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 3: | |
i=3;j=1; | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 4: | |
i=1;j=2; | |
//circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 5: | |
i=3;j=2; | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 6: | |
i=1;j=3; | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 7: | |
i=2;j=3; | |
//circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
case 8: | |
i=3;j=3; | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
break; | |
default: | |
break; | |
} | |
if(col[num]==6){ | |
col[num] = 0; | |
} | |
color(col[num]); | |
circle(rest_width,rest_height,i*thx,j*thy); | |
} | |
float deg2pixh(float degree){ //任意のdegree(角度)をピクセルに変換する関数 | |
float deg1,pix; | |
pix = atan(MONITH/(D*2)); //radian | |
//printf("%f\n",pix); | |
pix = pix * (180.0 / M_PI); //.to_degree | |
//printf("%f\n",pix); | |
deg1 = (HEIGHT/2.0)/pix; //1deg の pixel | |
//printf("%f\n",deg1); | |
return deg1*degree; | |
} | |
float deg2pixw(float degree){ //任意のdegree(角度)をピクセルに変換する関数 | |
float deg1,pix; | |
pix = atan(MONITW/(D*2)); //radian | |
//printf("%f\n",pix); | |
pix = pix * (180.0 / M_PI); //.to_degree | |
//printf("%f\n",pix); | |
deg1 = (WIDTH/2.0)/pix; //1deg の pixel | |
// printf("%f\n",deg1); | |
return deg1*degree; | |
} | |
void display(){ | |
glClear(GL_COLOR_BUFFER_BIT); | |
square(); | |
circle_loop(); | |
glFlush(); | |
} | |
void init(){ | |
glClearColor(0.0,0.0,0.0,0.0); | |
} | |
void resize(int w, int h){ | |
glViewport(0, 0, w, h); | |
glLoadIdentity(); | |
glOrtho(-0.5,(GLdouble)w-0.5,(GLdouble)h-0.5,-0.5,-1,1); | |
} | |
void circle_loop(){ | |
int i,j; | |
// int start_width = WIDTH*0.1; | |
// int start_height = HEIGHT *0.1; | |
//for(i=1;i<=3;i++){ | |
// for(j=1;j<=3;j++){ | |
// if(i==2 && j==2){continue;} | |
// printf("%d %d",i*thx,j*thy); | |
// circle(rest_width,rest_height,i*thx,j*thy); | |
for(i=1;i<=8;i++){ | |
circle2(i); | |
} | |
} | |
float randomF(){ | |
float a; | |
a = (float)rand() / 32768.0; | |
return a; | |
} | |
void circle(int start_width,int start_height ,int thx,int thy){ | |
int i, n = PART; | |
float x, y, r = deg2pixw(0.5); | |
double rate; | |
glBegin(GL_POLYGON); // ポリゴンの描画 | |
// 円を描画 | |
for (i = 0; i < n; i++) { | |
// 座標を計算 | |
rate = (double)i / n; | |
x = r * cos(2.0 * M_PI * rate)+thx + start_width; | |
y = r * sin(2.0 * M_PI * rate)+thy + start_height; | |
glVertex3f(x, y, 0.0); // 頂点座標を指定 | |
} | |
glEnd(); // ポリゴンの描画終了 | |
glFlush(); // OpenGLのコマンドを強制的に実行(描画終了) | |
} | |
void dot(int x, int y){ | |
glColor3d(0.0,0.0,1.0); | |
glBegin(GL_QUADS); | |
glVertex2d(x-4,y-4); | |
glVertex2d(x+4,y-4); | |
glVertex2d(x+4,y+4); | |
glVertex2d(x-4,y+4); | |
glEnd(); | |
glFlush(); | |
} | |
void square(){ | |
glClear(GL_COLOR_BUFFER_BIT); | |
glColor3f(1.0, 1.0, 1.0); | |
glBegin(GL_QUADS); | |
glVertex2d(rest_width, rest_height); | |
glVertex2d(rest_width+square_width,rest_height); | |
glVertex2d(rest_width+square_width,rest_height+square_height); | |
glVertex2d(rest_width,rest_height+square_height); | |
glEnd(); | |
glFlush(); | |
} | |
void mouse(int button, int state, int x, int y) | |
{ | |
switch (button) { | |
case GLUT_LEFT_BUTTON: | |
printf("left"); | |
break; | |
case GLUT_MIDDLE_BUTTON: | |
printf("middle"); | |
break; | |
case GLUT_RIGHT_BUTTON: | |
printf("right"); | |
break; | |
default: | |
break; | |
} | |
printf(" button is "); | |
switch (state) { | |
case GLUT_UP: | |
printf("up"); | |
col[select(x,y)]++; | |
break; | |
case GLUT_DOWN: | |
printf("down"); | |
// color(); | |
break; | |
default: | |
break; | |
} | |
// color(); | |
//dot(x,y); | |
glFlush(); | |
glutPostRedisplay(); | |
circle2(select(x,y)); | |
printf("Select => %d\n",select(x,y)); | |
printf(" at (%d, %d)\n", x, y); | |
} | |
int main(int argc, char *argv[]){ | |
//deg2pixw(5); | |
initialize(); | |
srand(time(NULL)); | |
glutInitWindowPosition(100,100); | |
glutInitWindowSize(WIDTH,HEIGHT); | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_RGBA); | |
glutCreateWindow(argv[0]); | |
glutDisplayFunc(display); | |
glutReshapeFunc(resize); | |
glutMouseFunc(mouse); | |
init(); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment