#!/bin/env python3

import argparse
import math
import matplotlib.pyplot as plt

units = [1, -1, 1j, -1j]

def norm(x):
	return x.real*x.real + x.imag*x.imag

def gidivisible(a, b):
	x = a*b.conjugate()
	nb = norm(b)
	return (x.real % nb == 0) and (x.imag % nb == 0)

def gidivisors(n):
	r = set()
	nn = norm(n)
	# for each point on the lattice
	for x in range(0, n+1):
		for y in range(0, n+1):
			# we check if it divides the number
			g = complex(x, y)
			if g != 0 and gidivisible(n, g):
				r.update([g*u for u in units])
	return r

def points_from_divisors(d):
	return [(x.real, x.imag) for x in d]

def plot(x):
	data = points_from_divisors(gidivisors(x))
	plt.scatter([d[0] for d in data], [d[1] for d in data])
	plt.show()

def csv(x, text=True):
	r = []
	for p in points_from_divisors(gidivisors(x)):
		r.append("{},{},{}".format(int(p[0]), int(p[1]), x))
	if text:
		return "\n".join(r)
	else:
		return r

if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	parser.add_argument("value", type=int)
	parser.add_argument("--csv", action="store_true")
	args = parser.parse_args()
	if args.csv:
		print(csv(args.value))
	else:
		plot(args.value)

