#!/usr/local/wish canvas .c pack .c # I need two associative arrays here so that I can quickly # find the item from the inder or the index from the item # number. # # Note that nhigh needs to be incremented as more nodes are added, # and both the m and n arrays need to be maintained. set n(0) [.c create oval 10 10 20 20 -fill white -tag nodes] set m($n(0)) 0 set n(1) [.c create oval 30 30 40 40 -fill white -tag nodes] set m($n(1)) 1 set n(2) [.c create oval 60 60 70 70 -fill white -tag nodes] set m($n(2)) 2 set n(3) [.c create oval 70 0 80 10 -fill white -tag nodes] set m($n(3)) 3 set nhigh 3 for {set coords {};set i 0} {$i <= $nhigh} {incr i} { foreach {x1 y1 x2 y2} [.c coords $n($i)] { append coords "[expr $x1+(($x2-$x1)/2)] [expr $y1+(($y2-$y1)/2)] " } } set outline [eval ".c create polygon $coords -fill {} -outline black"] .c lower $outline .c bind nodes <2> {startdrag [.c find closest [.c canvasx %x] [.c canvasy %y]]} .c bind nodes {movedrag %x %y} proc startdrag {i} { global moveitem m n nhigh x y set moveitem $i } proc movedrag {cx cy} { global moveitem m n nhigh x y coords outline set cx [.c canvasx $cx] set cy [.c canvasy $cy] set beg [expr $m($moveitem)*2] set end [expr $m($moveitem)*2+1] set dx [expr $cx - [lindex $coords $beg]] set dy [expr $cy - [lindex $coords $end]] .c move $moveitem $dx $dy set coords [lreplace $coords $beg $end $cx $cy] eval ".c coords $outline $coords" }