hex map calculations

By Peter L. Berghold ·

TML message #823From bundle 77

Not too long ago somebody asked who to go about creating hex maps
with a computer.  What follows is a rather "dirty"program that I wrote
not too long ago to create subsector map grids with troff.  It is
written in C and is portable to MSDOS with the LATTICE C compiler.  Its
output should be fed through PIC before submitting to troff.   I wholeheartedly
make no claims on it whatsoever and it is free for all to use as they see
fit.

- ----------------------------> CUT HERE <--------------------------------------


#include <math.h>
#include <stdio.h>
#ifndef LATTICE
#define CritAng	( (2 * M_PI) / 6.0 )
#else
#define CritAng ( (2 * PI ) / 6.0 )
#endif

main()
{
	int	Vertex;
	int	ix,iy;
	double	Radius;
	double  Ymove;
	double  Xmove;
	double	x,y;
	FILE	*fout;

	Radius= 0.5/sin( CritAng );	/* Sixty degree angle */
	Xmove= Radius + ( cos( CritAng ) * Radius) ;
	Ymove= 2.0*cos (CritAng)  * sin( CritAng / 2.0 ); /* Thirty Degrees */

	fout=fopen("hex.pic","w");

	fprintf(stdout,".PS 7 5 \n");
	fprintf(stdout,"xmove=%.3g\n",Xmove);
	fprintf(stdout,"ymove=%.3g\n",Ymove);
	fprintf(stdout,"define Hex X\n");
	fprintf(stdout,"cx=$1\ncy=$2\n");


	for (Vertex=1; Vertex <= 6; Vertex++){
		y= Radius * sin ( (double) (Vertex-1) * CritAng) ;
		x= Radius * cos ( (double) (Vertex-1) * CritAng) ;

		fprintf(stdout,"hexx%d=%.3g\n",Vertex,x);
		fprintf(stdout,"hexy%d=%.3g\n",Vertex,y);
	}

	fprintf(stdout,"move to (cx+hexx1,cy+hexy1)\n");

	for(Vertex=6;Vertex>=1;Vertex--){
		fprintf(stdout,"line to (cx+hexx%d,cy+hexy%d)\n",Vertex,Vertex);
	}

	fprintf(stdout,"\"\\s-2$3$4\\s0\" at (cx,hexy3+cy) below\n");

	fprintf(stdout,"X\ndefine CreateMap X\n");
	for(ix=1;ix<=8;ix++)
		for(iy=1;iy<=10;iy++){
		if (ix%2 == 0 ) y=(double)iy + Ymove;
		else y=(double)iy;
		x=(double)(ix-1) * Xmove;
		y= 11-y;
		if ( iy < 10 )
			fprintf(stdout,"Hex(%.3g,%.3g,0%d,0%d)\n",x,y,ix,iy);
		else
			fprintf(stdout,"Hex(%.3g,%.3g,0%d,%d)\n",x,y,ix,iy);
	}
	fprintf(stdout,"X\nCreateMap()\n\n.PE\n");

	fclose(fout);
}



- --
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
/*		Peter L. Berghold					*/
/*		System Administrator					*/
/*		AT&T Red Hill Systems Administration Group		*/
/*		1F138	+1 [phone removed]				*/
/*		EMAIL (UUCP):	{[email removed]|att}!violin!plb		*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */


All opinions and material above is the responsibility of the originator.
Submissions: [email removed], or [email removed]
Administrator: [email removed] (James Perkins)
The TML is made possible by facilities provided by Tektronix, Inc.