/* * epicycloid.c */ #include #include #include "glut.h" typedef float vec2[2]; void vadd(vec2 c, vec2 a, vec2 b) { c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; } void smul(vec2 b, float s, vec2 a) { b[0] = s * a[0]; b[1] = s * a[1]; } float a, b; float N; void setup(void) { gluOrtho2D(-1.0, 1.0, -1.0, 1.0); } void circle(float t, vec2 v) { float phi = 2*M_PI*t; v[0] = cos(phi); v[1] = sin(phi); } void epicycloid(float t, vec2 v) { float phi = 2*M_PI*t; v[0] = (a+b)*cos(phi) - cos(((a+b)/b)*phi); v[1] = (a+b)*sin(phi) - sin(((a+b)/b)*phi); } void display(void) { /* clear background */ glClearColor(0.95f, 0.95f, 0.95f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // circle of radius b rolling on a circle of radius a glColor3f(1.0f, 0.25, 0.25f); glLineWidth(2.0f); glBegin(GL_LINE_LOOP); float t; for( t = 0.0; t < 10.0; t += 10.0/N ) { vec2 u, v, w; circle(t, u); smul(v, a+b, u); circle((a/b+1)*t, u); smul(w, -b, u); vadd(u, v, w); glVertex2fv(u); //epicycloid(t,v); //glVertex2fv(v); } glEnd(); glFlush(); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(512,512); glutInitWindowPosition(32,32); glutCreateWindow("rect"); a = 0.25; b = a/3; N = 360*10; if( argc > 1 ) { if( argc > 2 ) { a = atof(argv[1]); b = atof(argv[2]); } if( argc > 3 ) { N = atoi(argv[3]); } } printf("a=%g, b=%g, N=%g\n", a, b, N); setup(); glutDisplayFunc(display); glutMainLoop(); }