/* ACM North Central Region, 1993-94 Problem F, PostScript Emulation Ed Karrels, March 1996 */ #include #include typedef struct Trans { int type; float x, y; struct Trans *next; } Trans; char cmds[7][10] = {"rotate", "translate", "scale", "moveto", "rmoveto", "lineto", "rlineto"}; int ReadMove(float *x, float *y) { char cmd[20]; int i; scanf("%f %f", x, y); scanf("%s", cmd); for (i=0; i<7; i++) { if (!strcmp(cmd, cmds[i])) return i+1; } return 0; } void Xlate(float *x, float *y, Trans *tr) { float tx, ty; /* printf("Tr %.2f %.2f\n", *x, *y); */ while (tr) { switch (tr->type) { case 1: tx = *x * cos(tr->x) - *y * sin(tr->x); *y = *x * sin(tr->x) + *y * cos(tr->x); *x = tx; break; case 2: *x += tr->x; *y += tr->y; break; case 3: *x *= tr->x; *y *= tr->y; break; } /* printf("-> %.2f %.2f\n", *x, *y); */ tr = tr->next; } } int main() { Trans *head_t = 0, *tail_t, *tr; int movetype; float x, y, cur_x, cur_y, tx, ty; while (movetype = ReadMove(&x, &y)) { if (movetype < 4) { tr = (Trans*)malloc(sizeof(Trans)); tr->type = movetype; tr->x = x; tr->y = y; tr->next = 0; if (!head_t) { head_t = tail_t = tr; } else { tr->next = head_t; head_t = tr; } switch (tr->type) { case 1: tr->x *= 3.141592653 / 180.0; tx = cur_x * cos(-tr->x) - cur_y * sin(-tr->x); cur_y = cur_x * sin(-tr->x) + cur_y * cos(-tr->x); cur_x = tx; break; case 2: cur_x -= tr->x; cur_y -= tr->y; break; case 3: cur_x /= tr->x; cur_y /= tr->y; break; } } else { switch (movetype) { case 4: cur_x = x; cur_y = y; Xlate(&x, &y, head_t); printf("%g %g moveto\n", x, y); break; case 5: tx = cur_x; ty = cur_y; cur_x += x; x = cur_x; cur_y += y; y = cur_y; Xlate(&x, &y, head_t); Xlate(&tx, &ty, head_t); printf("%g %g rmoveto\n", x-tx, y-ty); break; case 6: cur_x = x; cur_y = y; Xlate(&x, &y, head_t); printf("%g %g lineto\n", x, y); break; case 7: tx = cur_x; ty = cur_y; cur_x += x; x = cur_x; cur_y += y; y = cur_y; Xlate(&x, &y, head_t); Xlate(&tx, &ty, head_t); printf("%g %g rlineto\n", x-tx, y-ty); break; } } } return 0; }