Tcl proc to output a list or array -
i new tcl arrays. question follows.
i have rectangle box 2 rows r1 , r2. each of these rows has 8 different values. want return these 16 values (x , y coordinates) either in text file or list output proc. read earlier posts tcl proc cannot output array unless use dict. so, try draw picture u can understand question better.
r1 x1y1 x2y2 ... x8,y8 r2 x9,y9 ... x16, y16
expected output when run proc either on command prompt or in file dummy values example
$> (1,2) (2,3) (3,4) ....... (7,8) (9,10) (10,11) ......... (15,16)
so tried , getting results need. hardcoded 2 rows. want make able detect how many rows there , accordingly output number of rows.
proc getpointlist {rect_boundary rowoffset coloffset rowincr colincr } { set cordlist $rect_boundary set xl [lindex $cordlist 0] set yl [lindex $cordlist 1] set xh [lindex $cordlist 2] set yh [lindex $cordlist 3] set list "" ; {set y [expr {$yh - $coloffset}]} {$y >= [expr {$yl + $coloffset}]} { incr y $colincr } { {set x [expr {$xl + $rowoffset}]} {$x <= [expr {$xh - $rowoffset}]} { incr x $rowincr } { set list "$list $x $y" ; puts "value of x is: $x"; puts "\t value of y is: $y" ; } } return $list } set rect_boundary {10 15 100 40} # xl yl xh yh set rowoffset 5 set coloffset 5 set rowincr 10 set colincr 15
some logic need implement in code based on yh-yl , xh-xl calculate height , width of rectangle , accordingly output rows
command call proc
$> getpointlist $rect_boundary $rowoffset $coloffset $rowincr $colincr
just understanding there 8 x,y points inside rectangle on particular row. x offset first x point on row left or roght boundary, thereafter points separated increment value call rowincr. same holds true column.
expected output : above code hardcoded 2 rows. want increase , implement logic if rows , column variable.
$> r1: (15 40) (25 40) (35 40) (45 40) (55 40) (65 40) (75 40) (85 40) (95 40) r2: (15 15) (25 15) (35 15) (45 15) (55 15) (65 15) (75 15) (85 15) (95 15)
rectangle image better clarity thing wont let me update pictures
__________________________________________________________________________ (100,40) | |- 5 | | . . . . . . . . | | |- 15 | |-5-. . --10---. . . . . . | | | |_________________________________________________________________________| (10,15)
for jerry:
case1 rowincr 10 colincr 20 __________________________________________________________________________ (80,40) | |- 5 | | . . . . . . . . | | |- 20 | |-5-. . --10---. . . . . . | | | |_________________________________________________________________________| (10,10) case2 rowincr 20 colincr 35 _________________________________________________ (100,70) | |- 5 | | . . . . . | | |- 35 | |-5-. . --20---. . . | | | |-5 | |________________________________________________| (10,25)
and on ...
okay, think understand trying do, , think proc have worked number of rows after fixing:
set output [open "output.txt" w] proc getpointlist {rect_boundary rowoffset coloffset plist} { global output set cordlist $rect_boundary set xl [lindex $cordlist 0] set yl [lindex $cordlist 1] set xh [lindex $cordlist 2] set yh [lindex $cordlist 3] set xpoints [llength [lindex $plist 0]] set ypoints [llength $plist] set rowincr [expr {($xh-$xl-2*$rowoffset)/($xpoints-1)}] set colincr [expr {($yh-$yl-2*$coloffset)/($ypoints-1)}] set count 0 set list "" {set y [expr {$yh - $coloffset}]} {$y >= [expr {$yl + $coloffset}]} {incr y -$colincr} { {set x [expr {$xl + $rowoffset}]} {$x <= [expr {$xh - $rowoffset}]} {incr x $rowincr} { lappend list "($x,$y)" } incr count puts $output "r$count: [join $list " "]" set list "" } } set plist {{a b c d e} {a b c d e} {a b c d e} {a b c d e} {a b c d e}} set rect_boundary {0 0 100 100} set rowoffset 0 set coloffset 0 getpointlist $rect_boundary $rowoffset $coloffset $plist close $output
i changed colincr
put more rows.
in first loop, used incr y -$colincr
because decrement if start higher y
coordinate.
i changed output structure match 1 looking for. above snippet returns coordinates:
r1: (0,100) (25,100) (50,100) (75,100) (100,100) r2: (0,75) (25,75) (50,75) (75,75) (100,75) r3: (0,50) (25,50) (50,50) (75,50) (100,50) r4: (0,25) (25,25) (50,25) (75,25) (100,25) r5: (0,0) (25,0) (50,0) (75,0) (100,0)
edit: added variable offsets, blank final row , variable columns per row.
proc getpointlist {rect_boundary urowoffset lrowoffset ucoloffset lcoloffset plist} { set cordlist $rect_boundary set xl [lindex $cordlist 0] set yl [lindex $cordlist 1] set xh [lindex $cordlist 2] set yh [lindex $cordlist 3] set xpoints 0 foreach r $plist { if {[llength $r] > $xpoints} {set xpoints [llength $r]} } set ypoints [llength $plist] set rowincr [expr {($xh-$xl-$lrowoffset-$urowoffset)/($xpoints-1)}] set colincr [expr {($yh-$yl-$lcoloffset-$ucoloffset)/$ypoints}] set count 0 set list "" {set y [expr {$yh - $ucoloffset}]} {$y >= [expr {$yl + $lcoloffset}]} {incr y -$colincr} { set x [expr {$xl + $lrowoffset}] foreach n [lindex $plist $count] { lappend list $x $y incr x $rowincr } incr count if {$count == $ypoints} {return $list} } } set plist {{a b c d x} {e f g h} {i k l} {m n}} set qlist 1 foreach n $plist { set pattern$plist $n incr qlist } set rect_boundary {0 0 100 100} set upperrowoffset 0 set lowerrowoffset 0 set uppercoloffset 0 set lowercoloffset 0 set pointlist [getpointlist $rect_boundary $upperrowoffset $lowerrowoffset $uppercoloffset $lowercoloffset $plist] set count 1 foreach sub_list $plist { foreach n $sub_list { set pattern$count $n incr count } } set count 1 foreach {a b} $pointlist { set text "pattern$count" puts "command -point $a,$b -text [set $text]" incr count }
Comments
Post a Comment