'''
This program fires at the pixels on a frame.
The Geohashing Algorithm is used to generate
random pixels position.
'''

import md5, datetime, struct, re, sys, wx

# choose a size
WIDTH = 500
HEIGHT = 500

def randomCoords(date,dj):	
	#from the official implementation
	sum = md5.new("%s-%s" % (date, dj)).digest()
	breite = struct.unpack(">Q", sum[0:8])[0] / (2.**64)
	lange = struct.unpack(">Q", sum[8:16])[0] / (2.**64)
	return (breite*WIDTH,lange*HEIGHT)

class MainWindow(wx.Frame):
	def __init__(self, parent, id, title):
		start = datetime.datetime.today()#rememer time we started simulation
		#initialize frame and stuff
		wx.Frame.__init__(self, parent, id, title)
		self.SetPosition(wx.Point(0,0))
		self.SetClientSize(wx.Size(WIDTH,HEIGHT))
		
		self.SetBackgroundColour(wx.BLACK)
		self.Show(True)
		
		dc = wx.ClientDC(self);
		
		#create a canvas to paint on
		canvas = wx.EmptyImage(WIDTH,HEIGHT)
		
		i = 0
		color = 0 # i dont know if this is necessary
		red = 0 # i dont know if this is necessary
		date = "2008-03-11" #use a fixed date
		while i < WIDTH*HEIGHT: #in a perfect world, we would hit every pixel exactly once
			coords = randomCoords(date,11000.0+i*0.000002) #use some DIJA-like values
			red = canvas.GetRed(coords[0],coords[1])
			red+=50 #a pixel becomes more red the more it is hit
			if red > 255: red = 255
			# we paint to the canvas and the frame so you can watch the progress
			dc.SetPen(wx.Pen(wx.Colour(red,0,0), 1))
			dc.DrawPoint(coords[0],coords[1])
			canvas.SetRGB(coords[0],coords[1],red,0,0)
			i+=1
		# display the canvas in the frame
		wx.StaticBitmap(self, wx.ID_ANY, wx.BitmapFromImage(canvas), (0, 0), (WIDTH,HEIGHT))
		self.SetTitle('Randomity-Check took %s'%str(datetime.datetime.today()-start))
		
app = wx.PySimpleApp()
frame=MainWindow(None, wx.ID_ANY, 'Randomity-Check')
app.MainLoop()
