home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / psgraph / part01 / psgsimp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-06  |  4.2 KB  |  151 lines

  1. /*
  2.  * psgsimp.c
  3.  *
  4.  * Simplify a psgraph-style plot file, removing graph points closer
  5.  * together than a specified threshold.
  6.  *
  7.  * Jeffrey Mogul    DECWRL        10 January 1992
  8.  * 
  9.  *               Copyright (c) 1992 Digital Equipment Corporation
  10.  *                          All Rights Reserved
  11.  * 
  12.  * 
  13.  * Permission to use, copy, and modify this software and its documentation
  14.  * is hereby granted only under the following terms and conditions.  Both
  15.  * the above copyright notice and this permission notice must appear in
  16.  * all copies of the software, derivative works or modified versions, and
  17.  * any portions threof, and both notices must appear in supporting
  18.  * documentation.
  19.  * 
  20.  * Users of this software agree to the terms and conditions set forth
  21.  * herein, and hereby grant back to Digital a non-exclusive, unrestricted,
  22.  * royalty-free right and license under any changes, enhancements or
  23.  * extensions made to the core functions of the software, including but
  24.  * not limited to those affording compatibility with other hardware or
  25.  * software environments, but excluding applications which incorporate
  26.  * this software.  Users further agree to use their best efforts to return
  27.  * to Digital any such changes, enhancements or extensions that they make
  28.  * and inform Digital of noteworthy uses of this software.  Correspondence
  29.  * should be provided to Digital at:
  30.  * 
  31.  *                       Director of Licensing
  32.  *                       Western Research Laboratory
  33.  *                       Digital Equipment Corporation
  34.  *                       250 University Avenue
  35.  *                       Palo Alto, California  94301  
  36.  * 
  37.  * This software may be distributed (but not offered for sale or
  38.  * transferred for compensation) to third parties, provided such third
  39.  * parties agree to abide by the terms and conditions of this notice.
  40.  * 
  41.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS
  42.  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
  43.  * WARRANTIES OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL
  44.  * EQUIPMENT CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  45.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  46.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  47.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  48.  * PERFORMANCE OF THIS SOFTWARE.
  49.  */
  50.  
  51. #include <stdio.h>
  52. #include <math.h>
  53.  
  54. #ifndef    HUGE_VAL
  55. #define    HUGE_VAL    HUGE
  56. #endif
  57.  
  58. Usage()
  59. {
  60.     fprintf(stderr, "Usage: psgsimp thresh-pct <infile >outfile\n");
  61. }
  62.  
  63. main(argc, argv)
  64. int argc;
  65. char **argv;
  66. {
  67.     double thresh;
  68.     static char linebuf[1024];
  69.     static char lasthidden[1024];
  70.     float lastx, lasty;
  71.     float thisx, thisy;    /* sscanf() deals in floats, not doubles */
  72.     float deltax, deltay;
  73.     static char otherstuff[1024];
  74.  
  75.     if (argc != 2) {
  76.         Usage();
  77.         exit(1);
  78.     }
  79.     
  80.     thresh = atof(argv[1]);
  81.     
  82.     lastx = HUGE_VAL;
  83.     lasty = HUGE_VAL;
  84.  
  85.     lasthidden[0] = '\0';
  86.  
  87.     while (gets(linebuf)) {
  88.         if (sscanf(linebuf, "%f %f %s", &thisx, &thisy, otherstuff) == 2) {
  89.         /* only x, y value on this line */
  90.  
  91.         /* compute deltax ratio */
  92.         if ((thisx == 0.0) && (lastx == 0.0)) {
  93.             deltax = 0.0;
  94.         }
  95.         else if (thisx == 0.0) {
  96.             deltax = (thisx - lastx)/lastx;
  97.         }
  98.         else {
  99.             deltax = (thisx - lastx)/thisx;
  100.         }
  101.         if (deltax < 0.0)
  102.             deltax = -deltax;
  103.         
  104.         /* compute deltay ratio */
  105.         if ((thisy == 0.0) && (lasty == 0.0)) {
  106.             deltay = 0.0;
  107.         }
  108.         else if (thisy == 0.0) {
  109.             deltay = (thisy - lasty)/lasty;
  110.         }
  111.         else {
  112.             deltay = (thisy - lasty)/thisy;
  113.         }
  114.         if (deltay < 0.0)
  115.             deltay = -deltay;
  116.         
  117. #ifdef    DEBUG
  118.         printf("# x %f -> %f (%f), y %f -> %f (%f)\n",
  119.             lastx, thisx, deltax,
  120.             lasty, thisy, deltay);
  121. #endif    DEBUG
  122.         
  123.         if ((deltax < thresh) && (deltay < thresh)) {
  124.             printf("##%s\n", linebuf);
  125.             strcpy(lasthidden, linebuf);
  126.             continue;
  127.         }
  128.  
  129.         lastx = thisx;
  130.         lasty = thisy;
  131.         if (lasthidden[0] != '\0') {
  132.             printf("%s\n", lasthidden);
  133.         }
  134.         printf("%s\n", linebuf);
  135.         lasthidden[0] = '\0';
  136.         }
  137.         else {
  138.         if (lasthidden[0] != '\0') {
  139.             printf("%s\n", lasthidden);
  140.         }
  141.         printf("%s\n", linebuf);
  142.         lasthidden[0] = '\0';
  143.         if (linebuf[0] != '#') {
  144.             /* if non-comment, ensure next point is not suppressed */
  145.             lastx = HUGE_VAL;
  146.             lasty = HUGE_VAL;
  147.         }
  148.         }
  149.     }
  150. }
  151.