/*file routines.c */ #include #include #include "coords.h" /* These subroutines output .di for a framed "picture" of SIZE mentioned just below. The "picture" will lie on a (usually larger) "page"; the page is thought of as a regular 8.5 x 11 piece of paper. If the output units are interpreted as inches, with the origin of the coordinate system at the left edge of the paper, 8.5 inches down, then the output page will appear as intended. Our standard .di-->.ps and .di-->.pdf translation programs will make the correct interpretation if they are issued the window command w 0.0 0.0 8.5 8.5 This is in fact accomplished by the "window()" subroutine just below. */ #define SIZE 6.4 /* width = height of picture, in inches */ /* The location of the upper left corner of the picture will be as follows: */ #define H_OFFSET 1.2 /* inches from left edge of paper */ #define V_OFFSET 2 /* inches from top edge of paper */ begin_small_text() {printf("t");} end_small_text() {printf("\n");} begin_medium_text() {printf("x");} end_medium_text() {printf("\n");} begin_large_text() {printf("T");} end_large_text() {printf("\n");} draw_axes(left,right,bottom,top) double left, right, bottom, top; { point p0, p1; printf("%%Here we draw the coordinate axes, if they fit.\n"); set_pen(2); if ( (left < 0.0) && (right > 0.0)) { p0.x_coord = p1.x_coord = H_OFFSET + SIZE * (left/(left - right)); p0.y_coord = 8.5 - V_OFFSET; p1.y_coord = 8.5 - V_OFFSET - SIZE; segment(p0,p1); } if ( (bottom < 0.0) && (top > 0.0)) { p0.y_coord = p1.y_coord = 8.5 - V_OFFSET - SIZE*(1.0-(bottom/(bottom - top))); p0.x_coord = H_OFFSET; p1.x_coord = H_OFFSET + SIZE; segment(p0,p1); } printf("%%This ends the laying down of the axes.\n"); } draw_frame() { point p,q,r,s; p.x_coord = q.x_coord = H_OFFSET; q.y_coord = r.y_coord = 8.5 - V_OFFSET; r.x_coord = s.x_coord = H_OFFSET + SIZE; s.y_coord = p.y_coord = 8.5 - V_OFFSET - SIZE; printf("%%Here we begin drawing a frame.\n"); set_pen(12); move_to(p); line_to(q); line_to(r); line_to(s); line_to(p); printf("%%This concludes the drawing of a frame.\n"); } print_path(start_t,stop_t,step,width,left,right,bottom,top,f) double left, right, bottom, top, start_t, stop_t; point (*f)(); int step, width; { int i; double t0, t1; point lower_corner, p0, p1; point lower_corner_inches; point plus(), minus(), ttimes(); lower_corner.x_coord = left; lower_corner.y_coord = bottom; lower_corner_inches.x_coord = H_OFFSET; lower_corner_inches.y_coord = 8.5 - V_OFFSET - SIZE; printf("%%Here we begin printing a curve.\n"); set_pen(width); for (i=0; i<(step+1); i++) { t0 = t1; t1 = (start_t + (((double) i)/step)*(stop_t - start_t)); p0 = p1; p1 = (*f)(t1); if (i==0) pic_move_to(p1,left,right,bottom,top); else pic_line_to(p1,left,right,bottom,top); } printf("%%Now we have finished printing a curve.\n"); } move_to(p) point p; { printf("m %3.3f %3.3f\n",p.x_coord,p.y_coord); } point pic_to_inches(p,left,right,bottom,top) point p; double left, right, bottom, top; { point lower_corner, p0, p1; point lower_corner_inches; point plus(), minus(), ttimes(); lower_corner.x_coord = left; lower_corner.y_coord = bottom; lower_corner_inches.x_coord = H_OFFSET; lower_corner_inches.y_coord = 8.5 - V_OFFSET - SIZE; p = minus(p,lower_corner); p.x_coord = p.x_coord/(right-left); p.y_coord = p.y_coord/(top-bottom); p = plus(lower_corner_inches,ttimes(SIZE,p)); return(p); } pic_move_to(p,left,right,bottom,top) /* like move_to(), but coordinates taken so that the left edge of the picture area (see above) has x-coordinate equal to "left" and so on. */ point p; double left, right, bottom, top; { p = pic_to_inches(p,left,right,bottom,top); move_to(p); } line_to(p) point p; { printf("l %3.3f %3.3f\n",p.x_coord,p.y_coord); } pic_line_to(p,left,right,bottom,top) /* like line_to(), but coordinates taken so that the left edge of the picture area (see above) has x-coordinate equal to "left" and so on. */ point p; double left, right, bottom, top; { p = pic_to_inches(p,left,right,bottom,top); line_to(p); } set_pen(width) int width; { printf("W %d ---- sets the width to %d points.\n",width,width); } segment(p,q) point p, q; { move_to(p); line_to(q); } point affine(p,q,r) point p, q, r; { point z; z.x_coord = p.x_coord - q.x_coord + r.x_coord; z.y_coord = p.y_coord - q.y_coord + r.y_coord; return (z); } point convex_comb(alpha,p,q) double alpha; point p, q; { point z; z.x_coord = alpha*(p.x_coord) + (1.0 - alpha)*(q.x_coord); z.y_coord = alpha*(p.y_coord) + (1.0 - alpha)*(q.y_coord); return (z); } point plus(p,q) point p, q; { point z; z.x_coord = p.x_coord + q.x_coord; z.y_coord = p.y_coord + q.y_coord; return (z); } point minus(p,q) point p, q; { point z; z.x_coord = p.x_coord - q.x_coord; z.y_coord = p.y_coord - q.y_coord; return (z); } point ttimes(alpha,p) double alpha; point p; { point z; z.x_coord = alpha*(p.x_coord); z.y_coord = alpha*(p.y_coord); return (z); } label_x_axis(left,right) double left, right; { point p; p.x_coord = H_OFFSET - 0.2; p.y_coord = 8.5 - V_OFFSET - SIZE - 0.22; printf("%%Here we label the x-axis.\n"); move_to(p); begin_medium_text(); printf("%4.2f",left); end_medium_text(); p.x_coord = p.x_coord + SIZE + 0.1; move_to(p); begin_medium_text(); printf("%4.2f",right); end_medium_text(); printf("%%This finishes the job of labeling the x-axis.\n"); } label_y_axis(top,bottom) double top, bottom; { point p; p.x_coord = H_OFFSET - 0.55; p.y_coord = 8.5 - V_OFFSET - .07; printf("%%Here we label the y-axis.\n"); move_to(p); begin_medium_text(); printf("%4.2f",top); end_medium_text(); p.y_coord = p.y_coord - SIZE + .1; move_to(p); begin_medium_text(); printf("%4.2f",bottom); end_medium_text(); printf("%%This finishes the job of labeling the y-axis.\n"); } window() { printf("w 0.0 0.0 8.5 8.5\n"); /* This allows us to use inches everywhere */ } position_label() { printf("%%Moving to the label position\n"); printf("m 1.1 -0.7\n"); } position_login_signature() { printf("%%Moving to the login signature position\n"); printf("m 5.5 -1.5\n"); } position_date_stamp() { printf("%%Moving to the datestamp position\n"); printf("m 5.5 -1.65\n"); }