#define UNIX 1 /* Change to 0 if compiling on non-unix system. */ #include #include #if(UNIX) #include #include #endif typedef struct { double x_coord; double y_coord; } point; point return_point; #define DATAFILE "data" #define ERRORFILE stderr main() { int no_input_errors = 1; int ok_to_draw_line = 0; int N,Q; char in_filename[50], out_filename[50]; char text[100]; char current_line[100]; FILE *fp_in; int fclose(); point lower_left, upper_right, p, q, ttimes(); point r, s; double black=0.0, white=1.0; double dot_size; int initial_point; fp_in = stdin; strcpy(in_filename,"standard input"); lower_left.x_coord = 0.0; lower_left.y_coord = 0.0; upper_right.x_coord = 1.0; upper_right.y_coord = 1.0; begin_drawing(); N=0; while (no_input_errors) { q = p; if (N>0) { Q = fscanf(fp_in,"\n"); } Q = fscanf(fp_in,"%[^\n]",current_line); if (Q <= 0) {error_1(N,in_filename); break;} switch(current_line[0]) { case '%'/*comment*/: fprintf(ERRORFILE,"Line %d treated as comment.\n",N+1) ; Q = sscanf(current_line,"%*c%[^\n]",text); comment_print(text); break; case '\\'/*esape*/: Q = sscanf(current_line,"\\%[^\n]",text); escape_print(text); break; case 'N'/*Name of picture*/: Q = sscanf(current_line,"N%[^\n]",text); nameprint(text); break; case 't'/*text, small*/: Q = sscanf(current_line,"t%[^\n]",text); textprint_small(text,q); break; case 'x'/*text, medium*/: Q = sscanf(current_line,"x%[^\n]",text); textprint_medium(text,q); break; case 'T'/*text, large*/: Q = sscanf(current_line,"T%[^\n]",text); textprint_large(text,q); break; case 'z'/* insert comment in log file */: Q = sscanf(current_line,"z%[^\n]",text); logprint(text); break; case 'r'/*clip region*/: Q = sscanf(current_line,"r%lf%lf%lf%lf", &r.x_coord,&r.y_coord, &s.x_coord,&s.y_coord); if (Q <= 3) {error_r0(N+1,in_filename); no_input_errors=0; break;} if ( (r.x_coord==s.x_coord) || (r.y_coord==s.y_coord)) {error_w1(N+1,in_filename); no_input_errors=0;} r.x_coord = (r.x_coord-lower_left.x_coord) / (upper_right.x_coord-lower_left.x_coord); r.y_coord = (r.y_coord-lower_left.y_coord) / (upper_right.y_coord-lower_left.y_coord); s.x_coord = (s.x_coord-lower_left.x_coord) / (upper_right.x_coord-lower_left.x_coord); s.y_coord = (s.y_coord-lower_left.y_coord) / (upper_right.y_coord-lower_left.y_coord); s.y_coord = 1.0 - s.y_coord; r.y_coord = 1.0 - r.y_coord; set_clip_region(r,s); break; case 'G'/*extremes of gray scale; black then white*/: Q = sscanf(current_line,"G%lf%lf", &black,&white); if (Q <= 1) {error_w0(N+1,in_filename); no_input_errors=0;} if (black==white) {error_w1(N+1,in_filename); no_input_errors=0;} break; case 'w'/*window*/: Q = sscanf(current_line,"w%lf%lf%lf%lf", &lower_left.x_coord,&lower_left.y_coord, &upper_right.x_coord,&upper_right.y_coord); if (Q <= 3) {error_w0(N+1,in_filename); no_input_errors=0;} if ( (lower_left.x_coord==upper_right.x_coord) || (lower_left.y_coord==upper_right.y_coord)) {error_w1(N+1,in_filename); no_input_errors=0;} break; case 'W'/*Width*/: { int width; Q = sscanf(current_line,"W%d",&width); if (Q <= 0) {error_W(N+1,in_filename); no_input_errors=0; break;} set_width(width); } break; case 'C'/*color */: { double red, green, blue; Q = sscanf(current_line,"C%lf %lf %lf", &red,&green,&blue); if (Q <= 2) {error_W(N+1,in_filename); no_input_errors=0; break;} set_colors(red,green,blue); } break; case 'g'/*gray_level*/: { double gray_level; Q = sscanf(current_line,"g%lf",&gray_level); if (Q <= 0) {error_W(N+1,in_filename); no_input_errors=0; break;} gray_level = (gray_level - black)/(white - black); if (gray_level<=0.0) gray_level = 0.0; if (gray_level>=1.0) gray_level = 1.0; set_gray(gray_level); } break; case 'm'/*move*/: ok_to_draw_line = 1; Q = sscanf(current_line,"m%lf%lf",&p.x_coord, &p.y_coord); if (Q <= 1) {error_m(N+1,in_filename); no_input_errors=0; break;} p.x_coord = (p.x_coord-lower_left.x_coord) / (upper_right.x_coord-lower_left.x_coord); p.y_coord = (p.y_coord-lower_left.y_coord) / (upper_right.y_coord-lower_left.y_coord); /*===>> math kludge */ /* move_to(p); DELETED for the version only */ break; case 'l'/*line*/: if (!ok_to_draw_line){error_l1(N+1,in_filename ); no_input_errors=0; break;} Q = sscanf(current_line,"l%lf%lf",&p.x_coord, &p.y_coord); if (Q <= 1) {error_l(N+1,in_filename); no_input_errors=0; break;} p.x_coord = (p.x_coord-lower_left.x_coord) / (upper_right.x_coord-lower_left.x_coord); p.y_coord = (p.y_coord-lower_left.y_coord) / (upper_right.y_coord-lower_left.y_coord); makeline(p,q); break; case 'd'/*dot*/: Q = sscanf(current_line,"d%lf%lf%lf",&p.x_coord, &p.y_coord,&dot_size); if (Q < 3) {error_d(N+1,in_filename); no_input_errors=0; break;} p.x_coord = (p.x_coord-lower_left.x_coord) / (upper_right.x_coord-lower_left.x_coord); p.y_coord = (p.y_coord-lower_left.y_coord) / (upper_right.y_coord-lower_left.y_coord); make_dot(p,dot_size); break; case 'P': /* path (to be filled) */ initial_point = 1; printf("Polygon[{\n"); /* } */ break; case 'L': /* point in path */ Q = sscanf(current_line,"L%lf%lf",&p.x_coord,&p.y_coord); if (Q <= 1) {error_l(N+1,in_filename); no_input_errors=0; break;} p.x_coord = (p.x_coord-lower_left.x_coord) / (upper_right.x_coord-lower_left.x_coord); p.y_coord = (p.y_coord-lower_left.y_coord) / (upper_right.y_coord-lower_left.y_coord); printf("{%4.3f,%4.3f},\n", p.x_coord,p.y_coord); break; case 'q': /* terminate and fill path */ printf("}],\n"); break; case 'Q': /* terminate, fill path, and stroke */ printf("}],\n"); break; default: fprintf(ERRORFILE,"Data of unknown type in line "); fprintf(ERRORFILE,"%d; ignored up to newline.\n",N+1); break; } N++; } fclose(fp_in); end_drawing(); } set_colors(red,green,blue) double red, green, blue; { if ( (red < 0.0) || (green < 0.0) || (blue < 0.0) || (red > 1.0) || (green > 1.0) || (blue > 1.0)) fprintf(ERRORFILE,"Warning: Color value out of range.\n"); printf("RGBColor[%4.3f,%4.3f,%4.3f],\n",red,green,blue); } set_gray(gray_level) double gray_level; { /* printf("}, GrayLevel[%4.3f], {\n"); */ printf("GrayLevel[%4.3f],\n",gray_level); } make_dot(p,dot_size) point p; double dot_size; { fprintf(ERRORFILE,"Warning: no dots available here\n"); } set_clip_region(p,q) point p, q; { fprintf(ERRORFILE,"Warning: no clip region available here\n"); } comment_print(text) char text[]; { printf("(* %s *)\n",text); } escape_print(text) char text[]; { printf("%s",text); printf("\n"); } nameprint(text) char text[]; { point p; p.x_coord = 0.3; p.y_coord = 0.1; textprint_large(text,p); } logprint(text) char text[]; { fprintf(ERRORFILE,"Warning: no logprint available here\n"); } textprint_large(text,q) char text[]; point q; { printf("Text[FontForm[\"%s\",\"Plain\",24],",text); printf("{%4.3f,%4.3f},{-1,0}],\n",q.x_coord,q.y_coord); } textprint_medium(text,q) char text[]; point q; { printf("Text[FontForm[\"%s\",\"Plain\",24],",text); printf("{%4.3f,%4.3f},{-1,0}],\n",q.x_coord,q.y_coord); } textprint_small(text,q) char text[]; point q; { printf("Text[FontForm[\"%s\",\"Plain\",24],",text); printf("{%4.3f,%4.3f},{-1,0}],\n",q.x_coord,q.y_coord); } end_drawing() { printf("}]\nShow[picture]\n"); } begin_drawing() { #if(UNIX) char *getlogin(); #endif printf("(* *)\n"); printf("(* This is a Mathematica file. It makes a picture that *)\n"); printf("(* can be viewed from within Mathematica by doing *)\n"); printf("(* *)\n"); printf("(* << filename *)\n"); printf("(* *)\n"); printf("(* The file creates a graphics object called \"picture,\" *)\n"); printf("(* which can be brought back again (in a given *)\n"); printf("(* Mathematica session) by commanding *)\n"); printf("(* *)\n"); printf("(* Show[picture] *)\n"); printf("(* *)\n"); printf("(* This file was prepared by W. Taylor's translator that*)\n"); printf("(* is found in to_math.c. For a general discussion of *)\n"); printf("(* this sort of translator, see Section 2.3 of Taylor's *)\n"); printf("(* The Geometry of Computer Graphics, published 1991 *)\n"); printf("(* by Wadsworth & Brooks/Cole. This translator is very *)\n"); printf("(* much like the one that is found in Section 2.3.1. *)\n"); printf("(* ISBN 0-534-17100-1 *)\n"); printf("(* *)\n"); printf("(* Mathematica is a registered trademark of *)\n"); printf("(* Wolfram Research, Inc. *)\n"); printf("(* *)\n"); printf("(* For more information about Mathematica, see Stephen *)\n"); printf("(* Wolfram's book of that title, Addison-Wesley, 1988 *)\n"); printf("(* ISBN 0-201-19330-2 *)\n"); printf("(* *)\n"); #if(UNIX) printf("\(* Translation done by: %s *\)\n",getlogin()); #endif include_date_stamp(); printf("SetOptions[Graphics,AspectRatio->1.0]\n"); printf("SetOptions[Graphics,PlotRange->{{0.0,1.0},{0.0,1.0}}]\n"); printf("SetOptions[Graphics,Axes->None]\n"); printf("SetOptions[Graphics,AxesLabel->None]\n"); printf("SetOptions[Graphics,PlotLabel->None]\n"); printf("SetOptions[Graphics,Ticks->None]\n"); printf("SetOptions[Graphics,Framed->True]\n"); printf("(* *)\n"); printf("picture = Graphics[ {\n"); /* printf("Show[ Graphics[ {\n"); */ } set_width(width) int width; { double width_percent; width_percent = .002 *((double)width); printf("Thickness[%4.3f],\n",width_percent); } makeline(p,q) point p, q; { printf("Line[{ {%4.3f,%4.3f}, {%4.3f,%4.3f} }],\n", p.x_coord,p.y_coord,q.x_coord,q.y_coord); } char months[12][20] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; char days[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; include_date_stamp() { char month[20]; char day[20]; #if(UNIX) struct tm *localtime(), *tp; struct timeval tv; struct timezone tz; gettimeofday(&tv, &tz); tp = localtime(&tv.tv_sec); printf("(* the translation was made on *)\n"); printf("(* *)\n"); printf("(* %s, %s %d, 19%d ", days[tp->tm_wday], months[tp->tm_mon], tp->tm_mday, tp->tm_year); if (tp->tm_hour==0) printf(" %d:%d AM *)\n", 12 ,tp->tm_min); if (tp->tm_hour<12) printf(" %d:%d AM *)\n",tp->tm_hour,tp->tm_min); if (tp->tm_hour==12) printf(" %d:%d PM *)\n", 0 ,tp->tm_min); if (tp->tm_hour>12) printf(" %d:%d PM *)\n",tp->tm_hour -12,tp->tm_min); printf("(* *)\n"); #endif } error_1(N,in_filename) int N; char in_filename[]; { fprintf(ERRORFILE,"\nSuccessfully read "); fprintf(ERRORFILE,"%d lines of data from \"%s\"\n",N,in_filename); } error_w1(N) int N; { fprintf(ERRORFILE,"\nWindow error in line %d. Window must have "); fprintf(ERRORFILE,"non-zero",N); fprintf(ERRORFILE," width and height.\nProgram terminated.\n"); } error_r0(N) int N; { fprintf(ERRORFILE,"\nRegion error in line %d. Must give four region", N); fprintf(ERRORFILE," coordinates.\nProgram terminated.\n"); } error_w0(N) int N; { fprintf(ERRORFILE,"\nWindow error in line %d. Must give four window" ,N); fprintf(ERRORFILE," coordinates.\nProgram terminated.\n"); } error_l(N) int N; { fprintf(ERRORFILE,"\nLine error in line %d. Must give two line",N); fprintf(ERRORFILE," coordinates.\nProgram terminated.\n"); } error_l1(N) int N; { fprintf(ERRORFILE,"\nError in line %d. You cannot draw a line",N); fprintf(ERRORFILE," until you have moved at least once.\n"); fprintf(ERRORFILE,"Program terminated.\n"); } error_d(N) int N; { fprintf(ERRORFILE,"\nDot error in line %d. Must give two dot",N); fprintf(ERRORFILE," coordinates\n\t\t\tand one dot radius.\n"); fprintf(ERRORFILE,"Program terminated.\n"); } error_m(N) int N; { fprintf(ERRORFILE,"\nMove error in line %d. Must give two move",N); fprintf(ERRORFILE," coordinates.\nProgram terminated.\n"); } error_W(N) int N; { fprintf(ERRORFILE,"\nWidth error in line %d. Must give a ",N); fprintf(ERRORFILE,"single integral"); fprintf(ERRORFILE," coordinate.\nProgram terminated.\n"); }