Version 0 of spiral

Updated 2004-07-05 07:29:28 by suchenwi

if 0 {Richard Suchenwirth 2004-07-04 - Another sunny Sunday, another balcony fun project. What have I never coded in Tcl before? Umm ... A spiral on a canvas? And have it rotate for yet another animation effect? Here's what I came up with (quite a CPU burner on a 200 MHz iPaq):

http://mini.net/files/spiral.jpg

A spiral is a long line, consisting of many points. The center (for simplicity at 0 0.. move the thing if you want it elsewhere) is not part of the line, but the first point is closest to it. Every other point can be computed from its predecessor, by converting it to polar coordinates, adding a little bit to both radius and angle, and reconverting to Cartesian (canvas) coordinates. Simple enough.. after some less simple experiments.. In a tidy fashion, let's start with a main routine: }

 proc main {} {
    set ::tcl_precision 17
    pack [canvas .c]
    .c config -scrollregion {-100 -100 100 100}
    set s [.c create line [spiral 1 0] -width 3]
    every 40 [list rotate .c $s 0 0 .1]
 }

if 0 {The spiral routine produces an x y x y.. list of coordinates, that can be specified for a canvas line:}

 proc spiral {x y {max 500}} {
    set res [list $x $y]
    while {[llength $res]<=$max} {
       set r [expr {hypot($x,$y)+0.2}]
       set a [expr {atan2($y,$x)+0.2}]
       set x [expr {$r*cos($a)}]
       set y [expr {$r*sin($a)}]
       lappend res $x $y
    }
    set res
 }

if 0 {The generic rotation takes a canvas pathname, the ID of one canvas object, coordinates of rotation center, and finally the radians to rotate the thing:}

 proc rotate {w id x0 y0 da} {
    set coords {}
    foreach {x y} [$w coords $id] {
        set r [expr {hypot($y-$y0,$x-$x0)}]
        set a [expr {atan2($y-$y0,$x-$x0)+$da}]
       set x [expr {$x0+$r*cos($a)}]
       set y [expr {$y0+$r*sin($a)}]
       lappend coords $x $y
    }
    $w coords $id $coords
 }

# This repeating timer comes handy every now and then:

 proc every {ms body} {
    eval $body; after $ms [info level 0]
 }

# Ready.. Set.. Go!

 main

Arts and crafts of Tcl-Tk programming