#include #include #include #include #include #include #define DIM 41 #define G ((1.73205)/2)*((Y(s,t)*Y(s,t) - Z(s,t)*Z(s,t)) + Z(s,t)*X(s,t)*(Z(s,t)*Z(s,t) - X(s,t)*X(s,t)) + X(s,t)*Y(s,t)*(Y(s,t)*Y(s,t) - X(s,t)*X(s,t))) #define F ((2*X(s,t)*X(s,t) - Y(s,t)*Y(s,t) - Z(s,t)*Z(s,t)) + 2*Y(s,t)*Z(s,t)*(Y(s,t)*Y(s,t) - Z(s,t)*Z(s,t)) + Z(s,t)*X(s,t)*(X(s,t)*X(s,t) - Z(s,t)*Z(s,t)) + X(s,t)*Y(s,t)*(Y(s,t)*Y(s,t) - X(s,t)*X(s,t)))/2 #define H (X(s,t) + Y(s,t) + Z(s,t))*((X(s,t) + Y(s,t) + Z(s,t))*(X(s,t) + Y(s,t) + Z(s,t))*(X(s,t) + Y(s,t) + Z(s,t)) +4*(Y(s,t)-X(s,t))*(Z(s,t)-Y(s,t))*(X(s,t)-Z(s,t)))/8 typedef long Window; /* Global variables */ long xorigin, yorigin, xwidth, ywidth; float minx, miny, maxx, maxy; Window ParamWindow; float c1, c2, Pi, ds, dt, limitx, limity; float sline1[2], sline2[2], tline1[2], tline2[2], ld[2], lu[2], ru[2], rd[2]; int r, b, gr; /* declaration of functions */ float X (float s, float t); float Y (float s, float t); float Z (float s, float t); void AnimateBoys(); void AnimatePlane(); void main() { /* variables are declared and/or initialized */ Device dev; short val; char title[38]; float s, t; float incrx, incry; int inc_size, ok, xok,yok; Pi = 4.0*atan(1.0); ok = 1; xok = 1; yok = 1; inc_size = 16; incrx = Pi/32.0; incry = Pi/8.0; limitx = 0; limity = 0; /* calls the TCL instructions window */ system("animate_instructions.tcl &"); /*this is all for the graphics window */ foreground(); keepaspect(1, 2); minx = 0; maxx = Pi/2.0; miny = 0; maxy = 2.0*Pi; sprintf(title,"(%f,%f) (%f,%f)", minx, miny, maxx, maxy); ParamWindow = winopen(title); winset(ParamWindow); gconfig(); ortho2(minx, maxx, miny, maxy); color(WHITE); clear(); color(BLACK); /* This is for the devices */ qdevice(SKEY); qdevice(FKEY); qdevice(PKEY); qdevice(GKEY); qdevice(ESCKEY); qdevice(XKEY); qdevice(YKEY); qdevice(OKEY); /* This is an infinite loop that checks for input each time through then continues on to animate Boy's Surface */ while (1) { while (qtest()) { dev = qread(&val); switch(dev){ /* S slows it down, F speeds it up */ case SKEY: inc_size++; break; case FKEY: inc_size--; break; /* X stops x motion, Y stops y */ case XKEY: xok = 0; break; case YKEY: yok = 0; break; /* P pauses all motion */ case PKEY: ok = 0; break; /* G restarts all stopped motion */ case GKEY: ok = 1; yok = 1; xok = 1; break; /* ESC exits */ case ESCKEY: gexit(); exit(0); break; } /* this checks to make sure the inc_size hasn't become 0 or negative */ if(inc_size > 0) { /* these keep the motion going in the same direction */ if (incrx > 0) { incrx = (maxx- minx)/inc_size; } else { incrx = - (maxx- minx)/inc_size; } if (incry > 0) { incry = (maxy - miny)/inc_size; } else { incry = - (maxy - miny)/inc_size; } } } /* This switches direction if the maximum or minimum value of the variable has been reached */ if(limitx > Pi/2 || limitx < 0) { incrx = incrx*(-1); } if (limity > 2*Pi || limity < 0) { incry = incry*(-1); } /* This causes motion depending on input params */ if (ok == 1) { if (xok == 1) { limitx += incrx; } if (yok == 1) { limity += incry; } AnimateBoys(); AnimatePlane(); } } } float X (float s, float t) { return( 0.577295*cos(s) - 0.577295*cos(t)*sin(s) - 0.39504259905*sin(s)*sin(t) ); } float Y (float s, float t) { return( 0.577295*cos(s) + 0.577295*cos(t)*sin(s) - 0.333365118676*sin(s)*sin(t) ); } float Z (float s, float t) { return( 0.57746*cos(s) + 0.728199303743*sin(s)*sin(t) ); } /* animates Boy's Surface in the Geomview window */ void AnimateBoys() { float s, t; int i, j; ds = limitx/(float) (DIM - 1); dt = limity/(float) (DIM - 1); printf("(bbox-draw \"world\" off)\n"); printf("(normalization \"world\" none)\n"); printf("(geometry boys { : cake }) \n"); fflush(stdout); printf("(read geometry { define cake \n"); printf("CMESH \n"); printf("%1d %1d \n", DIM, DIM); for(i = 0, t = 0; i < DIM; i++, t += dt) { for(j = 0, s = 0; j < DIM; j++, s += ds) { if (t <= (2.0/3.0)*Pi) { printf("%f %f %f 1 0 0 1\n", G, F, H); } else if (t > (2.0/3.0)*Pi && t <= (4.0/3.0)*Pi) { printf("%f %f %f 0 0 1 1\n", G, F, H); } else if (t > (4.0/3.0)*Pi) { printf("%f %f %f 1 1 0 1\n", G, F, H); } } } printf("}) \n"); fflush(stdout); } /* animates the plane in the C window */ void AnimatePlane() { char col; sline1[0] = limitx; sline1[1] = 0; sline2[0] = limitx; sline2[1] = 2.0*Pi; tline1[0] = 0; tline1[1] = limity; tline2[0] = Pi/2.0; tline2[1] = limity; color(WHITE); clear(); swapbuffers(); color(BLACK); bgnline(); v2f(sline1); v2f(sline2); endline(); bgnline(); v2f(tline1); v2f(tline2); endline(); if(r == 1 && b == 0 && gr == 0) { col = RED; } else if(b == 1 && r == 0 && gr == 0) { col = BLUE; } else if(gr == 1 && r == 1 && b == 0) { col = YELLOW; } color(col); ld[0] = 0; ld[1] = 0; lu[0] = 0; lu[1] = limity; ru[0] = limitx; ru[1] = limity; rd[0] = limitx; rd[1] = 0; bgnpolygon(); v2f(ld); v2f(lu); v2f(ru); v2f(rd); endpolygon(); }