#!/usr/bin/env python

#HOPALONG PROGRAM Version CREATED 24 Feb 2021.
#Python 2.7.10 (default, Apr  6 2013, 19:55:15) [MSC v.1500 64 bit (AMD64)] on win32
#Type "copyright", "credits" or "license()" for more information.

""" Based on the original program (Hopalong) by Barry Martin, which featured on the
cover of Scientific American in September 1986.
"""

print();print()
print("The images may be saved in a user input filename which increments automatically witha pre-pended number.\n")

print("Successive images may be saved by hitting the ESCAPE key or the 'p' key.\n")

print("A final image is saved on quitting with the CLOSE icon.\n")

print("They may be saved as .bmp .tga .png or .jpg\n")


print("The input filename must be included in quote marks for Python2.")





#***********************************************
#Load required software
import pygame,sys,random
import pygame.gfxdraw
import math
from pygame.locals import *
pygame.init()

#************************************************
#Input pattern constants and running parameters
fnm = str(input("\nEnter File name to save image \n\n- FOR Python2 MUST BE IN QUOTES \n\n Can be .bmp, .png. .jpg, .tga "))
print(fnm) 

picnum = int(input("starting value for filename "))
print(picnum)

A = float(input("enter A = "))
B = float(input("enter B = "))
scale = float(input("enter scale - size of image - 20 is good = "))
#iterations =int(input("number-of-iterations = "))
# Get input data to add to saved filename
inf = "_" + str(A) +"_" + str(B) +"_" +str(scale) + "_"
print("info ",inf)
#*************************************************
#Determine colours
col=0
red=(250,0,0); green=(0,240,0); blue=(0,0,250)
lred=(240,50,0); lgreen=(100,200,0); lblue=(0,100,250)
cyan=(0,250,0); yellow=(250,250,0); magenta=(250,100,0);white=(250,250,250)        
colr=[red,green,blue,lred,lgreen,lblue,yellow,cyan,magenta]

#**********************************************
#Distance of graphics screen from LHS of screen
x = 120
#Distance of graphics screen from top of screen
y = 160
#Set up graphics screen
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
#*************************************************
#Size of grahics screen
#screen = pygame.display.set_mode((100,100))
x = 1200 ; y = 900
xc = x//2; yc = y//2

pygame.display.set_caption("Hopalong Martin")
window = pygame.display.set_mode((x,y), 0 ,32)
#************************************************

#Fixed constants and initial values
C=1
xcentre=xc;ycentre=yc;
x=0.0001;y=0;z=0;x1=0;y1=0
X=0;Y=0
mX=0
mY=0
n=0;m=0
count=100


#***********************************

#Hopalong equations******************************************
def hopalong1():
        x1=y-(x/math.fabs(x))*math.sqrt(math.fabs(B*x-C))
        return x1       
def hopalong2():
        y1=A-x
        return y1        
#Rotate axes    
def rotateX():
        X=1/1.414*(x+y)
        return X       
def rotateY():
        Y=1/1.414*(x-y)
        return Y
#Plot a pixel
def plot(xcentre,ycentre,X,Y,scale):      
        return pygame.gfxdraw.pixel(window,int(xcentre+scale*(X)),int(ycentre+scale*(Y)),colr[col])

#Save window as imagefile
# can save as .bmp .tga .png or .jpg  
def savepic():

        pygame.image.save(window, str(picnum)+ inf + fnm)
        print("file ", picnum, " has been saved")
        
def filename(picnum,fnm):
        fname =str(picnum) + fnm
        return fname
#******************************************************

# Calculate average coordinates of first 'count' plots for use on centering picture
for m in range(0,count):
        x1=hopalong1()  
        y1=hopalong2()
        X=rotateX()
        Y=rotateY()
        x=x1;y=y1
        mX=mX+X;mY=mY+Y
        
mX=(mX/count);mY=(mY/count)
x=0.001;y=0


#Main program to generate hopalong


#for n in range(iterations):
n=0
window.fill(white)
while True:
        '''The code below quits the program if the X button is pressed'''
        for event in pygame.event.get():
                if event.type == QUIT:
                        savepic()
                        picnum += 1
                        pygame.quit()
                        print("count= ", n)
                        sys.exit()
                elif event.type == pygame.KEYDOWN:
                        # optional Save with escape key
                        if event.key == pygame.K_ESCAPE:
                                savepic()
                                print("count= ", n)
                                picnum += 1
                        if event.key == pygame.K_p:        
                                savepic()
                                print("count= ", n)
                                picnum += 1
                
               
        
        X=rotateX()
        Y=rotateY()
        plot(xcentre,ycentre,X-mX,Y-mY,scale)
        
        x1=hopalong1()
        y1=hopalong2()
        x=x1
        y=y1
        n += 1
        
#Select colour
# Change colour randomly every 20000
        
        
        if (n % 20000 == 0) :
                col= (col+1)%8
                
# Udate display every 100 iterations        
        if (n % 100 == 0) :
                pygame.display.update()
#        pygame.display.update()

savepic()

pygame.quit()
sys.exit()
        
#******************************************************
