## Grid width
# The width of the grid in vertices.
## Grid height
# The height of the grid in vertices.

# The argument specifications start on the first line and continue until
# a non-commented line (like the one before this comment). An argument is
# specified by a double pound sign and the corresponding tool tip text
# follows on every subsequent single pound sign line.

# If there is an error with one of the arguments, output the problems to
# stderr and then exit with status 1.

import sys	# imported to get the command line arguments.

#################################################
# Arguments:					#
# sys.argv[0] = the path of this program	#
# sys.argv[1] = the path of the output file	#
# sys.argv[2] = the 1st argument		#
# sys.argv[3] = the 2nd argument		#
# ...						#
# sys.argv[n+1] = the nth argument		#
#################################################

# If there are too few arguments then one of the fields must not have 
# been filled out.
if len(sys.argv) < 4:
    sys.stderr.write("Please fill out all fields.")
    exit(1)			# Exit with status 1

file_name = sys.argv[1]		# The file to which to output the sgf code.

width = 0
height = 0

try:
	# Attempts to set the width of the graph
    width = int(sys.argv[2])
except:
	# If the width is not a number (contains non-numeric characters).
    sys.stderr.write("The width must be a positive number.")
    exit(1)
    
try:
	# Attempts to set the height of the graph
    height = int(sys.argv[3])
except:
	# If the height is not a number (contains non-numeric characters).
    sys.stderr.write("The height must be a positive number.")
    exit(1)
    
# If the width is negative...
if width < 1:
	sys.stderr.write("The width must be a positive number.");
	exit(1)

# If the height is negative...
if height < 1:
	sys.stderr.write("The height must be a positive number.");
	exit(1)
	
vertices = width * height
edges = 2 * width * height - width - height

from math import *

x_sep = 40
y_sep = 40

x_space = x_sep * (width - 1)
y_space = y_sep * (height - 1)

x_start = 400 - x_space / 2
y_start = 300 - y_space / 2

while x_start < 0 or y_start < 0:
    x_sep -= 10
    y_sep -= 10

    x_space = x_sep * (width - 1)
    y_space = y_sep * (height - 1)

    x_start = 400 - x_space / 2
    y_start = 300 - y_space / 2

    first = 0

output = open(file_name, 'w')
output.write(str(vertices) + " " + str(edges) + "\n")
for i in range(1, width * height):
	if i % width != 0:
		output.write(str(i) + " " + str(i+1) + "\n") 
	if ceil(float(i) / width) < height:
		output.write(str(i) + " " + str(i+width) + "\n")
for i in xrange(vertices):
	row = int(floor(float(i) / width))
	col = i % width
	output.write(str(x_start + col * x_sep) + " " + str(y_start + row * y_sep) + "\n")
output.close()
