Hexagonal playing fields

Started by
2 comments, last by michaeltaft 22 years, 10 months ago
Here is some code I''m using to generate a hexagonal playing field for a very, very simple little Java game. The math I''m using allows me to change the size of the hexes, if I want to. The thing that bugs me about this code is that it redraws about 50% of the lines every time = wasted processing effort. No biggy with such a little game. I''m curious, however, if somebody has a better algorithm for cranking out a hexagonal grid. Thanks. Here''s the code: for (double z=0;z<=12;z++){//number of rows double size = 15;//this number controls hex sizing double cos =Math.ceil(size * 0.50000);//this number is fixed because shape is hex double sin = Math.ceil(size * 0.86602540378);//this number is fixed because shape is hex for (double a=0;a<=19;a++){//number of columns int yC = (int)((size + cos) * z); int xC = (int)((sin * 2) * a); if (z%2 == 1){xC = (int)(xC + sin);} double hS = Math.ceil(size / 2); int halfSize = (int)hS; int sizeInt = (int)size; int sinInt = (int)sin; int aY = yC - sizeInt; int aX = xC; int bY = yC - halfSize; int bX = xC - sinInt; int cY = yC + halfSize; int cX = xC - sinInt; int dY = yC + sizeInt; int dX = xC; int eY = yC + halfSize; int eX = xC + sinInt; int fY = yC - halfSize; int fX = xC + sinInt; int x1Points[] = {aX,bX,cX,dX,eX,fX}; int y1Points[] = {aY,bY,cY,dY,eY,fY}; g2.drawPolygon(x1Points, y1Points, x1Points.length);//draws a polygon
Advertisement
You can only draw three lines for each hex. The other three lines will be drawn by its neighbours. Two things : you have to draw the same lines in all hexes (say, top, left upper and left lower), and you have to draw all of the lines for some border hexes.
Thanks, Diodor -
I understand the concept. Curious if you have the algorithm handy.
Not much of an algorithm. Its a hack of your program. Draws full hexes on the map edges and 3-line hexes for the inner hexes. Here''s the C code, directly derived from yours :

#include #include double size = 15, cs = 15 * 0.5, sn = 15 * 0.866;void hex3 (int a, int z){		int xC, yC, aX, aY, bX, bY, cX, cY, dX, dY, eX, eY, fX, fY, halfSize;        int sizeInt, sinInt;		double hS;		yC = (int)((size + cs) * z);		xC = (int)((sn * 2) * a);		if (z%2 == 1){xC = (int)(xC + sn);}		hS = size / 2.0;		halfSize = (int)hS;		sizeInt = (int)size;		sinInt = (int)sn;		aY = yC - sizeInt;		aX = xC;		bY = yC - halfSize;		bX = xC - sinInt;		cY = yC + halfSize;		cX = xC - sinInt;		dY = yC + sizeInt;		dX = xC;		line (aX, aY, bX, bY);		line (bX, bY, cX, cY);		line (cX, cY, dX, dY);}void hex6 (int a, int z){		int xC, yC, aX, aY, bX, bY, cX, cY, dX, dY, eX, eY, fX, fY;		int halfSize, sizeInt, sinInt;		double hS;		yC = (int)((size + cs) * z);		xC = (int)((sn * 2) * a);		if (z%2 == 1){xC = (int)(xC + sn);}		hS = size / 2.0;		halfSize = (int)hS;		sizeInt = (int)size;		sinInt = (int)sn;		aY = yC - sizeInt;		aX = xC;		bY = yC - halfSize;		bX = xC - sinInt;		cY = yC + halfSize;		cX = xC - sinInt;		dY = yC + sizeInt;		dX = xC;		eY = yC + halfSize;		eX = xC + sinInt;		fY = yC - halfSize;		fX = xC + sinInt;		line (aX, aY, bX, bY);		line (bX, bY, cX, cY);		line (cX, cY, dX, dY);		line (dX, dY, eX, eY);		line (eX, eY, fX, fY);		line (fX, fY, aX, aY);}void main (void){	int gd, gm;	int z, a;	detectgraph (&gd, &gm);	initgraph (&gd, &gm, "h:\\borlandc\\bgi");	for (z=2;z<13;z++)		for (a=2;a<20;a++)			hex3 (a, z);	for (z=1;z<=13;z++)	{		hex6 (1, z);		hex6 (20, z);	}	for (a=1 ; a<=20 ; a++)	{		hex6 (a, 1);		hex6 (a, 13);	}	getch ();	closegraph ();} 

This topic is closed to new replies.

Advertisement