/usr/share/tcltk/tklib0.6/plotchart/plotanim.tcl is in tklib 0.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | package require Tk 8.5
package require Plotchart
package provide plotanim 0.2
#
# This package is based on Arjen Markus' wiki page
# http://wiki.tcl.tk/21580 (Creating Mathematical Animations)
# and integrated into Plotchart by Torsten Berg
#
#
# the following objects are supported:
# point = an invisible dot
# dot = a dot
# circle = a cirle with center point and radius
# line = a line with starting and ending point
# rectangle = a rectangle with lower left and upper right corner
#
namespace eval ::Plotchart::anim {
#
# this array holds all information on the objects
# on the canvas created with this package:
variable item
#
# w = path to canvas
#
# item(w,type,<name>) = canvas item type of the item named <name>
# item($w,id,<name>) = the canvas item id of the item named <name>
# item($w,data,<name>) = configuration data of the canvas item named <name>
# item($w,track,<point>) = coordinates and configuration of a line tracking
# the movement of point <point>
#
# this array holds one element for each canvas,
# and is used to stop and restart animations (via the 'pause' command):
variable delayOver
#
# register the anim command for the plotchart package:
# variable ::Plotchart::config
# variable ::Plotchart::methodProc
# foreach type $config(charttypes) {
# set methodProc($type,anim) ::Plotchart::anim::animHandler
# }
}
# pause --
# Pause the program for a given amount of time
#
# Arguments:
# w path to the canvas
# delay Delay in ms
#
# Result:
# None
#
# Side effects:
# Program pauses, but any GUI is still active, screen updates occur
#
proc ::Plotchart::anim::pause {w delay} {
variable delayOver
after $delay [list set ::Plotchart::anim::delayOver($w) 1]
vwait ::Plotchart::anim::delayOver($w)
}
# create --
# Create an item on the canvas (type is fixed)
#
# Arguments:
# w path to the canvas
# name Name of the item
# itemdata Data for the item
#
# Result:
# None
#
# Side effects:
# An item with the given name now exists and is possibly visible
#
proc ::Plotchart::anim::create {w name itemdata} {
variable item
set item($w,type,$name) [lindex $itemdata 0]
switch -- $item($w,type,$name) {
"point" { # Nothing to do }
"dot" { set item($w,id,$name) [$w create oval 0 0 0 0] }
"circle" { set item($w,id,$name) [$w create oval 0 0 0 0] }
"line" { set item($w,id,$name) [$w create line 0 0 0 0] }
"rectangle" {set item($w,id,$name) [$w create rectangle 0 0 0 0]}
}
if { $item($w,type,$name) != "point" } {
$w itemconfig $item($w,id,$name) {*}[lindex $itemdata end]
}
#$w itemconfigure -tags $name
::Plotchart::anim::update $w $name {*}$itemdata
}
# update --
# Update the properties of an item
#
# Arguments:
# w path to the canvas
# name Name of the item
# args Data for the item
#
# Result:
# None
#
# Side effects:
# The item has updated properties
#
proc ::Plotchart::anim::update {w name args} {
variable item
set type $item($w,type,$name)
if { $type != [lindex $args 0] } {
return -code error "Item $name and given data are incompatible!"
}
set item($w,data,$name) [lrange $args 1 end]
UpdateCoords $w $name [lrange $item($w,data,$name) 0 end-1]
}
# UpdateCoords --
# Update the canvas coordinates of an item
#
# Arguments:
# w path to the canvas
# name Name of the item
# coords World coordinates for the item
# base the base of the given coords,
# can be either 'pixel' or 'coord' (the default)
#
# Result:
# None
#
# Side effects:
# The item is updated on the screen
#
proc ::Plotchart::anim::UpdateCoords {w name coords {base coord}} {
variable item
variable ::Plotchart::scaling
switch -- $item($w,type,$name) {
"point" {
# Not visible
}
"dot" {
if {$base eq "coord"} {
lassign $coords x y
lassign [::Plotchart::coordsToPixel $w $x $y] px py
} else {
lassign $coords px py
}
set xd1 [expr {$px - 3}]
set yd1 [expr {$py - 3}]
set xd2 [expr {$px + 3}]
set yd2 [expr {$py + 3}]
$w coords $item($w,id,$name) $xd1 $yd1 $xd2 $yd2
}
"circle" {
set r ""
if {$base eq "coord"} {
lassign $coords x y r
lassign [::Plotchart::coordsToPixel $w $x $y] px py
} else {
lassign $coords px py
}
if { $r == "" } {set r [lindex $item($w,data,$name) 2]}
set rad [expr {$scaling($w,xfactor)*$r}]
set xd1 [expr {$px - $rad}]
set yd1 [expr {$py - $rad}]
set xd2 [expr {$px + $rad}]
set yd2 [expr {$py + $rad}]
$w coords $item($w,id,$name) $xd1 $yd1 $xd2 $yd2
}
"line" - "rectangle" {
if {$base eq "coord"} {
lassign $coords x1 y1 x2 y2
lassign [::Plotchart::coordsToPixel $w $x1 $y1] px1 py1
lassign [::Plotchart::coordsToPixel $w $x2 $y2] px2 py2
} else {
lassign $coords px1 py1 px2 py2
}
$w coords $item($w,id,$name) $px1 $py1 $px2 $py2
}
}
}
# point, dot, circle, line, rectangle --
# Prepare the item data for an (invisible) point or some other canvas item
#
# Arguments:
# args Such things as x-coordinate/y-coordinate but also -fill colour
#
# Result:
# Item data for [create] or [update]
#
# Note:
# The extra arguments are only used in the [create] procedure
#
# point, dot:
# The first argment can be the name of a point or a pair of
# coordinates. The rest is used as attributes
#
# circle:
# The first argment can be the name of a point or a pair of
# coordinates. The second must be the radius, the rest is used as
# attributes
#
# line:
# The first and second argments can be the name of a point or a pair
# of coordinates. The rest is used as attributes.
#
# rectanlge:
# the first and second argments can be the name of a point or a pair
# of coordinates. The rest is used as attributes.
#
proc ::Plotchart::anim::point {w first args} {
variable item
if { [llength $first] == 2 } {
return [concat point $first [list $args]]
} else {
return [concat point [lrange $item($w,data,$first) 0 1] [list $args]]
}
}
proc ::Plotchart::anim::circle {w first rad args} {
variable item
if { [llength $first] == 2 } {
return [concat circle $first $rad [list $args]]
} else {
return [concat circle [lrange $item($w,data,$first) 0 1] $rad [list $args]]
}
}
proc ::Plotchart::anim::dot {w first args} {
variable item
if { [llength $first] == 2 } {
return [concat dot $first [list $args]]
} else {
return [concat dot [lrange $item($w,data,$first) 0 1] [list $args]]
}
}
proc ::Plotchart::anim::line {w first second args} {
variable item
if { [llength $first] == 1 } {
set first [lrange $item($w,data,$first) 0 1]
}
if { [llength $second] == 1 } {
set second [lrange $item($w,data,$second) 0 1]
}
return [concat line $first $second [list $args]]
}
proc ::Plotchart::anim::rectangle {w first second args} {
variable item
if {[llength $first] == 1} {
set first [lrange $item($w,data,$first) 0 1]
}
if {[llength $second] == 1} {
set first [lrange $item($w,data,$second) 0 1]
}
return [concat rectangle $first $second [list $args]]
}
# coords --
# Return the current coords of an item
#
# Arguments:
# w path to the canvas
# name name of the item
#
# Return:
# List of coordinates/radius in the same order a during creation
#
# Side effects:
# None
#
proc ::Plotchart::anim::coords {w name} {
variable item
switch -- $item($w,type,$name) {
"point" - "dot" - "circle" {set last 1}
"line" - "rectangle" {set last 3}
}
return [lrange $item($w,data,$name) 0 $last]
}
# add --
# Translate a point over a given vector and return the coordinates
#
# Arguments:
# w path to the canvas
# point Point (name or coordinate pair)
# vector Vector (name or coordinate pair)
#
# Result:
# New coordinate pair
#
proc ::Plotchart::anim::add {w point vector} {
variable item
if { [llength $point] == 1 } {
set point [lrange $item($w,data,$point) 0 1]
}
if { [llength $vector] == 1 } {
set vector [lrange $item($w,data,$vector) 0 1]
}
foreach {xb yb} $point {xe ye} $vector {
set xn [expr {$xb+$xe}]
set yn [expr {$yb+$ye}]
break
}
return [list $xn $yn]
}
# track --
# Track a point
#
# Arguments:
# w path to the canvas
# cmd Command in question (start, next or stop)
# point Name (!) of the point to track
# args Extra attributes (for colour and such)
#
# Result:
# None
#
# Side effect:
# A line connecting the dots is drawn
#
proc ::Plotchart::anim::track {w cmd point args} {
variable item
set xp [lindex $item($w,data,$point) 0]
set yp [lindex $item($w,data,$point) 1]
lassign [::Plotchart::coordsToPixel $w $xp $yp] xp yp
switch -- $cmd {
"start" {
set item($w,track,$point) [$w create line $xp $yp $xp $yp {*}$args]
}
"next" {
set coords [$w coords $item($w,track,$point)]
lappend coords $xp $yp
$w coords $item($w,track,$point) $coords
}
"stop" {
unset item($w,track,$point)
}
}
}
# rotate --
# Rotate one or more item over a given angle and update the coordinates
#
# Arguments:
# w path to canvas
# items List of items
# centre Centre of rotation (name or coordinate pair)
# angle Angle (in radians)
#
# Result:
# New coordinate pair
#
proc ::Plotchart::anim::rotate {w items centre angle} {
variable item
if { [llength $centre] == 1 } {set centre [lrange $item($w,data,$centre) 0 1]}
lassign $centre xr yr
set cosa [expr {cos($angle)}]
set sina [expr {sin($angle)}]
foreach name $items {
switch -- $item($w,type,$name) {
"point" - "dot" - "circle" {set last 1}
"line" - "rectangle" {set last 3}
}
set coords [lrange $item($w,data,$name) 0 $last]
set newcoords {}
foreach {xc yc} $coords {
set xn [expr {$xr + $cosa * ($xc-$xr) - $sina*($yc-$yr)}]
set yn [expr {$yr + $sina * ($xc-$xr) + $cosa*($yc-$yr)}]
lappend newcoords $xn $yn
}
set item($w,data,$name) [lreplace $item($w,data,$name) 0 $last {*}$newcoords]
UpdateCoords $w $name $newcoords
}
}
# translate --
# Translate one or more item over a given vector and update the coordinates
#
# Arguments:
# w path to canvas
# items List of items
# vector Vector (name or coordinate pair)
#
# Result:
# New coordinate pair
#
proc ::Plotchart::anim::translate {w items vector} {
variable item
if { [llength $vector] == 1 } {
set vector [lrange $item($w,data,$vector) 0 1]
}
lassign $vector xv yv
foreach name $items {
switch -- $item($w,type,$name) {
"point" - "dot" - "circle" {set last 1}
"line" - "rectangle" {set last 3}
}
set coords [lrange $item($w,data,$name) 0 $last]
set newcoords {}
foreach {xc yc} $coords {
set xn [expr {$xc + $xv}]
set yn [expr {$yc + $yv}]
lappend newcoords $xn $yn
}
set item($w,data,$name) [lreplace $item($w,data,$name) 0 $last {*}$newcoords]
UpdateCoords $w $name $newcoords
}
}
# morph --
# change the look of one or more items
#
# Arguments:
# w path to canvas
# items List of items
# shift a list of x y pairs giving the relative shift of the item coordinates
# args additional options:
# -scaling pixel|coord whether the shift is given in pixel space or coordinate space
#
# Result:
# New coordinate pair(s)
#
proc ::Plotchart::anim::morph {w items shift args} {
variable item
variable ::Plotchart::scaling
array set options {-scaling coord}
array set options $args
foreach name $items {
# calculate the shift in the coordinates
# depending on the base scale:
if {$options(-scaling) eq "pixel"} {
set dshift [list]
foreach {x y} $shift {
set dx [expr {$scaling($w,xfactor)/double($x)}]
set dy [expr {$scaling($w,yfactor)/double($y)}]
lappend dshift $dx $dy
}
set shift $dshift
} elseif {$options(-scaling) eq "coord"} {
# nothing needed here ...
} else {
return -code error "unknown -scaling: $options(-scaling)"
}
# how many coords to read:
switch -- $item($w,type,$name) {
"point" - "dot" - "circle" {set last 1}
"line" - "rectangle" {set last 3}
}
set coords [lrange $item($w,data,$name) 0 $last]
set newcoords {}
switch $item($w,type,$name) {
dot - point {
return -code error "a $item($w,type,$name) cannot be morphed"
}
circle {
lassign $coords x y
lassign $shift dx dy
set x [expr {$x + $dx}]
set y [expr {$y + $dy}]
set newcoords $x $y
}
line - rectangle {
lassign $coords x0 y0 x1 y1
lassign $shift dx0 dy0 dx1 dy1
set x0 [expr {$x0 + $dx0}]
set x1 [expr {$x1 + $dx1}]
set y0 [expr {$y0 + $dy0}]
set y1 [expr {$y1 + $dy1}]
set newcoords [list $x0 $y0 $x1 $y1]
}
}
set item($w,data,$name) [lreplace $item($w,data,$name) 0 $last {*}$newcoords]
UpdateCoords $w $name $newcoords $options(-scaling)
}
}
# id --
# Return the canvas id of an item in order to manipulate it with ordinary canvas commands
#
# Arguments:
# w path to the canvas widget
# name Name of the item
#
# Result:
# The canvas id of the item
#
proc ::Plotchart::anim::id {w name} {
variable item
return $item($w,id,$name)
}
# animate --
# Animate some canvas items following a user-defined specification
#
# Arguments:
# w path to the canvas
# args Arguments defining the animation
#
# -duration the duration of the complete animation in ms
# -body a script defining what to animate and how to do so
#
# The body consists of normal Tcl/Plotchart/Plotchart::anim commands
# plus some commands only available within this procedure -body. These can be
#
# morphto item first second
# item name of the item to be morphed
# first, second coordinates/radius, or a point name, to morph to
# (in same order as during item creation)
#
# translateto item first, second
# item name of the item to be translated
# first, second coordinates/radius, or a point name, to translate to
# (in same order as during item creation)
#
# Results:
# None
#
# Side effects:
# some canvas items are animated
#
proc ::Plotchart::anim::animate {w args} {
variable item
array set options {-body {} -duration 1000 -pause 20}
array set options $args
set stepCount [expr {$options(-duration)/$options(-pause)}]
# translate points into coordinates
# and reorganize body:
set body $options(-body)
set newBody [list]
while {[llength $body]>0} {
set cmd [lindex $body 0]
switch $cmd {
morphto {
set body [lassign $body cmd name first second]
set shiftList [list]
switch $item($w,type,$name) {
circle {}
line - rectangle {
if {[llength $first] == 2} {set newCoords $first} else {set newCoords [coords $first]}
if {[llength $second] == 2} {lappend newCoords {*}$second} else {lappend newCoords {*}[coords $second]}
foreach start [lrange $item($w,data,$name) 0 3] end $newCoords {
lappend shiftList [expr {double($end-$start)/$stepCount}]
}
}
default {return -code error "no such item type: $item($w,type,$name)"}
}
lappend newBody [list morph $w $name $shiftList -scaling coord]
}
default {
#while [info complete ]
set body [lrange $body 1 end]
}
}
}
# puts "newBody=$newBody ($stepCount steps)"
# do the animation:
for {set step 1} {$step <= $stepCount} {incr step} {
foreach element $newBody {
{*}$element
}
pause $w $options(-pause)
}
}
|