
"""
based on the hopalong algorithm by Barry Martin
presented in the September 1986 Scientific American
Computer Recreations column by A. K. Dewdney
algorithm starts on page 15
"""

from tWrapper import tWrapper
#import math    # must be built-in, this isn't needed for sin and cos ?!

# wrapper for any additional drawing routines
# that need to know about each other
class turtleCoat(tWrapper):
    def sign(self, n):
        if n < 0:
            return -1
        else:
            return 1

    def hopalong(self, a=30.0, b=1.0, c=0.9, iterations=100000,
			xOffset=300.0, yOffset=300.0, scale=10.0):
        #xOffset = 300.0
        #yOffset = 300.0
        #scale = 10.0;

        x = 0.0
        y = 0.0

        # other interesting values for a, b, and c are
        """
        a = 73, b = 2.6, c = 25
        a = -200, b = .1, c = -80
        a = .4, b = 1, c = 0 (try a scale of 100 or 200 on this one)
        a = -3.14, b = .3, c = .3
        """
        #a = 30.0
        #b = 1.0
        #c = 0.9
        #iterations = 500000

        colors = ['red', 'orange', 'yellow', 'white', 'green', 'blue', 'purple']
        
        #print len(colors)
        colorIter = iterations / len(colors)
        #colorIter = iterations / 500
        k = 0

        for i in range(len(colors)):
        #for i in range(colorIter):
            print k, colors[i]
            #self.color(i, 0, 0)
            self.color(colors[i])
            for j in range(colorIter):
                k = k + 1
                temp = y - self.sign(x) * sqrt(abs(b * x - c))
                y = a - x
                x = temp
                self.plot(xOffset + (x * scale), yOffset + (y * scale))
                # the calculation and drawing below take approx. 2/3 the time
                # compared to using plot
                #x2 = xOffset + (x * scale)
                #y2 = yOffset + (y * scale)
                #self.dc.DrawLine(x2, y2, x2 + 1, y2 + 1)
        #print k, "DONE"


#def drawMain(dc_local, w, rs=reset, turtleCoat=turtleCoat):
def drawMain(dc_local, w, turtleCoat=turtleCoat):
    t = turtleCoat(dc_local)
    # chaos1 (Ctrl-1) takes 2.2 seconds versus 7.14 seconds
    # for this chaos1 done as an exec
    t.setBackColor('black')
    t.cls()
    """
    a = 73, b = 2.6, c = 25
    a = -200, b = .1, c = -80
    a = .4, b = 1, c = 0 (try a scale of 100 or 200 on this one)
    a = -3.14, b = .3, c = .3
    """

    # beware of doing many iterations on a slow machine
    # 500,000 iterations takes approximately 15 seconds on my AMD 1.2GHz Windows 2000 box
    # with a GeForce 2 video card, so an older machine is likely to take much longer
    # for reference, a similar Java applet I wrote takes under 5 seconds to do 500,000
    # iterations on the same machine using the Sun JDK 1.3 and approximately 1 second
    # using the IBM JDK 1.3
    # if the self.dc.DrawLine ... is used above instead of the plot, drawing takes
    # under 11 seconds and if no drawing is done, just the loop and calculations
    # it takes 3 seconds to do 500,000 iterations
    # some of the difference is likely Python compared to Java
    # but some of it is how screen i/o is done, which is generally the slowest
    # part of any drawing program, especially without any optimizations to the
    # underlying APIs and drivers like DirectX
    #
    # The important point is that it is fast enough.
 
    # bump this up to 500000 or more iterations to get interesting
    # i set it at 100000 in case someone tried it on a slow machine
    t.hopalong(30.0, 1.0, 0.9, 100000, 300.0, 300.0, 10.0)
    #t.hopalong(73.0, 2.6, 25, 500000, 300.0, 300.0, 5.0)
