Skip to content
Tcl Source Code Artifact [811fc71d29]
Login

Artifact [811fc71d29]

Artifact 811fc71d29a162bb6bcf2c1d0d4d2bbcbeb62eec78702a5d0197116e7e365f50:


TIP 759 / TIP 672 examples

What Tcl looks like with $((...)) (TIP 672) / TIP 759

1. Cross product

# plain Tcl
proc crossPlain {U V} {
    lassign $U x y z
    lassign $V u v w
    list [expr {$y*$w - $v*$z}] [expr {$w*$x - $u*$z}] [expr {$x*$v - $u*$y}]
}

# TIP 672
proc cross672 {U V} {
    lassign $U x y z
    lassign $V u v w
    list $(($y*$w - $v*$z)) $(($w*$x - $u*$z)) $(($x*$v - $u*$y))
}

# TIP 759
proc cross759 {U V} {(
    lassign $U x y z 
    lassign $V u v w; # cmd in expr !
    $y*$w - $v*$z, $w*$x - $u*$z, $x*$v - $u*$y
)}

2. Rectangle from a centre, width and height (canvas coordinates)

# plain Tcl
list [expr {$Cx-$L/2.0}] [expr {$Cy-$H/2.0}] [expr {$Cx+$L/2.0}] [expr {$Cy+$H/2.0}]

# TIP 672
list $(($Cx-$L/2.0)) $(($Cy-$H/2.0)) $(($Cx+$L/2.0)) $(($Cy+$H/2.0))

# TIP 759
[( $Cx-$L/2.0, $Cy-$H/2.0, $Cx+$L/2.0, $Cy+$H/2.0)]

3. Scalar product (loop body)

# plain Tcl
proc dotPlain {U V} {
   set s 0
   foreach u $U v $V {
        set s [expr {$s + $u*$v}]
   }
   set s
}

# TIP 672
proc dot672 {U V} {
   set s 0
   foreach u $U v $V {
       set s $(($s + $u*$v))
   };
   set s
}

# TIP759
proc dot759 {U V} {(
   s = 0;
   foreach u $U v $V {(
       s = $s + $u*$v
    )};
    $s
)}

4. Tensor product

# plain Tcl
proc tensorPlain {U V} {lmap u $U {lmap v $V {expr {$u * $v}}}}

# TIP 672 (an lmap body is a script, so it needs a command around the value)
proc tensor672 {U V} {lmap u $U {lmap v $V {set _ $(($u * $v))}}}

# TIP 759 
proc tensor759 {U V} {lmap u $U {lmap v $V {($u * $v)}}}

5. Rotation matrix

# plain Tcl
proc rotPlain {deg} {
    set a [expr {$deg/180.0*acos(-1)}]
    list [expr {cos($a)}] [expr {sin($a)}] [expr {-sin($a)}] [expr {cos($a)}] 0 0
}

# TIP 672
proc rot672 {deg} {
    set a $(($deg/180.0*acos(-1)))
    list $((cos($a))) $((sin($a))) $((-sin($a))) $((cos($a))) 0 0
}

# TIP 759
proc rot759 {deg} {(
    a = $deg/180.0*acos(-1);
    c = cos($a), s = sin($a), -$s, $c, 0, 0
)}

6. Distance

# plain Tcl
proc distPlain {x0 y0 x1 y1} {
    expr {hypot($x1-$x0, $y1-$y0)}
}

# TIP 672 (a proc body ends with a command, so it needs `return`)
proc dist672 {x0 y0 x1 y1} {
    return $((hypot($x1-$x0, $y1-$y0)))
}

# TIP 759 (a proc arithmetic body return the last command)
proc dist759 {x0 y0 x1 y1} {(
    hypot($x1-$x0, $y1-$y0)
)}

7. Values inside strings, array indexes and conditions

# TIP 672
puts "area=$(($w*$h)) perimeter=$((2*($w+$h)))"    
# TIP 759
puts "area=[( $w*$h )] perimeter=[( 2*($w+$h) )]"
# TIP 672
puts "array: $a($((1+1)))"                            ;# array: two
# TIP 759
puts "array: $a([(1+1)])"                            ;# array: two
# TIP 672
if {$(($w % 2)) == 1} {puts "w is odd"}               ;# w is odd
# TIP 759 (uneccessary nested)
if {[($w % 2)] == 1} {puts "w is odd"}               ;# w is odd

8. Polynomial multiplication (inner loops, adapted from tcllib math)

# plain Tcl
proc polyPlain {c1 c2} {
    set out [lrepeat [expr {[llength $c1]+[llength $c2]-1}] 0]
    set i 0
    foreach a $c1 {
        set j 0
        foreach b $c2 {
            lset out [expr {$i+$j}] [expr {[lindex $out [expr {$i+$j}]] + $a*$b}]
            incr j
        }
        incr i
    }
    set out
}

# TIP 672
proc poly672 {c1 c2} {
    set out [lrepeat $(([llength $c1]+[llength $c2]-1)) 0]
    set i 0
    foreach a $c1 {
        set j 0
        foreach b $c2 {
            lset out $(($i+$j)) $(([lindex $out $(($i+$j))] + $a*$b))
            incr j
        }
        incr i
    }
    set out
}

# TIP 759 (using raw command in expr)
proc poly759 {c1 c2} {(
     out = lrepeat [( llength $c1 + llength $c2 - 1 )] 0;
     i = 0;
     foreach a $c1 {(
        j=0;
        foreach b $c2 {
            lset out [($i+$j)]  [( lindex $out [($i+$j)] + $a*$b )]
            incr j
        }
        incr i
    )}
    $out
)}

9. A longer function: distance from a point to the edges of a triangle

# Point in triangle

# Plain Tcl:
proc sgn {x1 y1 x2 y2 x3 y3} {
    expr {($x1-$x3)*($y2-$y3) - ($x2-$x3)*($y1-$y3)}
}

proc point_in_triangle {px py t1 t2 t3} {
    lassign $t1 ax ay
    lassign $t2 bx by
    lassign $t3 cx cy
    set d1 [sgn $px $py $ax $ay $bx $by]
    set d2 [sgn $px $py $bx $by $cx $cy]
    set d3 [sgn $px $py $cx $cy $ax $ay]
    expr {!((($d1 < 0) || ($d2 < 0) || ($d3 < 0)) && (($d1 > 0) || ($d2 > 0) || ($d3 > 0)))}
}

# TIP672 

# TIP759 (using command in expr)
proc sgn {x1 y1 x2 y2 x3 y3} {(
   ($x1-$x3)*($y2-$y3) - ($x2-$x3)*($y1-$y3)
)}

proc point_in_triangle {px py t1 t2 t3} {(
    lassign $t1 ax ay
    lassign $t2 bx by
    lassign $t3 cx cy;
    d1 = [sgn $px $py $ax $ay $bx $by];
    d2 = [sgn $px $py $bx $by $cx $cy];
    d3 = [sgn $px $py $cx $cy $ax $ay];
    !((($d1 < 0) || ($d2 < 0) || ($d3 < 0)) && (($d1 > 0) || ($d2 > 0) || ($d3 > 0)))
)}

# calculate_edge_distance
# Plain Tcl
proc calculate_edge_distance_Plain {px py t1 t2 t3} {
    set edges [list [list $t1 $t2] [list $t2 $t3] [list $t3 $t1]]
    set min_distance 999999999
    foreach edge $edges {
        lassign $edge p1 p2
        lassign $p1 x1 y1
        lassign $p2 x2 y2
        set A [expr {$y2-$y1}]
        set B [expr {$x1-$x2}]
        set C [expr {$x2*$y1 - $x1*$y2}]
        set distance [expr {abs($A*$px + $B*$py + $C) / hypot($A, $B)}]
        if {$distance < $min_distance} {set min_distance $distance}
    }
    expr {[point_in_triangle $px $py $t1 $t2 $t3] ? -$min_distance : $min_distance}
}

# TIP 672
proc calculate_edge_distance_672 {px py t1 t2 t3} {
    set edges [list [list $t1 $t2] [list $t2 $t3] [list $t3 $t1]]
    set min_distance 999999999
    foreach edge $edges {
        lassign $edge p1 p2
        lassign $p1 x1 y1
        lassign $p2 x2 y2
        set A $(($y2-$y1))
        set B $(($x1-$x2))
        set C $(($x2*$y1 - $x1*$y2))
        set distance $((abs($A*$px + $B*$py + $C) / hypot($A, $B)))
        if {$distance < $min_distance} {set min_distance $distance}
    }
    return $(([point_in_triangle $px $py $t1 $t2 $t3] ? -$min_distance : $min_distance))
}

TIP 759 (with command in expr). The example is not up to date.

proc calculate_edge_distance_Arith {px py t1 t2 t3} {(
        edges = (($t1,$t2),($t2,$t3),($t3,$t1));
        min_distance = 999999999;
        foreach edge $edges {(
            lassign $edge p1 p2
            lassign $p1 x1 y1
            lassign $p2 x2 y2;
            A = $y2-$y1;
            B = $x1-$x2;
            C = $x2*$y1 - $x1*$y2;
            distance = abs($A * $px + $B * $py + $C) / hypot($A, $B);
            if {$distance < $min_distance} {min_distance = $distance}
         )};
       [point_in_triangle $px $py $t1 $t2 $t3] ? -$min_distance : $min_distance
)}

Note : The example in the wiki is not up to date.

The measurement of calculate_edge_distance_Arith must corrected, since TIP 759 implemented "Cmd in expr"

string length [string map {{ } {}}\
     [info body calculate_edge_distance_Arith]; # 340, not 404