# response2.tcl, cr thu 08 feb 1999 by rha
# second version of response diagram, w/ 2 sliders and one button
# revised to run as Tclet, 23 apr 2000 by rha
#	# out all "puts" lines

###### inits ######

# init size of canvas
set vres 300
set hres 400

# set init size of visible state space
set xmin 0.0
set xmax 1.0

# set init size of visible control space
set cmin 0.0
set cmax 4.0

## temp
set param 2.5

###### conversion routines ########

# convert horiz index to param value
proc see { i } {
  set flarg [ expr double ($i) ]
  global cmin cmax hres
  set temp [ expr double ($flarg/$hres ) ]
  set cnow  [ expr double (($cmax - $cmin ) * $temp + $cmin)]
#  puts "cnow is $cnow"
  return $cnow
}

# convert vertical index to xvar value
proc eks { j } {
  set flarg [ expr double ($j) ]
  global xmin xmax vres
  set temp [ expr double ($flarg/$vres ) ]
  set xnow  [ expr double ($xmax - $xmin ) * $temp]
  return $xnow
} 

# convert xvar back to vertical index value
proc jay { x } {
  global xmin xmax vres
  set jnow [ expr  ($vres - int ($vres * ( $x - $xmin ) / ( $xmax - $xmin ))) ]
#  puts "j-of-x = $jnow "
  return $jnow
}
# jay 0.5 

# define the function
proc myfunc { xval } {
  global param
  return [ expr  $param * $xval - $param * $xval * $xval ]
}

##### Tk Widget Heaven #####

# frame two scales and a button to get them on top of canvas
frame .fi1 -relief groove -borderwidth 10 \
  -background RoyalBlue

# add a scale (slider) for the min param (r1) in response diagram
scale .fi1.c1 -from 0.0 -to 4.0 \
  -orient horizontal \
  -tickinterval 1.0 \
  -resolution 0.01 -digits 5 \
  -length 200 -label "C Min" \
  -command update_c1

# redraw  button in middle
button .fi1.b1 -text "Do It" \
  -command { doit }

# add a scale (slider) for the max param (r2) in response diagram
scale .fi1.c2 -from 0.0 -to 4.0 \
  -orient horizontal \
  -tickinterval 1.0 \
  -resolution 0.01 -digits 5 \
  -length 200 -label "C Max" \
  -command update_c2

pack .fi1.c1 .fi1.b1 .fi1.c2 -side left
pack .fi1

# second frame, only the canvas so far

frame .fi2 -relief groove -borderwidth 10 \
  -background RoyalBlue

# declare the canvas
canvas .fi2.can -width $hres -height $vres

#pack the canvas and its frame
pack .fi2.can
pack .fi2

# make a dot command
proc dot { x y } {
  # global vres
  .fi2.can create line $x $y $x [expr $y-2] -fill blue  -width 2
}

#######################

# procs for sliders and button
proc update_c1 {value} {
  global cmin
  set cmin $value
#  puts "C1: cmin param is now $cmin"
}

proc update_c2 {value} {
  global cmax
  set cmax $value
#  puts "C2: cmax param is now $cmax"
}

proc doit { } {
  clearcan
#  puts "done It"
  }

# clear the canvas
proc clearcan {} {
  global hres vres
  destroy .fi2.can
  canvas .fi2.can -width $hres -height $vres
  drawall
  #traj 100
  pack .fi2.can
  pack .fi2
}

# draw  attractor for fixed (global) param
proc traj { i } {
  set xinit 0.5
  set xnow $xinit
  set xnext $xnow
  for {set k 0 } { $k < 20 } { incr k } {
    set xnext [ myfunc $xnow ]
    set xnow $xnext
  }
  for {set k 0 } { $k < 50 } { incr k } {
    set xnext [ myfunc $xnow ]
    set j [ jay $xnext ]
    dot $i $j
    set xnow $xnext
  }
}

# advance control param, draw attractor
proc drawall { } {
  global hres param
  for { set i 0 } { $i < $hres } { incr i } {
    set localcnow [ see $i ]
    # global param
    set param $localcnow
    # puts "param is now $param"
    traj $i
  }
}
# first time doit
drawall
#clearcan

#end: response2.tcl
