Hex mapping algorithm

By James T. Perkins ·

TML message #780From bundle 74

This article looks appropriate.  My policy is, in general, when someone
posts something to USENET news, it can be redistributed to the list
without explicit permission, as long as it remains completely intact.

- ------- Forwarded Message

From: Bertil Jonell

Hi!
I remember someone asked about algorithms for hex mapping a while back
There is a posting on that by Jon Watte ([email removed])
I repost it here and I hope he won't be mad at me (again :-)

- - -------------------------------------------------------------------------------
Article 1220 of rec.games.programmer:
Path: [email removed]
From: [email removed] (Jon Watte)
Newsgroups: rec.games.programmer
Subject: Re: hex map
Message-ID: [email removed]
Date: 13 Jan 90 14:00:57 GMT
References: [email removed]
Reply-To: [email removed] (Jon W{tte)
Organization: Royal Institute of Technology, Stockholm, Sweden
Lines: 96

In article [email removed] [email removed] (Barry Wood) writes:
>A friend of mine is interested in any logarithms, etc that might help him
				       ~~~~~~~~~~

Algorythms, I presume. (*I* don't have english for native language,
so don't blame my spelling)

2 minutes of thought yields:

A hex map can be represented in a plain 2d array. From now on I
presume you are familiar with hex numbering:

0000        0002        0004
      0001        0003
0100        0102        0104
      0101        0103
0200        0202        0204

etc.

One easily notes that every second column is half a hex lower
down. This is no problem; just raise it:

0000  0001  0002  0003  0004

0100  0101  0102  0103  0104

etc.

Now, to display this array (say, array[X_SIZ][Y_SIZ] of char)
you use the following function:

void
display(a)
char ** a;
{
	int x, y;

	for(y=0; y<Y_SIZ; x++) {
		for(x=0; x<X_SIZ; y++) {
			move(x, x&1 + y*2);
			addch(a[x][y]);
		}
	}
	refresh();
}

if move(x,y) moves x == column, y == row (curses is other way round)

Second problem, movement. Say you are in hex yyxx and want to go
in direction d where 0 = N, 1 = NE, 2 = SE etc. (North == up)
the quick & dirty solution is:

void
move(x, y, d)
int *x, *y, d;
{
	switch(d) {
	case 0:
		*y--;
		break;
	case 1:
		*y -= (*x &1) ? 0 : 1;
		*x++;
		break;
	case 2:
		*y += (*x &1);
		*x++;
		break;
	case 3:
		*y++;
		break;
	case 4:
		*y += (*x &1);
		*x--;
		break;
	case 5:
		*y -= (*x &1) ? 0 : 1;
		*x--;
		break;
	default:
		/* Invalid move */
		;
	}
}

Of course, you'll want to check against movements running of
the map too... :-)

These functions do everything for you - it isn't any harder
than that !

h+
- - --
   ---  Stay alert !  -  Trust no one !  -  Keep your laser handy !  ---
             [email removed] ==  [email removed] ==  Jon Watte
                    longer .sig available on request

- ------- End of Forwarded Message

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.