/* coil.c */ #include #define PI M_PI #define MAX_JUMP 0.025 double theta = PI/6; double scale = 100; double x_origin = 2.5; double y_origin = 2.0; double t_start = 0; double t_end = 3; /* parameteric eqns */ double X(t) double t; { return cos(2*PI*t); } double Y(t) double t; { return sin(2*PI*t); } double Z(t) double t; { return t; } /* transformation 3-space to user space */ transform (x, y, z) double x, y, z; { double X = y - x * cos( theta ); double Y = z - x * sin( theta ); printf ( "%f %f lineto\n", X, Y ); } start_graph (x, y, z) double x, y, z; { double X = y - x * cos( theta ); double Y = z - x * sin( theta ); printf ( "newpath\n" ); printf ( "%f %f moveto\n", X, Y ); } /* draw the parametric curve */ draw ( t_1, x_1, y_1, z_1, t_2, x_2, y_2, z_2 ) double t_1, x_1, y_1, z_1, t_2, x_2, y_2, z_2; { double t_mid = (t_2 + t_1)/2; double x_mid = X(t_mid); double y_mid = Y(t_mid); double z_mid = Z(t_mid); double dist_sq = (t_2 - t_1) * (t_2 - t_1) + (x_2 - x_mid) * (x_2 - x_mid) + (x_1 - x_mid) * (x_1 - x_mid) + (y_2 - y_mid) * (y_2 - y_mid) + (y_1 - y_mid) * (y_1 - y_mid) + (z_2 - z_mid) * (z_2 - z_mid) + (z_1 - z_mid) * (z_1 - z_mid); if ( dist_sq < MAX_JUMP) { transform ( x_2, y_2, z_2 ); } else { draw ( t_1, x_1, y_1, z_1, t_mid, x_mid, y_mid, z_mid ); draw ( t_mid, x_mid, y_mid, z_mid, t_2, x_2, y_2, z_2 ); } } /* main routine */ main() { double t_1 = t_start; double t_2 = t_end; double x_1 = X(t_1); double y_1 = Y(t_1); double z_1 = Z(t_1); double x_2 = X(t_2); double y_2 = Y(t_2); double z_2 = Z(t_2); printf ( "%%!-PostScript\n" ); printf ( "%%%% The Good Doctors Coil.c\n" ); printf ( "%%%% not EPS\n" ); printf ( "%f %f scale\n", scale, scale ); printf ( "%f %f translate\n", x_origin, y_origin ); printf ( "0 setlinewidth\n" ); printf ( "0 0 moveto\n" ); transform (1.5, 0.0, 0.0 ); printf ( "stroke\n" ); printf ( "0 0 moveto\n" ); transform (0.0, 1.5, 0.0 ); printf ( "stroke\n" ); printf ( "0 0 moveto\n" ); transform (0.0, 0.0, 3.5 ); printf ( "stroke\n" ); start_graph ( x_1, y_1, z_1 ); draw ( t_1, x_1, y_1, z_1, t_2, x_2, y_2, z_2 ); printf ( "stroke\nshowpage\n" ); }