Looping in a spiral

前端 未结 30 2425
独厮守ぢ
独厮守ぢ 2020-11-22 15:07

A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fe

30条回答
  •  悲哀的现实
    2020-11-22 15:27

    Solution for AutoIt

    #include 
    #include 
    
    Func SpiralSearch($xMax,$yMax)
        $x = 0
        $y = 0
        $dx = 0
        $dy = -1
        for $i=0 To _max($xMax, $yMax)^2-1 Step 1
            if -$xMax/2 < $x and $x <= $xMax/2 And -$yMax/2 < $y And $y <= $yMax/2 Then
                MsgBox(0, "We are here ", $x & " " & $y)
            EndIf
            if $x == $y or ($x < 0 and $x == -$y) or ($x > 0 and $x == 1-$y) Then
                _ArraySwap ($dx, $dy)
                $dx=-$dx
            EndIf
            $x += $dx
            $y += $dy
        Next
    EndFunc
    

提交回复
热议问题