File: plot2d.tcl

package info (click to toggle)
swig 1.1p5-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,448 kB
  • ctags: 5,025
  • sloc: cpp: 21,599; ansic: 13,333; yacc: 3,297; python: 2,794; makefile: 2,197; perl: 1,984; tcl: 1,583; sh: 736; lisp: 201; objc: 143
file content (116 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Some simple Tcl functions for making 2D plots 

proc plotfunc {p2 func npoints color symbol} {

    # Substitute the variable 'x' in func to be a Tcl variable
    regsub -all x $func \$x myfunc
    puts $myfunc
    set xmin [$p2 cget -xmin]
    set xmax [$p2 cget -xmax]
    set dx [expr {1.0*($xmax-$xmin)/$npoints}]
    set x $xmin
    puts $dx
    set y [eval "expr {$myfunc}"]
    set lastx $x
    set lasty $y
    if {$symbol != "NULL"} {
	$p2 drawpixmap $symbol $x $y $color 0
    }
    for {set i 0} { $i < $npoints } {incr i 1} {
        set x [expr {$x + $dx}]
	set y [eval "expr {$myfunc}"]
	$p2 line $lastx $lasty $x $y $color
	if {$symbol != "NULL"} {
	    $p2 drawpixmap $symbol $x $y $color 0
	}
	set lastx $x
	set lasty $y
    }
}

proc newplot {p2 xtick ytick title xaxis_label yaxis_label} {
    set frame [$p2 cget -frame]
    $frame clear 0
    set width [$frame cget -width]
    set height [$frame cget -height]
    
    $frame noclip

    # Draw a bounding box around the image
    
    $frame box 50 50 [expr {$width-15}] [expr {$height-15}] 1

    # Draw graph title

    set l [string length $title]
    set sx [expr {(($width-50)/2) - 4*$l + 45}]
    $frame drawstring $sx [expr {$height-12}] 1 0 $title 1
    
    # Draw axis labels

    set l [string length $xaxis_label]
    set sx [expr {(($width-50)/2) - 4*$l + 45}]
    $frame drawstring $sx 38 1 0 $xaxis_label 1

    set l [string length $yaxis_label]
    set sy [expr {(($height-50)/2) - 4*$l + 45}]
    $frame drawstring 47 $sy 1 0 $yaxis_label 2

    # Draw plotting range
    
    set xmin [$p2 cget -xmin]
    set xmax [$p2 cget -xmax]
    
    $frame drawstring 50 38 1 0 $xmin 1
    set l [string length $xmax]
    set sx [expr {($width-15)-8*$l}]
    $frame drawstring $sx 38 1 0 $xmax 1

    set ymin [$p2 cget -ymin]
    set ymax [$p2 cget -ymax]
    $frame drawstring 47 50 1 0 $ymin 2
    set l [string length $ymax]
    set sy [expr {($height-15)-8*$l}]
    $frame drawstring 47 $sy 1 0 $ymax 2
    
    # Set clipping region for 2D plot

    $p2 setview 51 51 [expr {$width-15}] [expr {$height-15}]

    # Draw the axis
    $p2 xaxis 0 0 $xtick 3 1
    $p2 yaxis 0 0 $ytick 3 1
    $p2 start
}

# Take some screen coordinates and set global min and max values 

proc set_zoom {p2 mxmin mymin mxmax mymax x1 y1 x2 y2} {
    global xmin ymin xmax ymax

    set frame [$p2 cget -frame]
    set width [$frame cget -width]
    set height [$frame cget -height]

    if {$x1 < 50} {set x1 50}
    if {$x1 > ($width-15)} {set x1 [expr {$width-15}]}
    if {$x2 < 50} {set x2 50}
    if {$x2 > ($width-15)} {set x2 [expr {$width-15}]}
    if {$x1 < $x2} {set ixmin $x1; set ixmax $x2} {set ixmin $x2; set ixmax $x1}

    if {$y1 < 15} {set y1 15}
    if {$y1 > ($height-50)} {set y1 [expr {$height-50}]}
    if {$y2 < 15} {set y2 15}
    if {$y2 > ($height-50)} {set y2 [expr {$height-50}]}
    if {$y1 < $y2} {set iymin $y1; set iymax $y2} {set iymin $y2; set iymax $y1}

    # Now determine new min and max values based on screen location

    set xmin [expr {$mxmin + ($mxmax-$mxmin)*($ixmin-50)/($width-65)}]
    set xmax [expr {$mxmin + ($mxmax-$mxmin)*($ixmax-50)/($width-65)}]
    set ymin [expr {$mymin + ($mymax-$mymin)*(($height-65)-($iymax-15))/($height-65)}]
    set ymax [expr {$mymin + ($mymax-$mymin)*(($height-65)-($iymin-15))/($height-65)}]

    catch {make_plot}
}