3
based on the hopalong algorithm by Barry Martin
4
presented in the September 1986 Scientific American
5
Computer Recreations column by A. K. Dewdney
6
algorithm starts on page 15
9
from tWrapper import tWrapper
10
#import math # must be built-in, this isn't needed for sin and cos ?!
12
# wrapper for any additional drawing routines
13
# that need to know about each other
14
class turtleCoat(tWrapper):
21
def hopalong(self, a=30.0, b=1.0, c=0.9, iterations=100000,
22
xOffset=300.0, yOffset=300.0, scale=10.0):
30
# other interesting values for a, b, and c are
32
a = 73, b = 2.6, c = 25
33
a = -200, b = .1, c = -80
34
a = .4, b = 1, c = 0 (try a scale of 100 or 200 on this one)
35
a = -3.14, b = .3, c = .3
42
colors = ['red', 'orange', 'yellow', 'white', 'green', 'blue', 'purple']
45
colorIter = iterations / len(colors)
46
#colorIter = iterations / 500
49
for i in range(len(colors)):
50
#for i in range(colorIter):
54
for j in range(colorIter):
56
temp = y - self.sign(x) * sqrt(abs(b * x - c))
59
self.plot(xOffset + (x * scale), yOffset + (y * scale))
60
# the calculation and drawing below take approx. 2/3 the time
61
# compared to using plot
62
#x2 = xOffset + (x * scale)
63
#y2 = yOffset + (y * scale)
64
#self.dc.DrawLine(x2, y2, x2 + 1, y2 + 1)
68
#def drawMain(dc_local, w, rs=reset, turtleCoat=turtleCoat):
69
def drawMain(dc_local, w, turtleCoat=turtleCoat):
70
t = turtleCoat(dc_local)
71
# chaos1 (Ctrl-1) takes 2.2 seconds versus 7.14 seconds
72
# for this chaos1 done as an exec
73
t.setBackColor('black')
76
a = 73, b = 2.6, c = 25
77
a = -200, b = .1, c = -80
78
a = .4, b = 1, c = 0 (try a scale of 100 or 200 on this one)
79
a = -3.14, b = .3, c = .3
82
# beware of doing many iterations on a slow machine
83
# 500,000 iterations takes approximately 15 seconds on my AMD 1.2GHz Windows 2000 box
84
# with a GeForce 2 video card, so an older machine is likely to take much longer
85
# for reference, a similar Java applet I wrote takes under 5 seconds to do 500,000
86
# iterations on the same machine using the Sun JDK 1.3 and approximately 1 second
87
# using the IBM JDK 1.3
88
# if the self.dc.DrawLine ... is used above instead of the plot, drawing takes
89
# under 11 seconds and if no drawing is done, just the loop and calculations
90
# it takes 3 seconds to do 500,000 iterations
91
# some of the difference is likely Python compared to Java
92
# but some of it is how screen i/o is done, which is generally the slowest
93
# part of any drawing program, especially without any optimizations to the
94
# underlying APIs and drivers like DirectX
96
# The important point is that it is fast enough.
98
# bump this up to 500000 or more iterations to get interesting
99
# i set it at 100000 in case someone tried it on a slow machine
100
t.hopalong(30.0, 1.0, 0.9, 100000, 300.0, 300.0, 10.0)
101
#t.hopalong(73.0, 2.6, 25, 500000, 300.0, 300.0, 5.0)