#!/usr/bin/python2.3

from	math		import 	sqrt
from	optparse	import	OptionParser
from	string		import	atoi, atof

#	Postscript page is 612 by 792 points, lower left is 0, 0.

#	Get a command line option parser instance.
parser = OptionParser()
parser.add_option(
	"-x",
	"--xOffset",
	dest="xOffset",
	default='0',
	help="X offset from both PS edges",
	metavar="POINTS" )
parser.add_option(
	"-y",
	"--yOffset",
	dest="yOffset",
	default='0',
	help="Y offset from both PS edges",
	metavar="POINTS" )
parser.add_option(
	"-g",
	"--gray",
	dest="gray",
	default='0.5',
	help="Gray scale value of hexes from 0 to 1",
	metavar="VALUE" )
parser.add_option(
	"-l",
	"--lineWidth",
	dest="lineWidth",
	default='0.5',
	help="Line width in points (default 0.5)",
	metavar="POINTS" )
parser.add_option(
	"-w",
	"--width",
	dest="hexs",
	default='16.0',
	help="Hexes across (default 16)",
	metavar="NUMBER" )
parser.add_option(
	"-j",
	"--join",
	dest="join",
	default=False,
	action="store_true",
	help="Make hexes joinable",
	metavar="BOOLEAN" )
(options, args) = parser.parse_args()

lineWidth = atof( options.lineWidth )
gray      = atof( options.gray )
xOffset   = atoi( options.xOffset )
yOffset   = atoi( options.yOffset )
hexs      = atof( options.hexs )
join      = options.join

pageHeight = 768 - 2 * yOffset
pageWidth  = 612 - 2 * xOffset

#	Number of hexes across
radi = (hexs / 2.0) * 6.0

#	Calculate values.
radius   = int( pageWidth / radi )
diameter = radius * 2.0
offset   = int( sqrt(diameter * diameter - radius * radius) + 0.5 )

#	Set values.
height   = (pageHeight - 2 * offset) / (2 * offset)
width    = int( hexs / 2 )

#	Start path and new file.
print "%!PS"
print "%% %d, %d" % (width, height)
print "%f setlinewidth" % lineWidth
print "newpath"

def drawHexes(
	radius,
	diameter,
	gray,
	join ):

	#	Initialize starting point.
	x    = xOffset
	y    = yOffset + radius + offset
	yLow = yOffset + radius

	#	Draw hex line down.
	print "%d %d newpath moveto" % (x, y)
	print "%f setgray" % gray
	for j in range(0, width):
		#	Draw line segment 1.
		x = x + radius
		print "%d %d lineto" % (x, yLow)

		#	Draw line segement 2.
		x = x + diameter
		print "%d %d lineto" % (x, yLow)

		#	Draw line segement 3.
		x = x + radius
		print "%d %d lineto" % (x, y)

		#	Draw line segement 3.
		if join or j < width - 1:
			x = x + diameter
			print "%d %d lineto" % (x, y)

	#	Draw line.
	print "stroke"

	#	Initialize starting point.
	x    = xOffset
	yLow = y
	y    = y + offset

	#	Draw line segments.
	print "%d %d newpath moveto" % (x, yLow)
	for j in range(0, width):
		#	Draw line segment 1.
		x = x + radius
		print "%d %d lineto" % (x, y)

		#	Draw line segement 2.
		x = x + diameter
		print "%d %d lineto" % (x, y)

		#	Draw line segement 3.
		x = x + radius
		print "%d %d lineto" % (x, yLow)

		#	Draw line segement 3.
		if join or j < width - 1:
			x = x + diameter
			print "%d %d moveto" % (x, yLow)

	#	Draw line.
	print "stroke"

	#	For the number of lines, do:
	for i in range(0, height):
		#	Initialize starting point.
		x    = xOffset
		yLow = y
		y    = y + offset

		#	Draw hex line down.
		print "%d %d newpath moveto" % (x, y)
		for j in range(0, width):
			#	Draw line segment 1.
			x = x + radius
			print "%d %d lineto" % (x, yLow)

			#	Draw line segement 2.
			x = x + diameter
			print "%d %d moveto" % (x, yLow)

			#	Draw line segement 3.
			x = x + radius
			print "%d %d lineto" % (x, y)

			#	Draw line segement 3.
			if join or j < width - 1:
				x = x + diameter
				print "%d %d lineto" % (x, y)

		#	Draw line.
		print "stroke"

		#	Initialize starting point.
		x    = xOffset
		yLow = y
		y    = y + offset

		#	Draw line segments.
		print "%d %d newpath moveto" % (x, yLow)
		for j in range(0, width):
			#	Draw line segment 1.
			x = x + radius
			print "%d %d lineto" % (x, y)

			#	Draw line segement 2.
			x = x + diameter
			print "%d %d lineto" % (x, y)

			#	Draw line segement 3.
			x = x + radius
			print "%d %d lineto" % (x, yLow)

			#	Draw line segement 3.
			if join or j < width - 1:
				x = x + diameter
				print "%d %d moveto" % (x, yLow)

		#	Draw line.
		print "stroke"

#	Draw hexes.
drawHexes(
	radius,
	diameter,
	gray,
	join )
print "showpage"
