首頁 後端開發 C#.Net教程 從0自學C#07--螺旋隊列與螺旋運動

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

Feb 04, 2017 am 10:46 AM

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

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

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

1.螺旋隊列演算法分析

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

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


每圈最大值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--螺旋隊列與螺旋運動的內容,更多相關內容請關注PHP中文網(www.php.cn)!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
使用 C# 的活動目錄 使用 C# 的活動目錄 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

C# 中的隨機數產生器 C# 中的隨機數產生器 Sep 03, 2024 pm 03:34 PM

C# 隨機數產生器指南。在這裡,我們討論隨機數產生器的工作原理、偽隨機數和安全數的概念。

C# 資料網格視圖 C# 資料網格視圖 Sep 03, 2024 pm 03:32 PM

C# 資料網格視圖指南。在這裡,我們討論如何從 SQL 資料庫或 Excel 檔案載入和匯出資料網格視圖的範例。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範例和程式碼實作。

c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範例和程式碼實作。

C# 中的質數 C# 中的質數 Sep 03, 2024 pm 03:35 PM

C# 質數指南。這裡我們討論c#中素數的介紹和範例以及程式碼實作。

xml怎麼改格式 xml怎麼改格式 Apr 03, 2025 am 08:42 AM

可以採用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進行自動格式化;使用 XML 轉換工具(如 XSLT)定義轉換規則;或者使用編程語言(如 Python)進行解析和操作。修改時需謹慎,並備份原始文件。

See all articles