/* same as previous mailing, but with tektroniks output */ #include #include #include "coords.h" #define DATAFILE "data" #define ERRORFILE stderr main() { int no_input_errors = 1; int ok_to_draw_line = 0; int initial_point; int N,Q; char in_filename[50], out_filename[50]; char text[100]; char current_line[100]; FILE *fp_in, *get_data_file(); point lower_left, upper_right, p, q, ttimes(); point r, s; /* fp_in = get_data_file(in_filename); */ 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,"%%%[^\n]",text); comment_print(text); break; case 't'/*text, small*/: Q = sscanf(current_line,"t%[^\n]",text); textprint_small(text); break; case 'x'/*text, medium*/: Q = sscanf(current_line,"x%[^\n]",text); textprint_medium(text); break; case 'T'/*text, large*/: Q = sscanf(current_line,"T%[^\n]",text); textprint_large(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); set_clip_region(r,s); 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 '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); move_to(p); break; case 'L'/*line*/: /* fake L for use with to_tek */ 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); if (initial_point) move_to(p); else line_to(p); initial_point = 0; 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 'P'/*begin polygon*/: initial_point = 1; break; case 'Q'/*end polygon*/: case 'q'/*end polygon*/: break; /* fakes for to_tek */ 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_clip_region(p,q) point p, q; { /* maybe do this later */ } comment_print(text) char text[]; { /* fprintf(ERRORFILE,"Comment: %s.65\n",text); */ } textprint_large(text) char text[]; { putchar(31); printf("%s",text); } textprint_medium(text) char text[]; { putchar(31); printf("%s",text); } textprint_small(text) char text[]; { putchar(31); printf("%s",text); } end_drawing() { /* sleep(1000); */ } begin_drawing() { putchar(27); putchar('['); putchar('2'); putchar('J'); putchar(29); putchar(25); } set_width(width) int width; { } move_to(p) point p; { if ( (p.x_coord <= 1.0) && (p.y_coord <= 1.0) && (p.x_coord >= 0.0) && (p.y_coord >= 0.0)) { int X, Y, HI_X, HI_Y, LO_X, LO_Y, EXTRA; putchar(29); Y = (int) ( (p.y_coord / 1.0) * 844 ); X = (int) ( (p.x_coord / 1.0) * 855 ); HI_Y = 32 + (Y/32); LO_Y = 96 + (Y%32); HI_X = 32 + (X/32); LO_X = 64 + (X%32); Y = (int) ( (p.y_coord / 1.0) * 844 * 4); X = (int) ( (p.x_coord / 1.0) * 855 * 4); EXTRA = 96 + 4*(Y%4) + X%4; putchar(HI_Y); putchar(EXTRA); putchar(LO_Y); putchar(HI_X); putchar(LO_X); } } line_to(p) point p; { if ( (p.x_coord <= 1.0) && (p.y_coord <= 1.0) && (p.x_coord >= 0.0) && (p.y_coord >= 0.0)) { int X, Y, HI_X, HI_Y, LO_X, LO_Y, EXTRA; /* no command, just an address: */ Y = (int) ( (p.y_coord / 1.0) * 844 ); X = (int) ( (p.x_coord / 1.0) * 855 ); HI_Y = 32 + (Y/32); LO_Y = 96 + (Y%32); HI_X = 32 + (X/32); LO_X = 64 + (X%32); Y = (int) ( (p.y_coord / 1.0) * 844 * 4); X = (int) ( (p.x_coord / 1.0) * 855 * 4); EXTRA = 96 + 4*(Y%4) + X%4; putchar(HI_Y); putchar(EXTRA); putchar(LO_Y); putchar(HI_X); putchar(LO_X); } } makeline(p,q) point p, q; { if ( (p.x_coord <= 1.0) && (p.y_coord <= 1.0) && (p.x_coord >= 0.0) && (p.y_coord >= 0.0) && (q.x_coord <= 1.0) && (q.y_coord <= 1.0) && (q.x_coord >= 0.0) && (q.y_coord >= 0.0)) { int X, Y, HI_X, HI_Y, LO_X, LO_Y, EXTRA; putchar(29); Y = (int) ( (p.y_coord / 1.0) * 844 ); X = (int) ( (p.x_coord / 1.0) * 855 ); HI_Y = 32 + (Y/32); LO_Y = 96 + (Y%32); HI_X = 32 + (X/32); LO_X = 64 + (X%32); Y = (int) ( (p.y_coord / 1.0) * 844 * 4); X = (int) ( (p.x_coord / 1.0) * 855 * 4); EXTRA = 96 + 4*(Y%4) + X%4; putchar(HI_Y); putchar(EXTRA); putchar(LO_Y); putchar(HI_X); putchar(LO_X); Y = (int) ( (q.y_coord / 1.0) * 844 ); X = (int) ( (q.x_coord / 1.0) * 855 ); HI_Y = 32 + (Y/32); LO_Y = 96 + (Y%32); HI_X = 32 + (X/32); LO_X = 64 + (X%32); Y = (int) ( (p.y_coord / 1.0) * 844 * 4); X = (int) ( (p.x_coord / 1.0) * 855 * 4); EXTRA = 96 + 4*(Y%4) + X%4; putchar(HI_Y); putchar(EXTRA); putchar(LO_Y); putchar(HI_X); putchar(LO_X); } } FILE *get_data_file(in_filename) char in_filename[]; { FILE *fp_in, *fopen(); strcpy(in_filename,DATAFILE); fp_in = fopen(in_filename,"r"); if (fp_in==NULL) { fprintf(ERRORFILE,"\nI can't find the input file.\n\n"); } return(fp_in); } error_1(N,in_filename) int N; char in_filename[]; { /* This is a nuisance on a tektronix screen fprintf(ERRORFILE,"\nSuccessfully read "); fprintf(ERRORFILE,"%d lines of data from \"%s\"\n",N,in_filename); */ } error_w1(N) int N; { printf("\nWindow error in line %d. Window must have non-zero",N); printf(" width and height.\nProgram terminated.\n"); } error_r0(N) int N; { printf("\nRegion error in line %d. Must give four region",N); printf(" coordinates.\nProgram terminated.\n"); } error_w0(N) int N; { printf("\nWindow error in line %d. Must give four window",N); printf(" coordinates.\nProgram terminated.\n"); } error_l(N) int N; { printf("\nLine error in line %d. Must give two line",N); printf(" coordinates.\nProgram terminated.\n"); } error_l1(N) int N; { printf("\nError in line %d. You cannot draw a line",N); printf(" until you have moved at least once.\nProgram terminated.\n"); } error_m(N) int N; { printf("\nMove error in line %d. Must give two move",N); printf(" coordinates.\nProgram terminated.\n"); } error_W(N) int N; { printf("\nWidth error in line %d. Must give a single integral",N); printf(" coordinate.\nProgram terminated.\n"); }