//2Dfigures // examples of two dimensional figures // pentagon // multiple pentagon // pentagram // 19-gram // 7-rosette // 21-rossete // 10-simplepetal bloom // r(theta) = a sin(num theta) ....0 < theta < 2 Pi // num=10 // 10*3-simplepetal bloom // r(theta) = a sin(num1/num2 theta) ....0 < theta < 2 num2 Pi // num1=10, num2=3 // 3-petal bloom // r(theta) = a r0 (1 + sin(num theta)) ....0 < theta < 2 Pi // r0 = 0.75 + 0.25 sin(3 num theta) // num=3 // 8-petal bloom // r(theta) = a r0 (1 + sin(num theta)) ....0 < theta < 2 Pi // r0 = 0.75 + 0.25 sin(3 num theta) // num=8 // T. Kosaka CS TNCT 2001 #include #include #include #include "drawShape2D.h" #ifndef M_PI #define M_PI 3.141592653589793 #endif void userdraw(void); void display(void) { glClear( GL_COLOR_BUFFER_BIT); userdraw(); glutSwapBuffers(); } ////////////////////////////////////////////////////////////////// void drawcharX(float x,float y) //文字 x の表示 { drawLine(x,y,x+10,y+12);drawLine(x,y+12,x+10,y); } void drawcharY(float x,float y) //文字 y の表示 { drawLine(x+5,y,x+5,y+7);drawLine(x,y+12,x+5,y+7);drawLine(x+10,y+12,x+5,y+7); } void drawAxes(void) // xy軸の表示 { drawLine(-310,0,310,0);drawLine(310,0,300,5);drawLine(310,0,300,-5); drawcharX(300,-20); drawLine(0,-230,0,230);drawLine(0,230,5,220);drawLine(0,230,-5,220); drawcharY(-20,220); } ////////////////////////////////////////////////////////////////// void userdraw(void) { static int tick=0; color_t white={1.,1.,1.}; color_t cyan={0.,1.,1.}; int disp=(tick/100)%11; point2D_t shape[4000]; int i,j; setColor(white); drawAxes(); setColor(cyan); switch(disp) { case 0: // pentagon glutSetWindowTitle("2Dfigures - pentagon"); for (i=0;i<5;i++) { float theta=M_PI*(2.*i/5+0.5); shape[i].x=200.*cos(theta); shape[i].y=200.*sin(theta); } drawPolygon(shape,5); break; case 1: // multiple pentagon glutSetWindowTitle("2Dfigures - multiple pentagon"); for (j=0;j<41;j++) { for (i=0;i<5;i++) { float theta=M_PI*(2.*i/5+0.5+j*.01); float a=20.+5*j; shape[i].x=a*cos(theta); shape[i].y=a*sin(theta); } drawPolygon(shape,5); } break; case 2: // pentagram glutSetWindowTitle("2Dfigures - pentagram"); for (i=0;i<5;i++) { float theta=M_PI*(4.*i/5+0.5); shape[i].x=200.*cos(theta); shape[i].y=200.*sin(theta); } drawPolygon(shape,5); break; case 3: // heptagram glutSetWindowTitle("2Dfigures - heptagram"); for (i=0;i<7;i++) { float theta=M_PI*(6.*i/7+0.5); shape[i].x=200.*cos(theta); shape[i].y=200.*sin(theta); } drawPolygon(shape,7); break; case 4: // 19-gram glutSetWindowTitle("2Dfigures - 19-gram"); for (i=0;i<19;i++) { float theta=M_PI*(18.*i/19+0.5); shape[i].x=200.*cos(theta); shape[i].y=200.*sin(theta); } drawPolygon(shape,19); break; case 5: // 7-rosette glutSetWindowTitle("2Dfigures - 7-rosette"); for (i=0;i<7;i++) { float theta=M_PI*(2.*i/7+0.5); shape[i].x=200.*cos(theta); shape[i].y=200.*sin(theta); } for (i=0;i<6;i++) for(j=i+1;j<7;j++) drawLine(shape[i],shape[j]); break; case 6: // 21-rossete glutSetWindowTitle("2Dfigures - 21-rossete"); for (i=0;i<21;i++) { float theta=M_PI*(2.*i/21+0.5); shape[i].x=200.*cos(theta); shape[i].y=200.*sin(theta); } for (i=0;i<20;i++) for(j=i+1;j<21;j++) drawLine(shape[i],shape[j]); break; case 7: // 10-simple petal bloom glutSetWindowTitle("2Dfigures - 10-simple petal bloom"); for (i=0;i<4000;i++) { int num=10; float theta=2*M_PI*i/4000; float r=200*sin(num*theta); shape[i].x=r*cos(theta); shape[i].y=r*sin(theta); } drawPolygon(shape,4000); break; case 8: // 10*3-simple petal bloom glutSetWindowTitle("2Dfigures - 10*3-simple petal bloom"); for (i=0;i<4000;i++) { int num1=10; int num2=3; float theta=2*M_PI*i/4000*num2; float r=200*sin((float)num1/(float)num2*theta); shape[i].x=r*cos(theta); shape[i].y=r*sin(theta); } drawPolygon(shape,4000); break; case 9: // 3-petal bloom glutSetWindowTitle("2Dfigures - 3-petal bloom"); for (i=0;i<4000;i++) { int num=3; float theta=2*M_PI*i/4000; float r0=0.75+0.25*sin(3*theta*num); float r=100.*r0*(1+sin(theta*num)); shape[i].x=r*cos(theta); shape[i].y=r*sin(theta); } drawPolygon(shape,4000); break; case 10: // 8-petal bloom glutSetWindowTitle("2Dfigures - 8-petal bloom"); for (i=0;i<4000;i++) { int num=8; float theta=2*M_PI*i/4000; float r0=0.75+0.25*sin(3*theta*num); float r=100.*r0*(1+sin(theta*num)); shape[i].x=r*cos(theta); shape[i].y=r*sin(theta); } drawPolygon(shape,4000); break; default: break; } tick++; } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow ("2Dfigures"); glClearColor(0.0, 0.0, 0.0, 0.0); gluOrtho2D(-320., 320., -240.0, 240.0); // Define the dimensions of the Orthographic Viewing Volume glutIdleFunc(display); // idle event call back glutDisplayFunc(display); glutMainLoop(); return 0; }