[comp.graphics] World

johnf@apollo.COM (John Francis) (09/29/88)

Since a lot of people have indicated that they would like a
program that reorganises map data, I include the program that
I used to do this. It is written for an Apollo, and the scaling
is set for a 1280 x 1024 display, but the main grouping code
(under conditional GROUP) should run on any machine.

#define  BOUND  0
#define  GRAPH  0
#define  GROUP  1
#define  WRITE  1

#include <stdio.h>
#if GRAPH
#include <math.h>
#include <apollo/base.h>
#include <apollo/gpr.h>
#include <apollo/time.h>
#endif

#if BOUND
static struct
  {
  float xmin,xmax,ymin,ymax;
  } lbounds, tbounds;
#endif

#if GROUP
static struct
  {
  float x,y;
  short int partner;
  short int padding;
  } vector[12600][2], *point;
#endif

main()
  {
  FILE              *chan,*dump;
  float              x, y;
  int                n, m, count;
  int                inner,outer;
  short int          link1,link2; 
  long  int          links = 0;
  int                segnum,seglen;
#if GRAPH
  status_$t          status;
  gpr_$coordinate_t  ix, iy;
  gpr_$bitmap_desc_t bitmap;
  gpr_$offset_t      xysize;
  time_$clock_t      delay;
#endif

#if BOUND
  lbounds.xmin =  5000.0;
  lbounds.xmax = -5000.0;
  lbounds.ymin =  5000.0;
  lbounds.xmax = -5000.0;
  tbounds.xmin =  5000.0;
  tbounds.xmax = -5000.0;
  tbounds.ymin =  5000.0;
  tbounds.xmax = -5000.0;
#endif

  chan = fopen("usa.dat","r");
#if WRITE
  dump = fopen("usa.dmp","wb");
#endif
#if GRAPH
  xysize.x_size = 0x500;
  xysize.y_size = 0x400;
  gpr_$init(gpr_$borrow,1,xysize,7,&bitmap,&status);
  gpr_$set_draw_value(2,&status);
#endif
  for (count = 0 ; ; count++)
   for (n = 0, m = 0; n < 2; n++)
    {
    fscanf(chan,"%f%f",&x,&y);
    if (feof(chan)) goto done;
    if (x == 0.0)
      {
      long int cv;
      char buffer[80];

      if ((cv = y) == 0) goto done;
#if GRAPH
      if (cv > 0) gpr_$set_draw_value((gpr_$pixel_value_t) cv,&status);
#endif
      fgets(&buffer[0],80,chan);
      fscanf(chan,"%f%f",&x,&y);
      }
#if WRITE
      fwrite(&x,sizeof(float),1,dump);
      fwrite(&y,sizeof(float),1,dump);
#endif
#if GROUP
    vector[count][n].x       =  x;
    vector[count][n].y       =  y;
    vector[count][n].partner = -1;
#endif
#if BOUND
    if (lbounds.xmin > x) lbounds.xmin = x;
    if (lbounds.xmax < x) lbounds.xmax = x;
    if (lbounds.ymin > y) lbounds.ymin = y;
    if (lbounds.ymax < y) lbounds.ymax = y;
#endif
#if GRAPH
    x *= 3.1415927/180.0;
    y *= 3.1415927/180.0;
    x  = (1.95548 + x)*cos(y);
    y  = (0.84890 - y);
    if (tbounds.xmin > x) tbounds.xmin = x;
    if (tbounds.xmax < x) tbounds.xmax = x;
    if (tbounds.ymin > y) tbounds.ymin = y;
    if (tbounds.ymax < y) tbounds.ymax = y;
    ix = 640 + 1000.0*x;
    iy = 512 + 1000.0*y;
    if (n == m)
      {
      if (n == 0) gpr_$move(ix,iy,&status);
             else gpr_$line(ix,iy,&status);
      m++;
      }
#endif
    }
done:
  fclose(chan);
#if WRITE
  fclose(dump);
#endif
#if BOUND
  printf("%5d vectors, spread = %8.3f %8.3f\nBounds = %8.3f %8.3f %8.3f %8.3f\n",
    count,lbounds.xmax-lbounds.xmin,lbounds.ymax-lbounds.ymin,
          lbounds.xmin,lbounds.xmax,lbounds.ymin,lbounds.ymax);
#if GRAPH
  printf("         %8.5f %8.5f %8.5f %8.5f\n",
          tbounds.xmin,tbounds.xmax,tbounds.ymin,tbounds.ymax);
#endif
#endif
#if GROUP
#if GRAPH
  gpr_$terminate(0,&status);
#endif
  point = &vector[0][0];
  for (outer = 1; outer < (count << 1); outer++)
   for (inner = 0; inner < (outer & -2); inner++)
    if ( (point[inner].partner < 0) && (point[inner].x == point[outer].x) && (point[inner].y == point[outer].y) )
      {
      point[inner].partner = outer;
      point[outer].partner = inner;
      if ( (++links % 250) == 0 ) printf("%5d: Outer = %5d, Inner = %5d\n",links,outer,inner);
      inner = outer;
      }
  chan = fopen("usa.new","w");
  segnum = 0;
  for (outer = 0; outer < 2*count; outer++)
   if ( point[outer].partner == ((short int) -1) )
    {
    seglen = 0;
    link1  = outer;
    while ( link1 >= 0 )
      {
      seglen++;
      link2 = link1 ^ 1;
      fprintf(chan,"%8.3f%7.3f%10.3f%7.3f\n",point[link1].x,point[link1].y,point[link2].x,point[link2].y);
      point[link1].partner = 0x8000;
      link1  = point[link2].partner;
      point[link2].partner = 0x8000;
      }
    printf("Segment %3d(O), length %5d\n",++segnum,seglen);
    fprintf(chan,"\n");
    }
  for (outer = 0; outer < 2*count; outer++)
   if ( point[outer].partner >= 0 )
    {
    seglen = 0;
    link2  = point[link1 = outer].partner;
    point[link1].partner = 0x8000;
    point[link2].partner = 0x8000;
    while ( link1 >= 0 )
      {
      seglen++;
      link2 = link1 ^ 1;
      fprintf(chan,"%8.3f%7.3f%10.3f%7.3f\n",point[link1].x,point[link1].y,point[link2].x,point[link2].y);
      point[link1].partner = 0x8000;
      link1  = point[link2].partner;
      point[link2].partner = 0x8000;
      }
    printf("Segment %3d(C), length %5d\n",++segnum,seglen);
    fprintf(chan,"\n");
    }
  fclose(chan);
#endif
#if GRAPH
#if GROUP
#else
  delay.high16 = 5;
  delay.low32  = 1;
  while (1) time_$wait(time_$relative,delay,&status);
#endif
#endif
  }