從0自學C#07--螺旋隊列和螺旋運動

來源:互聯網
上載者:User

基於螺旋隊列邏輯的螺旋運動實現

螺旋隊列演算法的逆向方法,控制兩軸馬達按螺旋軌跡運動,如。

1.螺旋隊列演算法分析

是螺旋隊列。設1的座標是(0,0),x方向向右為正,y方向向下為正,例如,7的座標為(-1,-1),2的座標為(1,0)。編程實現輸入任意一點座標(x,y),輸出所對應的數字!(轉自網路)


每圈最大值max=(2*c+1)(2*c+1),c為由內往外的圈數。

這些基準值與max之間的差分別是1C(上邊),3C(左邊),5C(下邊),7C(右邊)(C表示當前圈數),在上邊和下邊,y座標表示(或等於)圈數(即C=y),而在左邊和右邊,x座標表示(或等於)圈數(即C=x)。因此前面提到的差值又可用座標表示成1y,3x,5y,7x。

代碼實現:

private static Object spiral(int x, int y)     {          int c = max(abs(x), abs(y));// 當前座標所在圈          int max = (c * 2 + 1) * (c * 2 + 1);// 當前圈上最大值          if (y == -c) { // 上邊              return max + (x + y);          } else if (x == -c) {// 左邊              return max + (3 * x - y);          } else if (y == c) {// 下邊              return max + (-x - 5 * y);          } else {// 右邊              return max + (-7 * x + y);          }      }

2.螺旋運動

首先自訂座標運算,表示PD的邏輯位置。

struct Coordinate    {        public int X;        public int Y;        public Coordinate(int a, int b)        {            X = a;            Y = b;        }        public static bool operator ==(Coordinate loc1, Coordinate loc2)        {            return (loc1.X == loc2.X) && (loc1.Y == loc2.Y);        }        public static bool operator !=(Coordinate loc1, Coordinate loc2)        {            return !(loc1 == loc2);        }        public override bool Equals(object loc)        {            return this == (Coordinate)loc;        }        public override int GetHashCode()        {            return base.GetHashCode();        }        public static Coordinate operator -(Coordinate loc1, Coordinate loc2)        {            return new Coordinate(loc1.X - loc2.X, loc1.Y - loc2.Y);        }        public static Coordinate operator +(Coordinate loc1, Coordinate loc2)        {            return new Coordinate(loc1.X + loc2.X, loc1.Y + loc2.Y);        }        public override string ToString()        {            return "(" + X + "," + Y + ")";        }    }

然後逆向方法,根據步數算出x,y座標。

public Coordinate ToLocation(int step, int pulse)        {            int c = (int)Math.Ceiling((Math.Sqrt(step) - 1) / 2);            int max = (int)Math.Pow(2 * c + 1, 2);            int x, y;            y = -c;//top            x = -(max + y - step);            if (Math.Abs(x) <= Math.Abs(y))            {                this.location.X = x * pulse;                this.location.Y = y * pulse;                return this.location;            }            x = -c;//left            y = max + 3 * x - step;            if (Math.Abs(y) <= Math.Abs(x))            {                this.location.X = x * pulse;                this.location.Y = y * pulse;                return this.location;            }            y = c;//bottom            x = max - 5 * y - step;            if (Math.Abs(x) <= Math.Abs(y))            {                this.location.X = x * pulse;                this.location.Y = y * pulse;                return this.location;            }            x = c;//right            y = -(max - 7 * x - step);            this.location.X = x * pulse;            this.location.Y = y * pulse;            //LocChange();            return this.location;        }


最後,根據座標的變化實現運動。

public void Start()        {            Coordinate moveToLoc, currentLoc, deltaLoc;                        currentLoc = ToLocation(1, 0);            logInfo = string.Format("{0}: {1}{2}.", DateTime.Now.ToString("HH:mm:ss"), "the start location is ", currentLoc.ToString());            log.SaveLogToTxt(logInfo);            logInfo = string.Format("{0}: {1}.", DateTime.Now.ToString("HH:mm:ss"), "begin to move... ");            log.SaveLogToTxt(logInfo);            for (int step = 1; step <= this.roMaxStep[0]; step++)            {                moveToLoc = ToLocation(step + 1, this.roPulse[0]);                deltaLoc = moveToLoc - currentLoc;                logInfo = string.Format("{0}: step{1}{2}{3}...", DateTime.Now.ToString("HH:mm:ss"), step + " ", "move to ", moveToLoc.ToString());                log.SaveLogToTxt(logInfo);                bool moveX = card.MoveX(deltaLoc.X);                bool moveY = card.MoveY(deltaLoc.Y);                if (moveX == false || moveY == false)                    //throw error                    return;                currentLoc = moveToLoc;                                //if RES > RoRESTarget                //break;            }            logInfo = string.Format("{0}: {1}.", DateTime.Now.ToString("HH:mm:ss"), "move done");            log.SaveLogToTxt(logInfo);            logInfo = string.Format("{0}: {1}{2}.", DateTime.Now.ToString("HH:mm:ss"), "the current location is ", currentLoc.ToString());            log.SaveLogToTxt(logInfo);        }

以上就是 從0自學C#07--螺旋隊列和螺旋運動的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.