/tags/rel-1-3-15/SWIG/Examples/GIFPlot/Tcl/mandel/mandel.tcl
TCL | 171 lines | 131 code | 33 blank | 7 comment | 17 complexity | f24db0e378b64ae79824a43ddbcd4567 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1catch { load ./gifplot.so }
2catch { load ./gifplot.dll } ; # Windows
3source display.tcl
4set tcl_precision 17
5set f [FrameBuffer -args 400 400]
6set cmap [ColorMap -args cmap]
7set p2 [Plot2D -args $f -3 -2 1 2]
8
9set xmin -3
10set xmax 1
11set ymin -2.0
12set ymax 2.0
13set tolerance 240
14set filename mandel.gif
15
16# Make a plot from the above parms
17
18proc make_plot {} {
19 global p2 cmap tolerance
20 global xmin ymin xmax ymax filename
21 $p2 setrange $xmin $ymin $xmax $ymax
22 $p2 start
23 . config -cursor watch
24 update
25 mandel $p2 $tolerance
26 . config -cursor arrow
27 [$p2 cget -frame] writeGIF $cmap $filename
28 display_image $filename $p2 set_zoom
29}
30
31
32# Take some screen coordinates and set global min and max values
33
34proc set_zoom {p2 mxmin mymin mxmax mymax x1 y1 x2 y2} {
35 global xmin ymin xmax ymax
36
37 set frame [$p2 cget -frame]
38 set width [$frame cget -width]
39 set height [$frame cget -height]
40
41 if {$x1 < 0} {set x1 0}
42 if {$x1 > ($width)} {set x1 $width}
43 if {$x2 < 0} {set x2 0}
44 if {$x2 > ($width)} {set x2 $width}
45 if {$x1 < $x2} {set ixmin $x1; set ixmax $x2} {set ixmin $x2; set ixmax $x1}
46
47 if {$y1 < 0} {set y1 0}
48 if {$y1 > ($height)} {set y1 $height}
49 if {$y2 < 0} {set y2 0}
50 if {$y2 > ($height)} {set y2 $height}
51 if {$y1 < $y2} {set iymin $y1; set iymax $y2} {set iymin $y2; set iymax $y1}
52
53 # Now determine new min and max values based on screen location
54
55 set xmin [expr {$mxmin + ($mxmax-$mxmin)*($ixmin)/($width)}]
56 set xmax [expr {$mxmin + ($mxmax-$mxmin)*($ixmax)/($width)}]
57 set ymin [expr {$mymin + ($mymax-$mymin)*(($height)-($iymax))/($height)}]
58 set ymax [expr {$mymin + ($mymax-$mymin)*(($height)-($iymin))/($height)}]
59
60 catch {make_plot}
61}
62
63# Box drag constrained to a square
64proc BoxDrag { w x y} {
65 global box
66 catch {$w delete $box(last)}
67 set x1 [lrange $box(anchor) 0 0]
68 set y1 [lrange $box(anchor) 1 1]
69 set dx [expr {$x - $x1}]
70 set dy [expr {$y - $y1}]
71 if {abs($dy) > abs($dx)} {set dx $dy}
72 set newx [expr {$x1 + $dx}]
73 set newy [expr {$y1 + $dx}]
74 set box(last) [eval {$w create rect} $box(anchor) {$newx $newy -tag box -outline white}]
75}
76
77
78proc BoxFinish {w x y p2 mxmin mymin mxmax mymax func } {
79 global box
80 set start $box(anchor)
81 set x1 [lrange $box(anchor) 0 0]
82 set y1 [lrange $box(anchor) 1 1]
83 set dx [expr {$x - $x1}]
84 set dy [expr {$y - $y1}]
85 if {($dx == 0) || ($dy == 0)} {
86 catch {$w delete $box(last)}
87 return
88 }
89 if {abs($dy) > abs($dx)} {set dx $dy}
90 set newx [expr {$x1 + $dx}]
91 set newy [expr {$y1 + $dx}]
92 $w config -cursor watch
93 update
94# Call the handler function
95 $func $p2 $mxmin $mymin $mxmax $mymax $x1 $y1 $newx $newy
96 catch {$w delete $box(last)}
97 $w config -cursor arrow
98}
99
100
101# Create a few frames
102
103wm title . Mandelbrot
104frame .title -relief groove -borderwidth 1
105label .title.name -text "Mandelbrot Set"
106button .title.quit -text "Quit" -command "exit"
107button .title.about -text "About" -command "about"
108pack .title.name -side left
109pack .title.quit .title.about -side right
110
111frame .func -relief groove -borderwidth 1
112
113frame .func.xrange
114label .func.xrange.xrlabel -text "X range" -width 12
115entry .func.xrange.xmin -textvar xmin -width 8
116label .func.xrange.xtolabel -text "to"
117entry .func.xrange.xmax -textvar xmax -width 8
118pack .func.xrange.xrlabel .func.xrange.xmin .func.xrange.xtolabel .func.xrange.xmax -side left
119
120frame .func.yrange
121label .func.yrange.yrlabel -text "Y range" -width 12
122entry .func.yrange.ymin -textvar ymin -width 8
123label .func.yrange.ytolabel -text "to"
124entry .func.yrange.ymax -textvar ymax -width 8
125pack .func.yrange.yrlabel .func.yrange.ymin .func.yrange.ytolabel .func.yrange.ymax -side left
126
127frame .func.npoints
128label .func.npoints.label -text "Tolerance " -width 12
129entry .func.npoints.npoints -textvar tolerance -width 8
130scale .func.npoints.scale -from 0 -to 2500 -variable tolerance -orient horizontal -showvalue false \
131 -sliderlength 13 -bigincrement 10 -resolution 10
132pack .func.npoints.label .func.npoints.npoints .func.npoints.scale -side left
133
134pack .func.xrange .func.yrange .func.npoints -side top -fill x
135
136# Filename dialog
137
138frame .save -relief groove -borderwidth 1
139
140frame .save.file
141label .save.file.label -text "Save as" -width 12
142entry .save.file.filename -textvar filename -width 20
143pack .save.file.label .save.file.filename -side left
144pack .save.file -side left -fill x
145button .save.go -text "Plot" -command "make_plot"
146pack .save.go -side right
147
148bind .save.file.filename <Return> {make_plot}
149
150pack .title .func .save -side top -fill both
151
152proc about { } {
153 toplevel .about -width 350
154
155 message .about.m -text "\
156Mandelbrot Set\n\n\
157Copyright (c) 1997\n\
158Dave Beazley\n\
159University of Utah\n\n\
160Creates a plot of the Mandelbrot set. Any displayed image can be zoomed by clicking and \
161dragging. Although the main calculation is written in C, it may take awhile for each \
162image to be calculated (be patient). Image quality can be improved at the expense of speed \
163by increasing the tolerance value.\n"
164
165button .about.okay -text "Ok" -command {destroy .about}
166
167pack .about.m .about.okay -side top
168focus .about.okay
169}
170
171make_plot