/*
 * Copyright (C) Andrew Wuensche
 *
 *  This file is part of DDLab (Discrete Dynamics Lab) <www.ddlab.org>
 *
 *  DDLab is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DDLab.  If not, see <http://www.gnu.org/licenses/>.
 */

/************** include/define 13 May 93 ********************/
#include "edecfunc.h"
#include <string.h>
/***************  list of functions in this file ************/
/******** basins of attraction  **********************/
/*void nodetext_ps(char bcolor, char *text, signed int px, signed int py);
void right_jtext_ps(signed int px,signed int py,char *text);
void resetfont_ps(char *thisfont,signed int newpoint);

void rect_outline_ps(signed int startx, signed int starty, 
                 signed int widthx, signed int heighty);
void rect_fill_ps(signed int startx, signed int starty, 
                 signed int widthx, signed int heighty);
void hex_outline_ps(void);
void hex_fill_ps(void);
void block_fill_ps(signed int px, signed int py);
void block_stroke_ps(signed int px, signed int py);
void lineto_ps(signed int px, signed int py);
void moveto_ps(signed int px, signed int py);
void lineto_stroke_ps(signed int px, signed int py);
void color_ps(char thisgrey,char pcolor);

void circle_ps(signed int px, py, double prad);
void circle_fill_ps(signed int px, py, double prad);
void smallspot_ps(signed int px, signed int py, double dia);
void line_ps(signed int px0, signed int py0,
                     signed int px1, signed int py1);
void thick_line_ps(double how_thick, signed int px0, signed int py0,
                     signed int px1, signed int py1);

void line_block_ps(signed int  *poly);
void arrow_block_ps(signed int *arrow);

void prepare_basinPS(char ps_type, char include_all);
*/
void define_block(char *blockname, signed int xd, signed int yd);
/*void sp_node_ps(char node2d,char* array,signed int px,signed int py,char color));*/
void draw_divs_basins_cg(signed int node_y,
			          signed int px, signed int py);

/******** space-time patterns **********************/
/*void save_to_PostScript(char* array, signed int zi,signed int zj,signed int zh,
          unsigned int patch_size,char cellscale,char show_2d_divs,unsigned char divcolor,
	     char show_dots,char post_hex2d,char gaps_in_string);*/
void draw_divs_cg(char lattice_type,unsigned char divcolor,char post_hex2d,char gapx);
void draw_divs_d(char d_gray,unsigned char divcolor,char post_hex2d,signed int zj);
void draw_symbol(char stype[], char post_hex2d, double hexside);

signed int ii,jj,row,col,max_zi,max_zj;
double psi,psj,offx=0,y2d;
char instant_pic=0;

#  define writethis_PS \
      vret=write(fid_PS,textb[0],strlen(textb[0]));\
      sprintf(textb[0],""); 

/***** PostScript show text *********/
void nodetext_ps(char ps_type, char bcolor, char *text, signed int px, signed int py){
                    
  double tempgrey;
  signed int texth,textw;

  if (ps_type=='L'){ //for Labels in interactive graphs -- expand border
    signed int thisx,thisy;
     if (bground=='w') tempgrey = 0.0;       /*for black/white*/
     else if (bground=='b') tempgrey = 1.0;

# if __WATCOM__
     if (bground=='b') bcolor=white; //to fix white on black -- hack
# endif

     textw=text_pix_width(text);
     //texth=CHeight*.85-vgax(1);
     texth=CHeight*.85-1;
     /*first draw background block*/
     sprintf(textb[0],
       "gsave newpath %d %d moveto %d 0 rlineto 0 %d rlineto -%d 0 rlineto\n",
             px-1,Maxy-py-1,textw+1,texth+1,textw+1);
     writethis_PS

     sprintf(textb[0],"closepath %s setrgbcolor fill grestore\n",dcolor[bcolor]);
     writethis_PS
     sprintf(textb[0],"gsave newpath %d %d moveto %.1f setgray (%s) show grestore\n",
                                            px-1,Maxy-py-1,tempgrey,text);
     writethis_PS 
  }
  
  else if (ps_type==1){ 
     if (bground=='w') tempgrey = 0.0;       /*for black/white*/
     else if (bground=='b') tempgrey = 1.0;
     textw=(strlen(text)*psize_ps)*.5;
     texth=psize_ps*.7;
     /*first draw background block*/
     sprintf(textb[0],
           "gsave newpath %d %d moveto %d 0 rlineto 0 %d rlineto -%d 0 rlineto\n",
              px-1,Maxy-py-1,textw+1,texth+1,textw+1); 
     writethis_PS

     sprintf(textb[0],"closepath %s setrgbcolor fill grestore\n",dcolor[bcolor]);
     writethis_PS
     sprintf(textb[0],"gsave newpath %d %d moveto %.1f setgray (%s) show grestore\n",
                                            px-1,Maxy-py-1,tempgrey,text);
     writethis_PS 
  }

  else if (ps_type==2){ /*for jump-graph*/
    signed int newpoint,xmove,ymove;
     if (bground=='w') tempgrey = 1.0;       /*for black/white*/
     else if (bground=='b') tempgrey = 0.0;     
     newpoint=(signed int) ((double)psize_ps*1.5);
     xmove=(signed int)((double)(strlen(text)*newpoint)*.3);   
     ymove=(signed int)( (double)newpoint*.3);
     sprintf(textb[0],"gsave newpath %d %d moveto %.1f setgray (%s) show grestore\n",
                                  px-xmove,Maxy-py-ymove,tempgrey,text);     
     writethis_PS 
  }
  if (ps_type==3){  /*for wiring matrix -- color or gray set there*/
     sprintf(textb[0],"gsave newpath %d %d moveto (%s) show grestore\n",
                                              px,Maxy-py,text);    
     writethis_PS 
  }   
}

/***** PostScript show text right justified *********/
void right_jtext_ps(signed int px,signed int py,char *text){

sprintf(textb[0], /*right justify text with rightshow*/
        "gsave newpath (%s) dup stringwidth pop %d exch sub %d moveto show grestore\n",
	                                 text,px,py); 
   writethis_PS
}  


/***** PostScript resetfont *********/
void resetfont_ps(char *thisfont, signed int newpoint){
     sprintf(textb[0],"/%s  findfont %d scalefont setfont\n",thisfont,newpoint);
     writethis_PS
}


/*** PostScript draw empty rectangle ******/
void rect_outline_ps(signed int startx, signed int starty, 
                 signed int widthx, signed int heighty){

  sprintf(textb[0],
   "gsave newpath %d %d moveto %d 0 rlineto 0 -%d rlineto -%d 0 rlineto closepath stroke grestore\n",
	            startx, Maxy-starty, widthx, heighty, widthx);
  writethis_PS            
}

/*** PostScript draw rectangle and fill with current color rectangle ******/
void rect_fill_ps(signed int startx, signed int starty, 
                 signed int widthx, signed int heighty){

  /*         *<--*
                 |           
	     *-->*  */

  sprintf(textb[0],
    "gsave newpath %d %d moveto %d 0 rlineto 0 -%d rlineto -%d 0 rlineto closepath fill grestore\n",
	            startx, Maxy-starty, widthx, heighty, widthx);
  writethis_PS            
}

/*** PostScript draw empty rectangle ******/
void hex_outline_ps(void){
sprintf(textb[0],
"gsave newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto %d %d lineto %d %d lineto closepath stroke grestore\n",
	  poly[0], Maxy-poly[1], poly[2], Maxy-poly[3], poly[4], Maxy-poly[5], 
          poly[6], Maxy-poly[7], poly[8], Maxy-poly[9], poly[10], Maxy-poly[11]);
  writethis_PS            
}

/*** PostScript draw rectangle and fill with current color rectangle ******/
void hex_fill_ps(void){
 sprintf(textb[0],
"gsave newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto %d %d lineto %d %d lineto closepath fill grestore\n",
	  poly[0], Maxy-poly[1], poly[2], Maxy-poly[3], poly[4], Maxy-poly[5], 
          poly[6], Maxy-poly[7], poly[8], Maxy-poly[9], poly[10], Maxy-poly[11]);
  writethis_PS  
}
/*** PostScript filled cell ******/
void block_fill_ps(signed int px, signed int py){

     sprintf(textb[0]," %d %d block_f\n",px, Maxy-py);
     writethis_PS 
} 

/*** PostScript empty cell  ******/
void block_stroke_ps(signed int px, signed int py){
    
     sprintf(textb[0]," %d %d block_s\n",px, Maxy-py);
     writethis_PS 
} 

/***** PostScript rlineto  *********/
void lineto_ps(signed int px, signed int py){

     sprintf(textb[0]," %d %d lineto\n",px, Maxy-py);
     writethis_PS 
} 

/***** PostScript rlineto  *********/
void lineto_stroke_ps(signed int px, signed int py){

     sprintf(textb[0]," %d %d lineto stroke\n",px, Maxy-py);
     writethis_PS 
}    
    

/***** PostScript moveto  *********/
void moveto_ps(signed int px, signed int py){

     sprintf(textb[0]," newpath %d %d moveto\n",px, Maxy-py);
     writethis_PS 
}     

/***** PostScript color  *********/
void color_ps(char thisgrey, char pcolor){
    
     if (thisgrey==0) sprintf(textb[0]," %s setrgbcolor\n",dcolor[pcolor]);
     else sprintf(textb[0]," %f setgray\n",grayscale[pcolor]);
     writethis_PS      
} 

/***** PostScript grayscale  *********/
/*void grey_ps(char pcolor){
 
  sprintf(textb[0]," %f setgray \n",grayscale[pcolor]);
  writethis_PS         
} */    

/***** PostScript node *********/
void circle_ps(signed int px, signed int py, double prad){
     
     sprintf(textb[0],"%d %d %.2f node\n",px,Maxy-py,prad);
     writethis_PS 
}

/***** PostScript node *********/
void circle_fill_ps(signed int px, signed int py, double prad){
     
     sprintf(textb[0],"%d %d %.2f node_fill\n",px,Maxy-py,prad);
     writethis_PS
}

/***** PostScript smallspot *********/
void smallspot_ps(signed int px, signed int py, double dia){

    sprintf(textb[0],"%d %d %.2f smallspot\n",px,Maxy-py,dia);
    writethis_PS 
}

/***** PostScript draw a variable thickness line *********/
void line_ps(signed int px0, signed int py0,
                     signed int px1, signed int py1){
   sprintf(textb[0],
	"gsave %d %d moveto %d %d lineto stroke grestore\n",
	                                 px0,Maxy-py0,px1,Maxy-py1);
   writethis_PS
}


/***** PostScript draw a variable thickness line *********/
void thick_line_ps(double how_thick, signed int px0, signed int py0,
                     signed int px1, signed int py1){
   /*0 thickness is thinline*/
   sprintf(textb[0],
	"gsave  %.2f setlinewidth 0 setlinecap %d %d moveto %d %d lineto stroke grestore\n",
	                                   how_thick,px0,Maxy-py0,px1,Maxy-py1);
   writethis_PS
}

/***** PostScript draw a hexagon  *********/
void line_block_ps(signed int *poly){ 
   sprintf(textb[0],"gsave newpath %d %d moveto %d %d lineto %d %d lineto\n",
                     poly[0],Maxy-poly[1],poly[2],Maxy-poly[3],poly[4],Maxy-poly[5]);
   writethis_PS
   sprintf(textb[0],"%d %d lineto closepath fill grestore\n",poly[6],Maxy-poly[7]);
   writethis_PS
}

/***** PostScript draw an arrow head *********/
void arrow_block_ps(signed int *arrow){  
  sprintf(textb[0],
    "gsave newpath %d %d moveto %d %d lineto %d %d lineto closepath fill grestore\n",
             arrow[0],Maxy-arrow[1],arrow[2],Maxy-arrow[3],arrow[4],Maxy-arrow[5]);
   writethis_PS
}

/***** create a PostScript file basins, subtrees, fields, range, dec 2007 *********/
void prepare_basinPS(char ps_type, char include_all){
  /*ps_type: basins=1, net or jump graph=2, matrix/wiring=3*/
  char bcolor,leftoff;
  signed int zi,zj;
 
  
  /* note: to include the .ps image, or any part of it, in a LaTex
           document, find the best BoundingBox by viewing the image in
           gv, there is a cursor position read out, top left.  Note
           x0,y0 bottom-left, and x1,y1 top-right. Substitute these
           co-ords after %%BoundingBox: 2nd line in the .ps file in
           place of the default co-ords (which are usualy 0 0 925 694)
           example to include in LaTex file:
           \epsfig{file=f_ddop2006/my_bPS.ps, width=10cm}*/

  if (include_all){ /*include_all==2 for matrix/wiring bold*/ 
           if (psize_ps==0) psize_ps=pointsize; 
           /*delete previous*/
           if (ps_type==1){ /*if (basin_postScript)*/
              fid_PS=myopen(filename_basin_PS,1,0);
              vret=write(fid_PS, "",0);
              close(fid_PS);
              /*start new*/
              fid_PS=myopen(filename_basin_PS,1,1);
	   }

           /*ps_type=4, 3d axo stp - done in input_string_cell( miscx.c*/
           /*ps_type=3, wiring matrix - done in print_wiring_scheme( wiringx.c*/
           if (ps_type<=2){ 
              sprintf(textb[0],"%%!PS-Adobe-3.0 EPSF-3.0\n");
              writethis_PS
	      if (isotropic_set==1)
                 sprintf(textb[0],"%%%%BoundingBox: %d %d %d %d\n",Maxx/2,0,Maxx,Maxy);
	      else sprintf(textb[0],"%%%%BoundingBox: 0 0 %d %d\n",Maxx,Maxy);
              writethis_PS
	   }
	   	   
           if (ps_type==1){ /*for basin*/
	       sprintf(textb[1],"Times-Roman");
               resetfont_ps(textb[1], psize_ps);/*small text*/
           }
           else if (ps_type==2){ /*big+bold, for wiring-graphic*/
                sprintf(textb[1],"Times-Bold");
	        resetfont_ps(textb[1],(signed int) ((double)psize_ps*1.5) );
                
           }          
           else if (ps_type==3){  /*for wiring matrix*/
	       if (include_all==2) sprintf(textb[1],"Times-Bold"); 
	       else sprintf(textb[1],"Times-Roman");
               resetfont_ps(textb[1],(signed int) ((double)psize_ps*1.25) );
           }
            
           sprintf(textb[0],".7 setlinewidth 1 setlinecap\n"); /*linewidth, was 0 minimum*/
           writethis_PS

           /*fill with black for black background*/          
	   if (bground=='b'){
                sprintf(textb[0],"gsave newpath 0 %d moveto\n",Maxy);
                writethis_PS
                sprintf(textb[0],"%d 0 rlineto\n",Maxx);
                writethis_PS
                sprintf(textb[0],"0 -%d rlineto\n",Maxy);
                writethis_PS
                sprintf(textb[0],"-%d 0 rlineto closepath\n",Maxx);
                writethis_PS
                sprintf(textb[0],"0 0 0 setrgbcolor fill grestore\n");
                writethis_PS               
	   }
       
	  /*define black/white for node pattern frames frames*/
          if (bground=='w'){ 
               sprintf(textb[5],"1 1 1 setrgbcolor");
               sprintf(textb[6],"0 0 0 setrgbcolor");
           }
           else if (bground=='b'){
              sprintf(textb[5],"0 0 0 setrgbcolor");
              sprintf(textb[6],"1 1 1 setrgbcolor");
           }
           sprintf(textb[0],"/bcolor{%s} def\n",textb[5]); 
           writethis_PS
           sprintf(textb[0],"/fcolor{%s} def\n",textb[6]); 
           writethis_PS

           /*define a node*/
           sprintf(textb[0],
               "/node{gsave newpath  0 360 arc closepath stroke grestore }def\n");
           writethis_PS

           /*define a filled node*/
           sprintf(textb[0],
               "/node_fill{gsave newpath  0 360 arc closepath fill grestore }def\n");
           writethis_PS

           /*define a dot*/
            sprintf(textb[0],
    "/smallspot{gsave setlinewidth 1 setlinecap moveto 0 0 rlineto stroke grestore }def\n");
            writethis_PS
    	    
	      if (ps_type==1){ /*if (basin_postScript*/
              /* for a range of fields. basins, or subtrees, this below is redefined*/
              /*define a square
              0.  .1

              3.  .2   this is to hilight a node in density problem */
   
              /*empty - stroke*/
              sprintf(textb[0],"/block_s{gsave newpath moveto\n");
              writethis_PS
              sprintf(textb[0],"%d 0 rlineto\n",scale);
              writethis_PS
              sprintf(textb[0],"0 -%d rlineto\n",scale);
              writethis_PS
              sprintf(textb[0],"-%d 0 rlineto closepath stroke grestore } def\n",scale);
              writethis_PS
              /*filled*/
              sprintf(textb[0],"/block_f{gsave newpath moveto\n");
              writethis_PS
              sprintf(textb[0],"%d 0 rlineto\n",scale);
              writethis_PS
              sprintf(textb[0],"0 -%d rlineto\n",scale);
              writethis_PS
              sprintf(textb[0],"-%d 0 rlineto closepath fill grestore } def\n",scale);
              writethis_PS
	  }
   }

   /************ this is for patterns at nodes **************/
   if (basin_postScript){
     //fprintf(stderr,"\nin patterns at nodes scale=%d",scale);
     
      /*define a dot*/
      sprintf(textb[0],
      "/dot1{gsave .7 setlinewidth 1 setlinecap moveto 0 0 rlineto stroke grestore} def\n");
       writethis_PS

      /*0.  .1

        3.  .2  to show one cell - part of the pattern*/
      sprintf(textb[1],"block1");
      define_block(textb[1],scale,scale);

      /*draw an outline filled with background color around grid*/
      zi=i2d;
      zj=j2d;
      if (show_one_att_state==0 && showbits_1d_as_2d==0 && twod==0){
         zi=lineL;
         zj=1;
      }
      
      leftoff=2;

      if (bground=='w') bcolor=1;
      else if (bground=='b') bcolor=0;
      sprintf(textb[1],"block_all");
      define_block(textb[1],scale*zi+leftoff,scale*zj+leftoff);
      sprintf(textb[1],"block_outer");
      define_block(textb[1],scale*zi+leftoff+4,scale*zj+leftoff+4);
   } 
}

/***** to draw individual cells in pattern *********/
void define_block(char *blockname, signed int xd, signed int yd){
  /*to draw individual cells in pattern*/

      sprintf(textb[0],"/%s{newpath moveto\n",blockname);
      writethis_PS
      sprintf(textb[0],"%d 0 rlineto\n",xd);
      writethis_PS
      sprintf(textb[0],"0 -%d rlineto\n",yd);
      writethis_PS
      sprintf(textb[0],"-%d 0 rlineto closepath} def\n",xd);
      writethis_PS
}


/***** PostScript 2d basin nodes, Dec 2007 *********/
void sp_node_ps(char node2d, char* array, 
      signed int px, signed int py, char color){
 
 char lattice_type,leftoff,this_value,old_value,dots_drawn=0,
      b_show_2d_divs=0,b_show_dots=0,tempcolor,no_more_cells=0;
 signed int zi,zj,node_y,ij_excess;
 double tempgrey;
 unsigned int poss;
 
 
 py=Maxy-py;
 zi=i2d;
 zj=j2d;
 b_show_2d_divs=0;
 b_show_dots=0;

 /*same as DDLab screen -- make this a variable ?*/
 if (scale>3 && node_tight==0){
    if (bground=='w') b_show_2d_divs=2;
    else if (bground=='b') b_show_2d_divs=1;
    b_show_dots=1;
 }
 if (scale>3 && node_dots==1) b_show_dots=1;

 if (node2d==0 || node2d==-1){
   zi=lineL;
   zj=1;
   tempcolor=color_v[0];
   color_v[0]=darkgray; /*so that 0 is visible*/
 }

 py-=scale*zj; /*position as in DDLab*/   
 if (basin_postScript==1) lattice_type='c'; /*color*/
 else if (basin_postScript==2) lattice_type='g'; /*greyscale*/

 row=1;
 node_y=zj*scale;

 old_value=this_value=-1;
 dots_drawn=0;

 leftoff=1;
 
 ij_excess=(zi*zj)-lineL; /*to allow i not whole factor of lineL - incomplete 2d*/
 for(jj=zj-1;jj>=0;jj--){ /*top row down*/   
   row-=1;
   col=-1;
   /*from left cell -> right*/
    for(ii=zi-1;ii>=0;ii--){
       col+=1;
       /*to allow i not whole factor of lineL - incomplete 2d*/
       poss=zi*jj + ii - ij_excess;
       old_value=this_value;
       if (poss==Maxu) no_more_cells=1; /*one time setting*/
       if (no_more_cells==1) this_value=0;
       else this_value=get_value(array,poss);          
      
       /*draw colored lattice as in DDLab, or grayscale*/
       if (lattice_type=='c' || lattice_type=='g'){
           /*first set the color or greyscale*/
          if (this_value!=old_value || dots_drawn==1){ /*reset color if different*/ 
	     if (lattice_type=='c'){
                if (value_range>2 || get_value(array,poss)==0) 
                                    color_ps(0,color_v[this_value]);
                else color_ps(0,color);/*for v=2 graphic nodes in basins to change color*/
             }
             else if (lattice_type=='g'){                         
                if (bground=='w') tempgrey = 1.0 - ((double)this_value)/(value_range-1);
                else if (bground=='b') tempgrey = ((double)this_value)/(value_range-1);
                if (value_range==3 && this_value==1) /*make grey lighter*/
		                               tempgrey=.7;
               sprintf(textb[0],"%.3f setgray ",tempgrey);
			   writethis_PS
             }             	                            
          }	 
          if (jj==zj-1 && ii==zi-1){ /*at start draw outline/s -- for 2d and 1d*/
	    if (hilight_learn && color!=white){
                /*blank out outer*/
	        sprintf(textb[0],
                "gsave %d %d block_outer bcolor fill grestore\n",
			px+col*scale-leftoff-2,py+row*scale+node_y+leftoff+2);
             
	        writethis_PS
	        /*draw outline outer*/
                sprintf(textb[0],
                "gsave %d %d block_outer fcolor stroke grestore\n",
                      px+col*scale-leftoff-2,py+row*scale+node_y+leftoff+2);
                 
                writethis_PS
             }
	   
             if (node2d==1 || node2d==-1){
	        /*draw outline inner*/             
                sprintf(textb[0],
                  "gsave %d %d block_all fcolor stroke grestore\n",
		   px+col*scale-leftoff,py+row*scale+node_y+leftoff);
                writethis_PS
	     }
	  }
          /*draw cells*/
          sprintf(textb[0],"%d %d block1 fill\n", px+col*scale,py+row*scale+node_y);
          writethis_PS
	  if (b_show_2d_divs){
              draw_divs_basins_cg(node_y,px,py);
              writethis_PS
	  }
          dots_drawn=0;
       }
       
       /* show dots as 1 pix at zero value */
       if (b_show_dots && no_more_cells==0){
          if (this_value==0){
             if (dots_drawn==0){
               char tempgrey=0;
	        if (lattice_type=='g') tempgrey=1;
                color_ps(tempgrey,green); /*draw green dots*/
             }
             sprintf(textb[0],"%d %d dot1\n", 
                                px+col*scale+scale/2,py+row*scale+node_y-scale/2);
             writethis_PS  
	     dots_drawn=1;
          }
       }       	                        
    }/*next ii*/
 }/*next jj*/
 if (node2d==0 || node2d==-1) color_v[0]=tempcolor;
}

/************************ draw hexagons or squares ***************************/
void draw_divs_basins_cg(signed int node_y,
                                 signed int px, signed int py){

    /*0 setlinewidth for minimum division width, 
              looks just ok (very thin) in book, 
              but divs look too wide in pdf on screen*/
    sprintf(textb[0],"gsave 0 setlinewidth %d %d block1 bcolor stroke grestore\n", 
                          px+col*scale,py+row*scale+node_y);
}

/*************************************************************************************/
/***** create a PostScript file for space-(time) patterns, 1d & 2d, Nov 2007 *********/
void save_to_PostScript(char* array, signed int zi,signed int zj,signed int zh,
          unsigned int patch_size,char cellscale,char show_2d_divs,unsigned char divcolor,
		     char show_dots,char post_hex2d,char gaps_in_string){
  
 unsigned long pixc=0, old_pixc=0;
 float rgb_red=0,rgb_green=0,rgb_blue=0,temp_gray;
 
 char file_ret,lattice_type;
 signed int xbox,row_count,
              bbx,bby; /*max bounding box*/
 double hexside,hexoff,startx,starty,
        psi_direct=0,psj_direct=0,
        offx_direct=0,tempgrey,
        dot_scale,dcellscale;
 unsigned int poss;
 char this_value,old_value,dots_drawn=0,d_gray=0,temp_show_2d_divs;
 char gapx=0,temp_oned,temp_twod;
 unsigned char temp1color; /*for value_range==2, to change color of 1 cells to black*/
 
 /*lattice can be hex or square, with or  without divs and dots
   lattice_type s=symbols (different symbols up to vmaxk=4, but works up to 8)
                c=color 
                g=greyscale
                d=direct from space-time pattern, then d_gray (1,0)
 */

 /*fprintf(stderr,"\nshow_2d_divs=%d divcolor=%d show_dots=%d gaps_in_string=%d",
   show_2d_divs,divcolor,show_dots,gaps_in_string);*/

 if (value_range==2)  temp1color=color_v[1];

 temp_show_2d_divs=show_2d_divs;   
 
 setcolor(white);
 temp_oned=oned;
 temp_twod=twod;
 if (oned && forward_only==1 && d_mode==2){ /*for 1d stp as 2d*/   
    oned=0;
    twod=2;
 }
 instant_pic=0;

start_postscript:
 if (show_dots=='d'){
   if (zi*scale>Maxx-1) zi=(Maxx-1)/scale;
   max_zi=zi;
   if (zj*scale>Maxx-1) zj=(Maxy-1)/scale;
   max_zj=zj;
   if (zh==-2) instant_pic=2; /*2d direct from interrupt prompt*/
   else if (zh==-1){  /*1d direct from interrupt prompt*/
      instant_pic=1;      
      if (post_hex2d) post_hex2d=1; /*treat left and right slant the same*/
      if (scale==1) post_hex2d=0;
   }
   show_dots=0;
 }
 
 if (instant_pic==1){ /*scanning directly from interrupted stp top left*/ 
     xbox=draw_prompt_box(5);
     outtext("create PostScript image ");
     lattice_type='d';
     zi=max_zi;/*defaults*/
     zj=max_zj;
     outtext(" for 1d space-time pattern, exit-q");
     moveto(xbox,gety+CHeight);
     sprintf(textb[0],"1d size (max/def=%d):",max_zi);
     outtext(textb[0]);
     input_string(3,0);     
     if (is[0]=='q') goto end_PostScript;
     if (is[0]==13); 
     else if (atoi(is)<=max_zi && atoi(is)>=1) zi=atoi(is);
     sprintf(textb[0]," time-steps (max/def=%d):",max_zj);
     outtext(textb[0]);
     input_string(3,0);
     if (is[0]==13); 
     else if (atoi(is)<=max_zj && atoi(is)>=1) zj=atoi(is);
     if (is[0]=='q') goto start_postscript;
     moveto(xbox,gety+CHeight);
     if (d_gray==1) sprintf(textb[1],"g");
     else sprintf(textb[1],"c"); 
     sprintf(textb[0],
        "greyscale-g color-c (def-%s):",textb[1]);
     outtext(textb[0]);
     input_string(1,0);
     if (is[0]=='g') d_gray=1;
     else if (is[0]=='c') d_gray=0;
     if (is[0]=='q') goto start_postscript;
 }
 
 else{   
     xbox=draw_prompt_box(4);
     outtext("create PostScript image ");
     if (threed) sprintf(textb[0], "for 3d, %dx%dx%d", zi,zj/zh,zh);
     else if (twod) sprintf(textb[0], "for 2d, %dx%d", zi,zj);
     else if (oned)sprintf(textb[0], "for 1d, %dx%d", zi,zj);         
     outtext(textb[0]);
     moveto(xbox,gety+CHeight); 
     if (secondpart==2 && d_mode!=3){
        if (value_range==2) sprintf(textb[0], /*was !threed*/
           "symbols-s greyscale-g color-c/C direct-d exit-q (def-c):");
        else sprintf(textb[0], /*was !threed*/
           "symbols-s greyscale-g color-c direct-d exit-q (def-c):");
     }
     else{
        if (value_range==2) sprintf(textb[0],
            "symbols-s greyscale-g color-c/C exit-q (def-c):");
        else sprintf(textb[0], "symbols-s greyscale-g color-c exit-q (def-c):");
     }
     outtext(textb[0]);
     input_string(3,0); 
     if (is[0]=='q') goto end_PostScript;
     /*not direct until stp start*/
     if ((d_mode==3 && is[0]=='d') || (is[0]=='d' && secondpart!=2)) is[0]='c'; /*was threed==3*/
     else if (is[0]!='s' && is[0]!='g' && is[0]!='d'){
        if (value_range==2 && is[0]=='C') color_v[1]=white; /*to change color of 1 cells to black*/
        lattice_type='c';
     }
     else lattice_type=is[0];
     if (lattice_type=='d'){        
        setfillstyle(SOLID_FILL,black);
	bar(xbox,gety,Maxx-2,gety+CHeight);
        moveto(xbox,gety);
        if (d_gray==1) sprintf(textb[1],"g");
        else sprintf(textb[1],"c"); 
        sprintf(textb[0],
           "direct: greyscale-g color-c (def-%s):",textb[1]);
        outtext(textb[0]);
        input_string(1,0);
        if (is[0]=='q') goto start_postscript;
        if (is[0]=='g') d_gray=1;
        else d_gray=0;
     }
 }
 
 if (lattice_type=='d'){ /*direct from DDLab space-time pattern*/
     show_dots=0;
     psi_direct=psj_direct=(double)scale;
     /*y between hex centers*/
     if (instant_pic!=1 && post_hex2d==1 && cellscale>=3)/*hex laypout*/ 
                   psj_direct=psj_direct*cos(PI/6);
 }
 if (cellscale<=3) show_dots=0;
 if (show_dots==0) sprintf(textb[1],"off");
 else sprintf(textb[1],"on");

 
 if (show_2d_divs==0) sprintf(textb[2],"off");
 else{
       if (divcolor==black){
           if (bground=='w')  sprintf(textb[2],"w"); 
           else sprintf(textb[2],"b");
       }
       else if (divcolor==white){
           if (bground=='w')  sprintf(textb[2],"b");
           else sprintf(textb[2],"w");
       }
       else if (divcolor==lightgray) sprintf(textb[2],"g");
 }
 
 if (bground=='w'){ //for white background -- swap divs black/white, so defalt=background
    if (divcolor==black) divcolor=white;
    else if (divcolor==white) divcolor=black;
 }
 
 dcellscale=(double)cellscale;
 dot_scale=.7;

 moveto(xbox,gety+CHeight);
 sprintf(textb[0],"cellscale=%.2f dots(%s)=%.2f divs(%s), amend settings-a:",
                       dcellscale,textb[1],dot_scale,textb[2]);
 outtext(textb[0]);
 sprintf(textb[0],"");
 input_string(1,0);
 if (is[0]=='q') goto start_postscript;
 if (is[0]=='a'){
       moveto(xbox,gety+CHeight);
       outtext("change: cellscale:");
       input_string(4,0);
       if (is[0]==13);
       else if ((isdigit(is[0]) || is[0]=='.') &&
	     atof(is)>=0) dcellscale=atof(is);
       /*to force showing dots, or not*/
       outtext(" togdots-x:");
       input_string(1,0);    
       if (is[0]=='x') show_dots=abs(show_dots-1); 
       if (show_dots==1){
          outtext(" dotscale:");
          input_string(4,0);
          if (is[0]==13);
          else if ((isdigit(is[0]) || is[0]=='.') && atof(is)>=0) 
                      dot_scale=(float)atof(is);
       }
       outtext(" divs(0,b,w,g):");
       input_string(1,0);    
       if (is[0]==13);
       else if (is[0]=='0') show_2d_divs=0;
       else if (is[0]=='b'){
             divcolor=black;
	     show_2d_divs=1; 
       } 
       else if (is[0]=='w'){
             divcolor=white;
             show_2d_divs=1;
       }
       else if (is[0]=='g'){
             divcolor=lightgray;
             show_2d_divs=1;
       }       
 }
 if (dcellscale<=3) show_2d_divs=0; /*otherwize line overwrite image*/
 
 psi=psj=dcellscale;
 /*y between hex centers, and side, and offset, for hex laypout*/
 if (instant_pic!=1 && post_hex2d==1){
     psj=dcellscale*cos(PI/6);
     hexside=dcellscale*sin(PI/6);
     hexoff=psj-hexside;
 } 

 
 file_ret=select_filename("SAVE","postScript","*.ps","my_sPS");
 if (file_ret=='q') goto start_postscript;

 fid_PS=myopen(filename,1,0);
 sprintf(textb[0],"%%!PS-Adobe-3.0 EPSF-3.0\n");
 writethis_PS

 if (gaps_in_string && lattice_type!='d'){
    if (Maxx>=799) gapx+=3;
    else gapx+=2;
 }

 //bbx=(signed int)((zi+3)*psi);
 bbx=(signed int)((zi+3 + gapx)*psi );
 bby=(signed int)((zj+2)*psj);
 sprintf(textb[0],"%%%%BoundingBox: %d %d  %d %d\n",
      -(signed int)psi,
      -(signed int)psj*2,
      bbx + (signed int)ceil(zi/8) * gapx, /* + to allow for gaps if set*/
      bby);
 writethis_PS
 gapx=0;

/*fill with black for black background*/ 
/*           *<--*           ^  postscript coords x right, y up
                 |           y
	     *-->*  x-> */ 
 
 /*fill with black for black background (not just for symbols)*/          
 if (bground=='b'){  
    sprintf(textb[0],"gsave newpath %d %d moveto\n",-(signed int)psi,bby);
    writethis_PS
    sprintf(textb[0],"%d 0 rlineto\n",bbx+(signed int)psi*2);
    writethis_PS
    sprintf(textb[0],"0 -%d rlineto\n",bby+(signed int)psj*2);
    writethis_PS
    sprintf(textb[0],"-%d 0 rlineto closepath\n",bbx+(signed int)psi*2);
    writethis_PS
    sprintf(textb[0],"0 setgray fill grestore\n");
    writethis_PS               
 }
 /*for 1d lattice_type=='d', in gv, change scale to see all*/

 sprintf(textb[0],"/Helvetica findfont 10 scalefont setfont\n");/*for text*/
 writethis_PS
 

 row=1;
 y2d=zj*psj;

 if (lattice_type=='d') this_value=0;
 else  this_value=-1; /* to set color at start*/
 
 old_value=0;
 
 if (lattice_type=='d'){ 
    this_value=0;
    if (post_hex2d==1){
       startx=-psi_direct;
       starty=-psj_direct/2;
    }   
 }
 
 sprintf(textb[0],".5 setlinewidth 1 setlinecap\n"); 
 writethis_PS /*setlinewidth was 0 minimum limewidth, now .3  for better printing*/

 /*define a dot*/
 sprintf(textb[0],
   "/dot1{gsave %.2f setlinewidth 1 setlinecap moveto 0 0 rlineto stroke grestore} def\n",
                                                                     dot_scale);
   writethis_PS

 if (instant_pic==1 || post_hex2d==0){/*define a square
 
    0.  .1

    3.  .2  */

   sprintf(textb[0],"/block1{newpath moveto\n");
   writethis_PS
   sprintf(textb[0],"%.1f 0 rlineto\n",psi);
   writethis_PS
   sprintf(textb[0],"0 %.1f rlineto\n",-psi);
   writethis_PS
   sprintf(textb[0],"-%.1f 0 rlineto closepath} def\n",psi);
   writethis_PS
 }

 else{ /*define a hexagon
          1  
          .
      0.     .2

      5.     .3
          .
          4         */

   sprintf(textb[0],"/hex1{newpath moveto\n"); 
   writethis_PS
   sprintf(textb[0],"%.3f %.1f rlineto\n",psi/2,hexoff);
   writethis_PS
   sprintf(textb[0],"%.3f %.1f rlineto\n",psi/2,-hexoff);
   writethis_PS
   sprintf(textb[0],"0 %.3f rlineto\n",-hexside);
   writethis_PS
   sprintf(textb[0],"%.3f %.1f rlineto\n",-psi/2,-hexoff);
   writethis_PS
   sprintf(textb[0],"%.3f %.1f rlineto closepath} def\n",-psi/2,hexoff);
   writethis_PS
 }

 row_count=0;
 offx=offx_direct=0;
 dots_drawn=0;
 if (lattice_type=='s' && bground=='b'){
    sprintf(textb[0],"1 setgray "); 
    writethis_PS
 }
 

 for(jj=zj-1;jj>=0;jj--){ /*top row down*/
   row_count+=1;  
   row-=1;
   col=0;
   gapx=0;
   /*fprintf(stderr,"\nrow=%d row_count=%d ",row,row_count);*/
   if (post_hex2d==1){     
      if (row_count%2==0) offx=psi/2;  
      else offx=0;
      if (lattice_type=='d'){
         if (row_count%2==0) offx_direct=psi_direct/2;  
         else offx_direct=0;
         startx=-psi_direct; 
         starty+=psj_direct;
         starty=(double)((signed int)starty); /*to conform with update in forx.c*/
         if (starty>=Maxy-psj_direct) break; /*don't look for pix outside window*/
      }     
   }
   /*from left cell -> right*/
    for(ii=zi-1;ii>=0;ii--){     
       col+=1;

       if (gaps_in_string && lattice_type!='d'){
          if ((col-1)%8==0){
             if (Maxx>=799) gapx+=3;
	     else gapx+=2;
          }
       }

       poss=zi*jj + ii;
       /*to print 1d from seed window, where pattern is not rectangular*/
       if (oned==1 && lattice_type!='d'){
           poss=poss - ( (zi*zj)%patch_size);
           /*fprintf(stderr,"%d ", poss);*/
           if (poss==Maxu) break;
       } 
       if (lattice_type=='d'){       
          old_pixc=pixc; /* for direct */
          if (post_hex2d==1){ 
             startx+=psi_direct;
             if (startx>=Maxx-psi_direct) break; /*for 1d space-time*/
          }
       }
       else{
          old_value=this_value;
          this_value=get_value(array,poss);
          /*fprintf(stderr,"%d ",this_value);*/          
       }
      
       /*draw colored lattice as in DDLab, or grayscale*/
       if (lattice_type=='c' || lattice_type=='g'){
          /*skip bground cells - put dots (if set) on bground*/
          //dont skip to allow light green for zero cells, show dots below
	  /*if (this_value==0){
             if (show_2d_divs){ 
	        draw_divs_cg(lattice_type,divcolor,post_hex2d,gapx);
                writethis_PS
	     }
             goto dots;
	  }*/
	  /*first set the color or greyscale*/
          if (this_value!=old_value || dots_drawn==1){ /*reset color if different*/
	     if (lattice_type=='c'){	                   
                sprintf(textb[1],"%s",dcolor[color_v[this_value]]);
	        sprintf(textb[0],"%s setrgbcolor ",textb[1]);
			   writethis_PS
	     }	    
             else if (lattice_type=='g'){                          
	        if (bground=='w') 
                tempgrey = 1.0 - ((double)this_value)/(value_range-1);
                else if (bground=='b') tempgrey = ((double)this_value)/(value_range-1);
                if (value_range==3 && this_value==1) //make grey lighter
		                               tempgrey=.7;
                sprintf(textb[0],"%.3f setgray ",tempgrey);
			   writethis_PS
             }            	                            
          }
          /*fprintf(stderr,"x");*/
          if (post_hex2d==1){ /*filled hexagons*/
	       /*fprintf(stderr,"x");*/
	       sprintf(textb[0],"%.2f %.2f hex1 fill\n",
	                        col*psi+offx+gapx,row*psj+y2d);
               writethis_PS 
          }
          else{ /*or squares*/
                /*fprintf(stderr,"x");*/
                sprintf(textb[0],"%.2f %.2f block1 fill\n", 
                         col*psi-psi/2+gapx,row*psj+y2d);
                writethis_PS               
          }
          if (show_2d_divs){ 
	        draw_divs_cg(lattice_type,divcolor,post_hex2d,gapx);
                writethis_PS
	  }
          dots_drawn=0;
          if (this_value==0 && (lattice_type=='c' || lattice_type=='g')) goto dots;
          else continue;
       }

       else if (lattice_type=='d'){
          /*get rgb colors directly from 2d image*/	        
          if (instant_pic!=1 && post_hex2d==1){
             if (scale<=2) pixc=
                 getpixel(startx+psi_direct/2,abs(row)*psi_direct+psi_direct/2);
             else pixc=getpixel(startx+offx_direct+psi_direct/2,starty+psj_direct/2);
          }
          /*ddlab hex cells are square scale<=2, also for 1d, +offx for slant*/
          else{
	    if (scale>5) pixc=getpixel(ii*scale+2+offx_direct,jj*scale+2);
            else pixc=getpixel(ii*scale+1+offx_direct,jj*scale+1);
	       /*fprintf(stderr,"\n row_count=%d jj=%d offx_direct=%f",
		                     row_count,jj,offx_direct);*/
          }      
          /*skip color if repeated*/
          if (pixc!=old_pixc || dots_drawn==1){
                      
                rgb_red=((pixc>>16)&0xff);
                rgb_green=((pixc>>8)&0xff);
                rgb_blue=(pixc&0xff);

                if (d_gray==0){
                   sprintf(textb[1],"%.3f %.3f %.3f",
                     (float)rgb_red/255,(float)rgb_green/255,(float)rgb_blue/255);
                
	           sprintf(textb[0],"%s setrgbcolor ",textb[1]);
		      writethis_PS
		}
                else{
		   temp_gray=((float)rgb_red/255+(float)rgb_green/255+
                                                      (float)rgb_blue/255)/3;
                   sprintf(textb[0],"%.3f setgray ",temp_gray);
			   writethis_PS
		}
	  }
          /*fprintf(stderr,"x");*/
	  if (instant_pic!=1 && post_hex2d==1){ /*draw filled hexagons*/
	    if (instant_pic==2){ /*2d lattice hex lattice*/                
	        sprintf(textb[0],"%.2f %.2f hex1 fill\n", 
	                         col*psi+offx,row*psj+y2d);
              }
	      else  sprintf(textb[0],"%.2f %.2f hex1 fill\n",  /*for 1d slant*/
		          (float)ii*psi+offx,(float)((zj-jj)*psj));
              writethis_PS           
          }
          else{ /*draw colored squares*/
              sprintf(textb[0],"%.2f %.2f block1 fill\n", 
	                (float)ii*psi+offx,(float)((zj-jj)*psj));
              writethis_PS 
          }
          if (show_2d_divs){ 
	     draw_divs_d(d_gray,divcolor,post_hex2d,zj);
             writethis_PS
	  }
          dots_drawn=0;
          
          /*skip background cells - put dots if set*/ 
          if ((bground=='b' && pixc==0) ||
              (bground=='w' && pixc==16777215)){ /*draw dots on black*/
	     /*fprintf(stderr,"+");*/
             if (show_2d_divs){ 
	        draw_divs_d(d_gray,divcolor,post_hex2d,zj);
                writethis_PS
	     }
             goto dots;
          }          
          else continue;
       }

       /*draw black and white symbols, similar to Adamatzky*/
       else if (lattice_type=='s'){
         char stype[2];
	 /* dot  or black square/hex for zero or max*/
          if (this_value==0){             
             if (show_2d_divs){ 
	        draw_divs_cg(lattice_type,divcolor,post_hex2d,gapx);
                writethis_PS
	     }
             /*fprintf(stderr,"+");*/
             goto dots; /*skip for 0 value*/
          }
          if (this_value==value_range-1){ /*max value*/
	      stype[0]=0; stype[1]=6;
              draw_symbol(stype,post_hex2d,hexside);
	  }
          else if (value_range==3){
	      stype[0]=0; stype[1]=2; /*(small-black-disc)*/
              draw_symbol(stype,post_hex2d,hexside);
	  }
          else if (value_range==4){
	      if (this_value==1){ stype[0]=0; stype[1]=3;} /*(big-empty-disc)*/
              else if (this_value==2){ stype[0]=0; stype[1]=2;} /*(small-black-disc)*/
              draw_symbol(stype,post_hex2d,hexside);
	  }
          else if (value_range==5){
              if (this_value==1){ stype[0]=0; stype[1]=3;} /*(big-empty-disc)*/
              else if (this_value==2){ stype[0]=3; stype[1]=1;} /*(big+small empty-disc)*/
              else if (this_value==3){ stype[0]=0; stype[1]=2;} /*(small-black-disc)*/
              draw_symbol(stype,post_hex2d,hexside);
	  }
          else if (value_range==6){
              if (this_value==1){ stype[0]=0; stype[1]=3;} /*(big-empty-disc)*/
              else if (this_value==2){ stype[0]=3; stype[1]=1;} /*(big+small empty-disc)*/
              else if (this_value==3){ stype[0]=0; stype[1]=2;} /*(small-black-disc)*/
              else if (this_value==4){ stype[0]=0; stype[1]=4;} /*(small-black-disc)*/
              draw_symbol(stype,post_hex2d,hexside);
          }
          else if (value_range==7){
              if (this_value==1){ stype[0]=0; stype[1]=1;} /*(big-empty-disc)*/
	      else if (this_value==2){ stype[0]=0; stype[1]=3;} /*(big-empty-disc)*/
              else if (this_value==3){ stype[0]=3; stype[1]=1;} /*(big+small empty-disc)*/
              else if (this_value==4){ stype[0]=0; stype[1]=2;} /*(small-black-disc)*/
              else if (this_value==5){ stype[0]=0; stype[1]=4;} /*(big-black-disc)*/
              draw_symbol(stype,post_hex2d,hexside);
          
          }
          else if (value_range==8){
              if (this_value==1){ stype[0]=0; stype[1]=1;} /*(big-empty-disc)*/
	      else if (this_value==2){ stype[0]=0; stype[1]=3;} /*(big-empty-disc)*/
              else if (this_value==3){ stype[0]=3; stype[1]=1;} /*(big+small empty-disc)*/
              else if (this_value==4){ stype[0]=0; stype[1]=2;} /*(small-black-disc)*/
              else if (this_value==5){ stype[0]=0; stype[1]=4;} /*(big-black-disc)*/
              else if (this_value==6){ stype[0]=6; stype[1]=5;} 
                                                 /*(small-white-disc inside square/hex)*/
              draw_symbol(stype,post_hex2d,hexside);         
          }

          /*in between 
          v3 (small-black-disc)
          v4 (big-empty-disc) (small-black-disc)
          v5 (big-empty-disc) (big+small empty-disc) (small-black-disc)
          v6 (big-empty-disc) (big+small empty-disc) (small-black-disc)
                                                   (big-black-disc)
          v7 (small-empty-disc) (big-empty-disc) (big+small empty-disc) 
                               (small-black-disc) (big-black-disc)
          v8 (small-empty-disc) (big-empty-disc) (big+small empty-disc) 
                   (small-black-disc) (big-black-disc)
                                      (small-white-disc inside square/hex)

          types: 0. empty
                 1. (small-empty-disc)
                 2. (small-black-disc)
                 3. (big-empty-disc)
                 4. (big-black-disc)
                 5. (small-white-disc) filled with white
                 6. (square/hex full black)*/
                                 

          if (show_2d_divs){ 
	        draw_divs_cg(lattice_type,divcolor,post_hex2d,gapx);
                writethis_PS
	  }
          continue;
       }

      dots:
       /* in all cases, show dots as 1 pix at zero value */
       if (show_dots){
        if (this_value==0 ){
          if (dots_drawn==0){
	      if (lattice_type=='s'); 
	      else if (lattice_type=='g') sprintf(textb[0],
                                   " %f setgray\n",grayscale[green]);
              else sprintf(textb[0]," %s setrgbcolor\n",dcolor[green]);
              writethis_PS
          }
          if (lattice_type=='s' || lattice_type=='c' || lattice_type=='g'){
	     if (post_hex2d==1){
	        sprintf(textb[0],"%.2f %.2f dot1\n", 
	                        col*psi+offx+psj/2+gapx,row*psj+y2d-hexside/2);
                writethis_PS
             }
             else{ /*or squares*/
                sprintf(textb[0],"%.2f %.2f dot1\n", 
                         col*psi+gapx,row*psj+y2d-psj/2);
                writethis_PS
	     }
          }
          else if (lattice_type=='d'){         
	     if (instant_pic!=1 && post_hex2d==1){
                if (instant_pic==2) sprintf(textb[0],"%.2f %.2f dot1\n",
                         col*psi+offx+psj/2,row*psj+y2d-hexside/2);
             }
             else sprintf(textb[0],"%.2f %.2f dot1\n", /*includes 1d slant*/
                    (float)ii*psi+offx+psi/2,(float)((zj-jj)*psj-psj/2));
             writethis_PS
	  }
          dots_drawn=1;              
        }
       }    	                        
    }/*next ii*/
 }/*next jj*/

 /*title below graphic*/
 /*if (d_mode==1) sprintf(textb[1],"size=%d ",zi);
 else if (d_mode==2) sprintf(textb[1],"size=%dx%d ",zi,zj);
 sprintf(textb[2],"%s",filename);
 sprintf(textb[0],"newpath 10 -10 moveto 0 setgray (%s%s) show",textb[1],textb[2]);
 writethis_PS*/

 close(fid_PS); 
 goto start_postscript;

end_PostScript:
  oned=temp_oned;
  twod=temp_twod;
  show_2d_divs=temp_show_2d_divs;
  if (value_range==2) color_v[1]=temp1color;

/*then resultant file xxx.ps can be included in your tex source file 
 \epsfig{file=xxx.ps}*/

}


/************** draw hexagons or squares ***************************/
void draw_divs_cg(char lattice_type,unsigned char divcolor,char post_hex2d,char gapx){
    
    if (divcolor==lightgray){
       if (lattice_type=='c'){
          sprintf(textb[5],"%s",dcolor[oldcolor[7]]);/*darkgray*/ 
	  sprintf(textb[4],"%s setrgbcolor",textb[5]);
       }
       else if (lattice_type=='g'){ 
	 sprintf(textb[5],"%f",grayscale[oldcolor[7]]);
	 sprintf(textb[4],"%s setgray",textb[5]);
       }
    }
    else if (divcolor==black) sprintf(textb[4],"%d setgray",0);
    else if (divcolor==white) sprintf(textb[4],"%d setgray",1); 
    //else sprintf(textb[4],"%d setgray",show_2d_divs-1); /*black or white*/
    
    if (post_hex2d==1) //hexagons for divisions
       sprintf(textb[0],"gsave %s %.2f %.2f hex1 stroke grestore \n",
	                        textb[4],col*psi+offx+gapx,row*psj+y2d);    
    else  //squares for divisions
       sprintf(textb[0],"gsave %s %.2f %.2f block1 stroke grestore\n", 
                          textb[4],col*psi-psi/2+gapx,row*psj+y2d);
}

/************* draw hexagons or squares - direct *******************/
void draw_divs_d(char d_gray, unsigned char divcolor,char post_hex2d,signed int zj){

   if (divcolor==lightgray){
       if (d_gray==0){ /*color*/
          sprintf(textb[5],"%s",dcolor[oldcolor[7]]);/*darkgray*/ 
	  sprintf(textb[4],"%s setrgbcolor",textb[5]);
       }
       else if (d_gray==1){ /*grayscale*/
	 sprintf(textb[5],"%f",grayscale[oldcolor[7]]);
	 sprintf(textb[4],"%s setgray",textb[5]);
       }
    }
    else if (divcolor==black) sprintf(textb[4],"%d setgray",0);
    else if (divcolor==white) sprintf(textb[4],"%d setgray",1);
   //else sprintf(textb[4],"%d setgray",show_2d_divs-1);
    
    if (instant_pic!=1 && post_hex2d==1){ /*draw filled hexagons*/
       if (instant_pic==2) /*2d hex lattice*/                
	    sprintf(textb[0],"gsave %s %.2f %.2f hex1 stroke grestore\n", 
	       textb[4],col*psi+offx,row*psj+y2d);
       else sprintf(textb[0],"gsave %s %.2f %.2f hex1 stroke grestore\n",
	     textb[4],(float)ii*psi+offx,(float)((zj-jj)*psj)); /*for 1d slant*/
    }
    else /*draw colored squares*/
       sprintf(textb[0],"gsave %s %.2f %.2f block1 stroke grestore\n", 
	  textb[4],(float)ii*psi+offx,(float)((zj-jj)*psj));
}

/*********************** symbols **************************/
void draw_symbol(char stype[], char post_hex2d, double hexside){
  signed int z;

   sprintf(textb[0],""); 
   for (z=0; z<=1; z++){

     if (stype[z]==1){ /*(small empty-disc)*/
        if (post_hex2d==1) sprintf(textb[0],
              "%.2f %.2f %.2f 0 360 arc closepath stroke\n", 
                 col*psi+offx+psi/2,row*psj+y2d-hexside/2,psi/4);
        else sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath stroke\n",  
                            col*psi,row*psj+y2d-psj/2,psi/4);
     }
     else if (stype[z]==2){  /*(small-black-disc)*/
        if (post_hex2d==1) sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath fill\n",  
                            col*psi+offx+psi/2,row*psj+y2d-hexside/2,psi/3);
        else sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath fill\n",  
                            col*psi,row*psj+y2d-psj/2,psi/3);
     }
     else if (stype[z]==3){
        if (post_hex2d==1) /*(big-empty-disc)*/
                    sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath stroke\n",  
                            col*psi+offx+psi/2,row*psj+y2d-hexside/2,(psi/2)*0.9);
        else sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath stroke\n",  
                            col*psi,row*psj+y2d-psj/2,(psi/2)*0.9);
     }
     else if (stype[z]==4){
        if (post_hex2d==1) /*(big-empty-disc)*/
                    sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath fill\n",  
                            col*psi+offx+psi/2,row*psj+y2d-hexside/2,(psi/2)*0.9);
        else sprintf(textb[0],"%.2f %.2f %.2f 0 360 arc closepath fill\n",  
                            col*psi,row*psj+y2d-psj/2,(psi/2)*0.9);
     }
     if (stype[z]==5){ /*(small-white-disc) -- filled with white*/
        if (post_hex2d==1) sprintf(textb[0],
              "%d setgray  %.2f %.2f %.2f 0 360 arc closepath fill %d setgray\n", 
                 white,col*psi+offx+psi/2,row*psj+y2d-hexside/2,psi/4,black);
        else sprintf(textb[0],
                     "%d setgray %.2f %.2f %.2f 0 360 arc closepath fill %d setgray\n",  
                            white,col*psi,row*psj+y2d-psj/2,psi/4,black);
     }
     else if (stype[z]==6){ /*draw black filled hexagons/squares*/
        if (post_hex2d==1) sprintf(textb[0],"%.2f %.2f hex1 fill\n", 
                            col*psi+offx,row*psj+y2d);
        else sprintf(textb[0],"%.2f %.2f block1 fill\n", 
                         col*psi-psi/2,row*psj+y2d);
     }
     writethis_PS
   }   
}
/*****************************************************************************/
