~toolpart/+junk/pythoncard

« back to all changes in this revision

Viewing changes to samples/turtle/scripts/hopalong.txt

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2006-11-12 17:52:13 UTC
  • mfrom: (2.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061112175213-tv8bnl6rtpa2qw1o
Tags: 0.8.1-8.1
* Non-maintainer upload.
* Fix path to findfiles, codeEditor and resourceEditor:
   + patch from Ernest ter Kuile <ernestjw@xs4all.nl>. (Closes: #397018)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
"""
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
7
 
"""
8
 
 
9
 
from tWrapper import tWrapper
10
 
#import math    # must be built-in, this isn't needed for sin and cos ?!
11
 
 
12
 
# wrapper for any additional drawing routines
13
 
# that need to know about each other
14
 
class turtleCoat(tWrapper):
15
 
    def sign(self, n):
16
 
        if n < 0:
17
 
            return -1
18
 
        else:
19
 
            return 1
20
 
 
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):
23
 
        #xOffset = 300.0
24
 
        #yOffset = 300.0
25
 
        #scale = 10.0;
26
 
 
27
 
        x = 0.0
28
 
        y = 0.0
29
 
 
30
 
        # other interesting values for a, b, and c are
31
 
        """
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
36
 
        """
37
 
        #a = 30.0
38
 
        #b = 1.0
39
 
        #c = 0.9
40
 
        #iterations = 500000
41
 
 
42
 
        colors = ['red', 'orange', 'yellow', 'white', 'green', 'blue', 'purple']
43
 
        
44
 
        #print len(colors)
45
 
        colorIter = iterations / len(colors)
46
 
        #colorIter = iterations / 500
47
 
        k = 0
48
 
 
49
 
        for i in range(len(colors)):
50
 
        #for i in range(colorIter):
51
 
            print k, colors[i]
52
 
            #self.color(i, 0, 0)
53
 
            self.color(colors[i])
54
 
            for j in range(colorIter):
55
 
                k = k + 1
56
 
                temp = y - self.sign(x) * sqrt(abs(b * x - c))
57
 
                y = a - x
58
 
                x = temp
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)
65
 
        #print k, "DONE"
66
 
 
67
 
 
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')
74
 
    t.cls()
75
 
    """
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
80
 
    """
81
 
 
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
95
 
    #
96
 
    # The important point is that it is fast enough.
97
 
 
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)