Tk Source Code

Artifact [912201f0]
Login

Artifact 912201f00a79c8d62301980a0e767d9907d247319ced7297bb72da7afacbdf78:

Attachment "BetterDemoScript.tcl" to ticket [3406785f] added by fvogel 2017-09-09 13:48:36.
package require Tk
catch {console show}
catch {wm state . zoomed}

canvas .c -bg white -highlightthickness 0
grid .c -sticky news
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1

#Each "pixel" is zoomed by "zoom":
set zoom 50
for {set f 0} {$f < 2000} {incr f $zoom} {
    .c create line $f 0 $f 2000 -width 0 -fill grey
    .c create line 0 $f 2000 $f -width 0 -fill grey
}

set x1 $zoom
set x2 [expr {2*$zoom}]
set basey1 6.0
set basey2 6.0
for {set f 1} {$f < 20} {incr f} {
    # Original demo values:
    set realy1 [expr {$basey1 - 0.25*$f}]
    set realy2 [expr {$basey2}]

    # "Obviously correct" drawing:
    set cx1 [expr {$x1 + 7}]
    set cx2 [expr {$x2 - 7}]
    # It's assumed that y1 <= y2 here:
    if {($realy2 - $realy1) < 1} {
       # Here it gets complex, as a pixel MUST be drawn.
       # There's 1 or 2 "potential" pixels involved, check which ones:
       set corry1 [expr {int($realy1)}]
       set corry2 [expr {int($realy2)}]
       if {$corry1 == $corry2} {
          # We need pixel they are both in:
          incr corry2
       } else {
          # So, realy1 and realy2 "cover" two pixels. Get the one there the
          # covered "area" is bigger:
          set dy1 [expr {$corry2 - $realy1}]
          set dy2 [expr {$realy2 - $corry2}]
          if {$dy2 > $dy1} {
             incr corry1
             incr corry2
          }
       }
    } else {
       set corry1 [expr {round($realy1)}]
       set corry2 [expr {round($realy2)}]
    }
    set cy1 [expr {$corry1 * $zoom + 7}]
    set cy2 [expr {$corry2 * $zoom - 7}]
    set corr [.c create rectangle $cx1 $cy1 $cx2 $cy2 -fill blue -width 0]
    .c lower $corr

    # Imitate Tk drawing (incomplete):
    set y1 [expr {$realy1 * $zoom}]
    set y2 [expr {$realy2 * $zoom}]
    if {($y2-$y1) < $zoom} {
       set a [.c create rectangle $x1 $y2 $x2 [expr {$y2+$zoom}] -fill red -width 0]
    } else {
       set a [.c create rectangle $x1 [expr {round($y1/(1.0*$zoom))*$zoom}] \
                                  $x2 [expr {round($y2/(1.0*$zoom))*$zoom}] \
                                  -fill lightblue -width 0]
    }
    .c lower $a

    # Show the actual coordinates (zoomed):
    .c create rectangle [expr {$x1+2}] [expr {$y1+2}] \
                        [expr {$x2-2}] [expr {$y2-2}] -outline darkgreen
    incr x1 $zoom
    incr x2 $zoom
}