Posted to tcl by emiliano at Tue Nov 04 14:16:09 GMT 2008view raw

  1. package require Tk 8.5
  2. namespace path tcl::mathop
  3.  
  4. pack [canvas .c -bg white]
  5. .c create line {
  6. 0 0
  7. 10 1
  8. 20 4
  9. 30 9
  10. 40 16
  11. 50 25
  12. 60 36
  13. 70 49
  14. 80 64
  15. 90 81
  16. 100 100
  17. 110 121
  18. 120 144
  19. 130 169
  20. 140 196
  21. } -tags line
  22. .c create oval {0 0 5 5} -fill red -tags circle
  23. .c create text 150 10 -text "Mouse: " -tags MC -anchor w
  24. .c create text 150 25 -text "Point: " -tags PC -anchor w
  25.  
  26. proc CenterCircle {x y} {
  27. .c coords circle [- $x 2.5] [- $y 2.5] [+ $x 2.5] [+ $y 2.5]
  28. }
  29.  
  30. proc FollowMouse {x y} {
  31. set idx [.c index line @$x,$y]
  32. set clist [.c coords line]
  33. lassign [lrange $clist $idx ${idx}+1] X Y
  34. if {$X eq ""} return
  35. CenterCircle $X $Y
  36. .c itemconfigure MC -text [format "Mouse: %.1f,%.1f" $x $y]
  37. .c itemconfigure PC -text [format "Point: %.1f,%.1f" $X $Y]
  38. }
  39.  
  40. bind .c <Motion> {FollowMouse %x %y}
  41.  
  42. # move the mouse cursor along the curve down and up again.
  43. # some horizontal ticks will appear.
  44. # for an example see http://img145.imageshack.us/my.php?image=outputuw1.png