home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d165 / plotview.lha / PlotView / Plot2Tek / plot2tek.c < prev    next >
C/C++ Source or Header  |  1988-11-22  |  5KB  |  293 lines

  1.  
  2. /*
  3.      plot2tek.c : Plot Unix plot files on Tektronix 410x terminal
  4.  
  5.      By Joel Swank April, 1988
  6.  
  7.      */
  8. #include <stdio.h>
  9. #include "4107.h"
  10.  
  11. FILE *input;
  12.  
  13. /* scaling factors */
  14.  
  15. long xscale, yscale;  /* width, height of output device */
  16. long xmult, ymult;  /* width, height on 410x Screen */
  17. long xoff, yoff;      /* offset to lower left corner  */
  18.  
  19. /* linemode constants */
  20.  
  21. char *lmodestr[] = { "dotted",
  22.                      "solid",
  23.                      "longdashed",
  24.                      "shortdashed",
  25.                      "dotdashed" };
  26.  
  27. short lmode[] = { 1, 2, 4, 3, 2 };
  28.  
  29.  
  30. main(argc,argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.  
  35. struct IntuiMessage    *msg;
  36. int cmd, i;
  37. long x,y, r;
  38. long r1, r2;
  39. long x1,y1,x2,y2;
  40. char textbuf[100], *p;
  41.  
  42. /*************************************************
  43.      Init defaults
  44. *************************************************/
  45.  
  46. xscale = 3120;
  47. yscale = 3120;
  48. xmult = 4096;
  49. ymult = 4096;
  50. xoff = 0;
  51. yoff = 0;
  52.  
  53.  
  54. /*************************************************
  55.      Interrogate command line 
  56. *************************************************/
  57.  
  58. if (argc == 1)
  59.     {
  60.     fprintf(stderr,"Usage:plot2tek plotfile\n");
  61.     exit(2);
  62.     }
  63.  
  64. while (*argv[1] == '-') 
  65.     {
  66.     p = (char *) &*argv[1];
  67.     p++;        /* point to the option chars */
  68.     switch (*p)
  69.         {
  70.         case 'x':        /* x size value */
  71.             xmult = atol(++p);
  72.             break;
  73.  
  74.         case 'y':        /* y size value */
  75.             ymult = atol(++p);
  76.             break;
  77.  
  78.         default:
  79.             fprintf(stderr,"plot2tek:Invalid option %s\n",argv[1]);
  80.             exit(27);
  81.         }
  82.     argc--;
  83.     argv++;
  84.     }
  85.  
  86. if (argc == 1 || argc >2)
  87.     {
  88.     fprintf(stderr,"plot2tek:Exactly One filename required\n");
  89.     exit(2);
  90.     }
  91.  
  92.  
  93. if ((input = fopen(argv[1],"r")) == NULL)
  94.     {
  95.     fprintf(stderr,"plot2tek: %s: open failed\n",argv[1]);
  96.     exit(3);
  97.     }
  98.  
  99.  code(TEK);
  100.  
  101. /*************************************************
  102.      MAIN Drawing loop
  103. *************************************************/
  104.  
  105. while ((cmd = getc(input)) != EOF)
  106.     {
  107.     switch (cmd)
  108.         {
  109.         case 'm':    /* move x,y */
  110.             get_xy(&x,&y);
  111.             mov(x,y);
  112.             break;
  113.         case 'n':    /* draw x,y */
  114.             get_xy(&x,&y);
  115.             drw(x,y);
  116.             break;
  117.         case 'p':    /* point x,y */
  118.             get_xy(&x,&y);
  119.             mov(x,y);
  120.             drw(x,y);
  121.             break;
  122.         case 'l':    /* line xs,ys, xe,ye */
  123.             get_xy(&x,&y);
  124.             mov(x,y);
  125.             get_xy(&x,&y);
  126.             drw(x,y);
  127.             break;
  128.         case 'a':    /* arc xc,yc, xs,ys, xe,ye */
  129.             get_xy(&x,&y);    /* get center */
  130.             get_xy(&x1,&y1);  /* get start point */
  131.             get_xy(&x2,&y2);  /* get end point */
  132.             arc(x, y, x1, y1, x2, y2);  /* draw counterclockwise  */
  133.             mov(x2,y2);
  134.             break;
  135.         case 't':    /* Text string\n   */
  136.             get_txt(textbuf);
  137.             gtext(textbuf);
  138.             break;
  139.         case 'c':    /* circle xc,yc, r */
  140.             get_xy(&x,&y);
  141.             get_int(&r);
  142.             r1 = r*xmult/xscale;
  143.             r2 = r*ymult/yscale;
  144.             mov(x-r1,y-r2);
  145.             circle(x+r1,y+r2,x-r1,y-r2);
  146.             break;
  147.         case 'f':    /* linemode string\n   */
  148.             get_txt(textbuf);
  149.             for (i=0; i<5; i++)
  150.                 {
  151.                 if (0 == strcmp(textbuf,lmodestr[i]))
  152.                     {
  153.                     linestyle(lmode[i]);
  154.                     break;
  155.                     }
  156.                 }
  157.             break;
  158.         case 's':    /* space xlo,ylo, xhi,yhi */
  159.             get_int(&xoff);
  160.             get_int(&yoff);
  161.             get_int(&xscale);
  162.             get_int(&yscale);
  163.             xscale = xscale - xoff;
  164.             yscale = yscale - yoff;
  165.             break;
  166.         case 'e':    /* erase */
  167.             page();
  168.             break;
  169.         }
  170.     }
  171.  linestyle(0);
  172.  code(ANSI);
  173. }
  174.  
  175.  
  176. /*************************************************
  177.      Parameter input routines
  178. *************************************************/
  179.  
  180.  
  181. /*
  182.  * input a pair of 16 bit ints, scale and clip to screen,
  183.  * and return them as longs
  184.  *
  185.  */
  186.  
  187. get_xy(x,y)
  188. register long *x, *y;
  189. {
  190.     get_int(x);
  191.     *x = (*x-xoff)*xmult/xscale;
  192.     get_int(y);
  193.     *y = (*y-yoff)*ymult/yscale;
  194. }
  195.  
  196. /*
  197.  * input a 16 bit int and return as a long
  198.  */
  199.  
  200. get_int(num)
  201. long *num;
  202. {
  203.     register short hi, lo;
  204.     lo =  getc(input);
  205.     hi = ( getc(input)) << 8;
  206.     *num = (long) lo + hi;
  207. }
  208.  
  209. /*
  210.  * input a text string delimited by newline,
  211.  * return to buffer delimited by a null.
  212.  */
  213.  
  214. get_txt(str)
  215. char *str;
  216. {
  217.     register int cmd;
  218.     while ((cmd = getc(input)) != '\n')
  219.         *str++ = cmd;
  220.     *str = '\0';
  221. }
  222.  
  223. /*
  224.  * arc and integer sqrt routines.
  225.  * lifted from sunplot program by:
  226.  
  227. Sjoerd Mullender
  228. Dept. of Mathematics and Computer Science
  229. Free University
  230. Amsterdam
  231. Netherlands
  232.  
  233. Email: sjoerd@cs.vu.nl
  234. If this doesn't work, try ...!seismo!mcvax!cs.vu.nl!sjoerd or
  235. ...!seismo!mcvax!vu44!sjoerd or sjoerd%cs.vu.nl@seismo.css.gov.
  236.  
  237.  *
  238.  */
  239.  
  240. long
  241. isqrt(n)
  242. long n;
  243. {
  244.     long a, b, c;
  245.  
  246.     a = n;
  247.     b = n;
  248.     if (n > 1) {
  249.         while (a > 0) {
  250.             a = a >> 2;
  251.             b = b >> 1;
  252.         }
  253.         do {
  254.             a = b;
  255.             c = n / b;
  256.             b = (c + a) >> 1;
  257.         } while ((a - c) < -1 || (a - c) > 1);
  258.     }
  259.     return b;
  260. }
  261.  
  262.  
  263. arc(x, y, x1, y1, x2, y2)
  264. long x, y, x1, y1, x2, y2;
  265. {
  266.     register long a1 = x1 - x, b1 = y1 - y, a2 = x2 - x, b2 = y2 - y;
  267.     register long c1 = a1 * y - b1 * x, c2 = a2 * y - b2 * x;
  268.     register long r2 = a1 * a1 + b1 * b1;
  269.     register long i, sqrt;
  270.  
  271.     for (i = isqrt(r2 >> 1); i >= 0; i -= 1) {
  272.         sqrt = isqrt(r2 - i * i);
  273.         setcir(x + i, y + sqrt, a1, b1, c1, a2, b2, c2);
  274.         setcir(x + i, y - sqrt, a1, b1, c1, a2, b2, c2);
  275.         setcir(x - i, y + sqrt, a1, b1, c1, a2, b2, c2);
  276.         setcir(x - i, y - sqrt, a1, b1, c1, a2, b2, c2);
  277.         setcir(x + sqrt, y + i, a1, b1, c1, a2, b2, c2);
  278.         setcir(x + sqrt, y - i, a1, b1, c1, a2, b2, c2);
  279.         setcir(x - sqrt, y + i, a1, b1, c1, a2, b2, c2);
  280.         setcir(x - sqrt, y - i, a1, b1, c1, a2, b2, c2);
  281.     }
  282. }
  283.  
  284. static setcir(x, y, a1, b1, c1, a2, b2, c2)
  285. long x, y, a1, b1, c1, a2, b2, c2;
  286. {
  287.     if (a1 * y - b1 * x >= c1 && a2 * y - b2 * x <= c2)
  288.         {
  289.         mov(x, y);
  290.         drw(x, y);
  291.         }
  292. }
  293.