Tk Source Code

Artifact [5527a03d]
Login

Artifact 5527a03d7776effa3e93881e47f7ea26eeb3c507:

Attachment "koch-reduced.tcl" to ticket [3017005f] added by sgeard 2010-06-16 16:23:27.
# Access Polish notation
namespace import tcl::mathfunc::*
namespace import tcl::mathop::*

# Load Tk if necessary
package require Tk
rename scale tk_scale

# Ensure the correct version of Itcl is loaded
if {$tcl_version eq "8.6"} {
    package require Itcl 4
} else {
    package require Itcl 3
}
namespace import itcl::*

# Load linearalgebra
package require math::linearalgebra
namespace import math::linearalgebra::*
rename ::math::linearalgebra::sub subtract
rename ::math::linearalgebra::scale multiply

# Statistics for random numbers
package require math::statistics
namespace import math::statistics::random*

class snowFlake {
    private variable m_colour
    private variable m_position
    private variable m_size
    private variable m_depth
    private variable m_points
    
    common root3 [sqrt 3.0e0]
    common rot_p60 [mkMatrix 2 2]
    common one_third [expr {1.0e0/3.0e0}]
    
    constructor {colour} {} {
        set m_colour $colour
        setrow rot_p60 0 [list   0.5 [/ -$root3 2]]
        setrow rot_p60 1 [list   [/  $root3 2] 0.5]
    }
    
    public method createAt {position size depth} {
        set m_position $position
        set m_size $size
        set m_depth $depth
        
        # Set the position
        set pos_v [mkVector 2]
        setcol pos_v 0 $position
        
        # Create the points at the corners of a standard equilateral triangle
        # of side length $size
        set p [mkVector 2]
        set q [mkVector 2]
        set r [mkVector 2]
        setcol p 0 [list [* -0.5 $size] [* [/ -0.5 $root3] $size]]
        setcol q 0 [list             0  [/ $size $root3]]
        setcol r 0 [list [*  0.5 $size] [* [/ -0.5 $root3] $size]]
        
        # Move the triangle to the requested position
        set new_boundary [list [add $p $pos_v] [add $q $pos_v] [add $r $pos_v]]
        
        # Subdivide each boundary segment the required number of times ($depth)
        for {set d 1} {$d <= $depth} {incr d} {
            set boundary $new_boundary
            unset -nocomplain new_boundary
            for {set i 0} {$i < [llength $boundary]} {incr i} {
                set j [expr {($i+1)%[llength $boundary]}]
                lassign [subDivide [lindex $boundary $i] [lindex $boundary $j]] p1 p2 p3
                lappend new_boundary [lindex $boundary $i] $p1 $p2 $p3
            }
        }
        set m_points $new_boundary
    }
    
    public method draw {can} {
        foreach v $m_points {
            lappend boundary {*}[getcol $v 0]
        }
        $can create polygon $boundary -fill $m_colour -tags $this
        
        ########################################################################
        ## BUG: have to select update according to platform
        if {$::tcl_platform(platform) eq {windows}} {
            update           
        } else {
            update idletasks
        }
        ########################################################################
    }
    
    private method subDivide {p0 p1} {
        # Each new side is 1/3 the distance p0->p1
        set v [mkVector 2]
        setcol v 0 [multiply $one_third [subtract $p1 $p0]]
        
        # First new point is p0 + v
        set np1 [mkVector 2]
        setcol np1 0 [add $p0 $v]
        
        # Second new point is np1 + Rotate60(v)
        set np2 [mkVector 2]
        setcol np2 0 [add $np1 [matmul $rot_p60 $v]]
        
        # Third new point is p1 -v
        set np3 [mkVector 2]
        setcol np3 0 [subtract $p1 $v]
        
        return [list $np1 $np2 $np3]
    }
}

# Create the canvas on which to draw
set can [canvas .c -width 400 -height 400 -bg grey90]
pack $can -fill both -expand 1

# Create snowflakes at random along the top of the canvas
set clist {red orange yellow green blue grey40 grey60 black cyan magenta}
set n_clist [llength $clist]
set size 20
set sdev 4
set N [expr {[$can cget -width]/(2*$size)}]
set x [random-uniform 0 [$can cget -width] $N]
set y [random-uniform 0 [$can cget -height] $N]
set z [random-normal $size $sdev $N]
for {set i 0} {$i < $N} {incr i} {
    set newsnow [snowFlake #auto [lindex $clist [expr {$i%$n_clist}]]]
    set p [list [lindex $x $i] [lindex $y $i]]
    set s [expr {max([lindex $z $i],10)}]; # Ensure size isn't too small
    if {$s < $size - $sdev} {
        set depth 4
    } elseif {$s < $size} {
        set depth 5
    } else {
        set depth 6
    }
    $newsnow createAt $p $s $depth
    $newsnow draw $can
}