I am new to Tcl arrays. My question is as follows.
I have a rectangle box with two rows R1 and R2. Each of these rows has 8 different values. I want to return these 16 values (x and y coordinates) either in a text file or as an list output from a proc. I read some earlier posts about Tcl proc cannot output an array unless we use dict. So, I will try to draw a picture so u can understand my question better.
R1 x1y1 x2y2 ... x8,y8 R2 x9,y9 ... x16, y16
Expected output when I run the proc either on command prompt or in a file with dummy values as an example
$> (1,2) (2,3) (3,4) ....... (7,8) (9,10) (10,11) ......... (15,16)
So this is what I tried and I am getting the results that I need. But this is hardcoded for two rows. I want to make it able to detect how many rows are there and then accordingly output the 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 "" ;
for {set y [expr {$yh - $colOffset}]} {$y >= [expr {$yl + $colOffset}]} { incr y $colIncr } {
for {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 I need to implement in this code based on yh-yl and xh-xl to calculate height and width of rectangle and accordingly output rows
Command to call the proc
$> getPointList $rect_boundary $rowOffset $colOffset $rowIncr $colIncr
Just for your understanding there are eight x,y points inside the rectangle on a particular row. x offset is the first x point on a row from the left or roght boundary, thereafter all the points are separated by an increment value which I call rowIncr. Same holds true for column.
Expected output : This is what the above code does but it is hardcoded for two rows. I want to increase and implement the logic if the rows and column are 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 for better clarity as this 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 so on ...