为什么在循环过程中不能使用while

时间:2018-03-31 04:14:07

标签: tcl

为什么我的脚本不起作用。我正在使用while循环过程。 希望有人能帮我解决我的问题。脚本如下;

设置全局

$config['compress_output'] = FALSE;
And this worked for me, but the real solution should be inside apache configurations




or you can do this
$config['compress_output'] = TRUE;

 function reply(){
        $msg = $this->Message_model;        
        $insert = $msg->send_message2();
        header("Content-type: application/json; charset=utf-8");
echo json_encode(array("status" => TRUE));
die;


    }

设置参数

global xmin xmax ymin ymax xVer yVer x1 y1 Count

执行循环过程

set xmin 0
set xmax 51
set ymin 0
set ymax 51

set x1 2
set y1 2

set xVer 2
set yVer 2

set Count 1
set goToVer "n"

提前致谢!

1 个答案:

答案 0 :(得分:0)

我不知道问题是什么,但我们可以采取一些措施来改善一切。第一步:让我们将坐标转换代码本身分解为一个小程序(我也修复了表达式周围的大括号):

proc convert {x y length azimuth dip} {
    set dist [expr {cos($dip) * $length}]
    set x1 [expr {$x + sin($azimuth) * $dist}]
    set y1 [expr {$y + cos($azimuth) * $dist}]
    return [list $x1 $y1]
}

while {$x1 > $xmin && $x1 < $xmax && $y1 > $ymin && $y1 < $ymax} {
  # For horizontal axis
  while {$x1 > $xmin && $x1 < $xmax} {
    set azi    [expr (45+90)]
    set dip    0
    set length 2
    lassign [convert $x1 $y1 $length $azi $dip] x1 y1
    set goToVer "y"
    incr Count
  }

  # For vertical axis
  if {$goToVer == "y"} {
    set azi   45
    set dip    0
    set length 5
    lassign [convert $xVer $yVer $length $azi $dip] x1 y1
    incr Count
  }
}

接下来,内循环中azi的值是可疑的;看起来它是以度为单位但是Tcl的三角函数(就像大多数其他编程语言中的函数一样)以弧度为单位。乘以π/ 180°。

最后,循环的逻辑很奇怪。我并不是说这是错的......但是我真的很难理解你使用的循环方式。要在轴上使用相等的步骤在一些空间上循环一对坐标,可以使用带有整数迭代器变量的for循环,然后应用转换来获取浮点坐标(这是最好的,因为它限制了累积误差):

set azi [expr {(45 + 90) * 3.1415927/180}]
set dip 0
set length  2
for {set x $xmin} {$x <= $xmax} {incr x} {
    for {set y $ymin} {$y <= $ymax} {incr y} {
        set dist [expr {cos($dip) * $length}]
        set x1 [expr {$x + sin($azimuth) * $dist}]
        set y1 [expr {$y + cos($azimuth) * $dist}]
        # I assume you want to do something with $x1,$y1 here…
    }
}

或者,您可以在极坐标或任何其他常规方案中使用常规间距;这只是好的代码利用规律性,如果可以的话,强烈建议你这样做。 但这可能不是你想要做的事情。你的代码的意图令人困惑。

这让我想到了你的实际错误,这些错误似乎围绕着国家管理。 goToVer的逻辑很困惑,BTW,这可能是你遇到的问题。你是在内循环中设置它,但从那时起它总是被设置。我建议不要做那样的事情,因为它很难调试(有些情况下它可以有意义,但它看起来不像你正在做它们)而是坚持常规网格,但它们可以工作。我猜你在外循环中的某个点错过了变量的重置,可能就在内循环开始之前。