題名の通りのプログラムを書いてみて、とりあえず円1つで正しく動くかどうか、円をひとつ(200, 0)を軸にして45°回転させて試してみたところ、1回めのプログラムの実行についてはうまくいったのですが、2回目以降実行すると円が45°ではなく何故か90°回転してしまいます。
環境はLinux、コンパイルはgcc test.c -lglut -lGLU -lGL -lm
で行い、./a.outを複数回実行すると2回目以降に90°回転してしまいます。
何故このような現象が発生しているのか見当もつきません。
どなたか教えてくださると助かります。
c
1コード 2#include<stdio.h> 3#include<stdlib.h> 4#include<math.h> 5#include <GL/glut.h> 6#define r 30//円の半径 7 8int num = 0;//円の数 9double center[100][2]; 10 11 12void rotation(double *c, double theta,double x, double y){//全体を(x,y)を軸にthetaだけ回転させる 13 double wx,wy; 14 wx = (c[0]-x)*cos(theta) - (c[1]-y)*sin(theta); 15 wy = (c[0]-x)*sin(theta) + (c[1]-y)*cos(theta); 16 c[0] = wx+x; 17 c[1] = wy+y; 18} 19 20 21 22void drawFigure(double center[2]){ 23 double x,y; 24 glBegin(GL_POLYGON); 25 glColor3d(0.0,0.0,1.0); 26 for(int i = 0; i < 50; i++){//円の描画 27 x = r*cos(2.0*M_PI*((double)i/50)); 28 y = r*sin(2.0*M_PI*((double)i/50)); 29 glVertex2d(x+center[0],y+center[1]); 30 } 31 glEnd(); 32} 33 34void display(void){ 35 glClear(GL_COLOR_BUFFER_BIT); 36 glBegin(GL_POINTS); 37 glVertex2d(200,0); 38 glEnd(); 39 glBegin(GL_POINTS); 40 glVertex2d(0,0); 41 glEnd(); 42 rotation(center[0], M_PI/4,200,0); 43 for(int i = 0; i < num; i++){ 44 drawFigure(center[i]); 45 } 46 glFlush(); 47} 48 49void resize(int w, int h)// ワールド座標系とマウスの座標系を一致させる 50{ 51 glViewport(0, 0, w, h); 52 glLoadIdentity(); 53 glOrtho(-w, w, -h , h , -1.0, 1.0); 54} 55 56void init(){ 57 glClearColor(1.0,1.0,1.0,1.0); 58 glColor3d( 0.0, 0.0, 0.0 ); 59 glPointSize( 4.0 ); 60 61} 62 63int main(int argc, char** argv) 64{ 65 center[0][0] = 0.0; 66 center[0][1] = 0.0; 67 num++; 68 69 glutInitWindowSize(800, 800); 70 glutInitWindowPosition(10, 10); 71 glutInit(&argc, argv); 72 glutInitDisplayMode(GLUT_RGB); 73 glutCreateWindow(argv[0]); 74 glutDisplayFunc(display); 75 glutReshapeFunc(resize); 76 init(); 77 glutMainLoop(); 78 return 0; 79} 80
あなたの回答
tips
プレビュー