iT邦幫忙

0

讓我們一起完成一個C#小程式 ♡

  • 分享至 

  • xImage

前言:學校有堂課要用Windows Forms App(.Net Framework) C#做三則一(中緯、麥式、大圈)的航法程式,用以計算「距離和航向」或是「目的地經緯度」。


小妹我在這裡先感謝大家願意抽空看我這破程式碼。

下圖為分別為「介面」跟目前的「程式碼」。

Save和Read這兩個Button目前不用做,只是老師介面長那樣,我先做出來而已。

https://ithelp.ithome.com.tw/upload/images/20241203/20170842bykgC3Xu3O.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace navigation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Size = new Size(450,550);
        }

        double LatS, LatE, LonS, LonE, l, Lm, Dlo, p, Dist, Co,C, LatVertex,Dlov, Degs, D, M, S;

        private void button1_Click(object sender, EventArgs e)
        {
            //宣告變數
            double LatS, LatE, LonS, LonE, Co, Dist, LatVertex,Dlov;

            //獲得出發點與目的地的值
            LatS = Convert.ToDouble(textBox1.Text) + Convert.ToDouble(textBox2.Text) / 60 + Convert.ToDouble(textBox3.Text) / 3600;
            LonS = Convert.ToDouble(textBox4.Text) + Convert.ToDouble(textBox5.Text) / 60 + Convert.ToDouble(textBox6.Text) / 3600;
            LatE = Convert.ToDouble(textBox7.Text) + Convert.ToDouble(textBox8.Text) / 60 + Convert.ToDouble(textBox9.Text) / 3600;
            LonE = Convert.ToDouble(textBox10.Text) + Convert.ToDouble(textBox11.Text) /60 + Convert.ToDouble(textBox12.Text) /3600;

            //用if判斷ComboBox裡面的方位
            if (comboBox1.Text == "S")
            {
                LatS = LatS * -1;
            }
            if (comboBox2.Text == "W")
            {
                LonS = LonS * -1;
            }
            if (comboBox3.Text == "S")
            {
                LatE = LatE * -1;
            }
            if (comboBox4.Text == "W")
            {
                LonE = LonE * -1;
            }

            Dist = 0;
            Co = 0;
            LatVertex = 0;
            Dlov = 0;


            if (comboBox3.Text=="Middle_Latitude")
            {
                this.Middle_Latitude_CoAndDist(LatS, LonS, LatE, LonE, ref Dist, ref Co);
            }

            if (comboBox3.Text == "Mercator")
            {
                this.Mercator_CoAndDist(LatS, LonS, LatE, LonE, ref Dist, ref Co);
            }

            if (comboBox3.Text == "Great_Circle")
            {
                this.GreatCirle_CoAndDist(LatS, LonS, LatE, LonE, ref Dist, ref C,ref LatVertex,ref Dlov);
            }

            textBox13.Text = Co.ToString();
            textBox14.Text = Dist.ToString();

            if (comboBox3.Text == "Great_Circle")
            {
                textBox13.Text = C.ToString();
                textBox15.Text = LatVertex.ToString();
                textBox16.Text = Dlov.ToString();
            }
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            LatS = Convert.ToDouble(textBox1.Text) + Convert.ToDouble(textBox2.Text) / 60 + Convert.ToDouble(textBox3.Text) / 3600;
            LonS = Convert.ToDouble(textBox4.Text) + Convert.ToDouble(textBox5.Text) / 60 + Convert.ToDouble(textBox6.Text) / 3600;
            if (comboBox3.Text =="Middle_Latitude")
            {
                this.Middle_Latitude_Destination(LatS,LonS,Dist,Co,ref LatE,ref LonE);
            }

            if (comboBox3.Text == "Mercator")
            {
                this.Mercator_Destination(LatS, LonS, Dist, Co, ref LatE, ref LonE);
            }

            if (comboBox3.Text == "Great_Circle")
            {
                this.Great_Circle_Destination(LatS, LonS, Dist, Co, ref LatE, ref LonE);
            }

            if (LonE < 0)
            {
                comboBox4.Text = "W"; // 經度為負,設置為西經
            }
            else
            {
                comboBox4.Text = "E"; // 經度為正,設置為東經
            }

            if (LatE < 0)
            {
                comboBox3.Text = "S"; // 緯度為負,設置為南緯
            }
            else
            {
                comboBox3.Text = "N"; // 緯度為正,設置為北緯
            }



            //度轉度分秒,並呈現在textBox
            Degs = Math.Abs(LatE);
            D = (int)Degs;

            M = (int)((Degs - D) * 60);
            S = (Degs - D - M / 60) * 3600;
            textBox7.Text = D.ToString("000");
            textBox8.Text = M.ToString("00");
            textBox9.Text = S.ToString();

            Degs = LonE;
            D = (int)Degs;
            M = (int)((Degs - D) * 60);
            S = (Degs - D - M / 60) * 3600;
            textBox10.Text = D.ToString("000");
            textBox11.Text = M.ToString("00");
            textBox12.Text = S.ToString("");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();
            textBox9.Clear();
            textBox10.Clear();
            textBox11.Clear();
            textBox12.Clear();
            textBox13.Clear();
            textBox14.Clear();
            textBox15.Clear();
            textBox16.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Middle_Latitude_CoAndDist(double LatS, double LonS, double LatE, double LonE, ref double Dist, ref double Co)
        {
            //計算
            l = LatE - LatS;
            Lm = (LatE + LatS) / 2;
            Dlo = LonE - LonS;

            //修正Dlo
            if (Dlo > 180)
            {
                Dlo = Dlo - 360;
            }
            else if (Dlo < -180)
            {
                Dlo = Dlo + 360;
            }

            p = Dlo * Math.Cos(Lm * Math.PI / 180);

            if (Lm == 0)
            {
                p = Dlo;
            }

            if (l == 0)
            {
                if (Dlo > 0) { Co = 90; }
                else if (Dlo < 0) { Co = 270; }
                Dist = Math.Abs(Dlo * Math.Cos(LatS * Math.PI / 180) * 60);
            }
            else
            {
                Co = Math.Atan2(p , l);
                Dist = (Math.Abs(l / Math.Cos(Co))) * 60;
                Co = Co * 180 / Math.PI;
            }

            //修正(因為前面有修正+象限問題,這裡航向角是負的)
            /*if (l < 0)
            {
                Co = Co + 180;
            }
            else if (l > 0 & Dlo < 0)
            {
                Co = Co + 360;
            }
            else if (Co > 360)
            {
                Co = Co - 360;
            }
            */
        }

        private void Mercator_CoAndDist(double LatS, double LonS, double LatE, double LonE, ref double Dist, ref double Co)
        {
            //計算
            l = LatE - LatS;
            Dlo = LonE - LonS;

            //計算出緯度見長比數差(m)
            double M1, M2, m;
            M1 = 7915.704468 * Math.Log(Math.Tan(45 + LatS / 2)) - 23.0133633 * Math.Sin(LatS) - 0.051353 * Math.Pow(Math.Sin(LatS), 3) - 0.000206 * Math.Pow(Math.Sin(LatS), 5);
            M2 = 7915.704468 * Math.Log(Math.Tan(45 + LatE / 2)) - 23.0133633 * Math.Sin(LatE) - 0.051353 * Math.Pow(Math.Sin(LatE), 3) - 0.000206 * Math.Pow(Math.Sin(LatE), 5);
            m = 0;
            if (LatS > 0 && LatE < 0)
            {
                m = M2 + M1;
            }
            else if (LatS < 0 && LatE > 0)
            {
                m = M2 + M1;
            }
            else if (M1 > M2)
            {
                m = M1 - M2;
            }
            else if (M1 < M2)
            {
                m = M2 - M1;
            }

            Co = Math.Atan(Dlo /m);

            if (Dlo > 180)
            {
                Dlo = Dlo - 360;
            }
            else if (Dlo < -180)
            {
                Dlo = Dlo + 360;
            }

            p = Dlo * Math.Cos(Lm * Math.PI / 180);

            //防止變成平行
            Lm = (LatE + LatS) / 2;
            if (Lm == 0)
            {
                p = Dlo;
            }
            //防止變成平行
            if (l == 0)
            {
                if (Dlo > 0) { Co = 90; }
                else if (Dlo < 0) { Co = 270; }
                Dist = Math.Abs(Dlo * Math.Cos(LatS * Math.PI / 180) * 60);
            }
            else
            {
                Co = Math.Atan(p / l);
                Dist = (Math.Abs(l / Math.Cos(Co))) * 60;
                Co = Co * 180 / Math.PI;
            }


            //修正(因為前面有修正+象限問題,這裡航向角是負的)
            if (l < 0)
            {
                Co = Co + 180;
            }
            else if (l > 0 & Dlo < 0)
            {
                Co = Co + 360;
            }
            else if (Co > 360)
            {
                Co = Co - 360;
            }
        }

        private void GreatCirle_CoAndDist(double LatS,double LonS,double LatE,double LonE,ref double Dist,ref double C,ref double LatVertex, ref double Dlov)
        {
            //求Dist&C(起始點航向)
            Dlo = (LonE - LonS) * Math.PI / 180;
            
            if (Dlo > 180)
            {
                Dlo = Dlo - 360;
            }
            else if (Dlo < -180)
            {
                Dlo = Dlo + 360;
            }

            Dist = Math.Acos(Math.Sin(LatS) * Math.Sin(LatE) + Math.Cos(LatS) * Math.Cos(LatE) * Math.Cos(Dlo));
            C = Math.Acos((Math.Sin(LatE) - Math.Sin(LatS) * Math.Cos(Dist))/(Math.Cos(LatS)*Math.Sin(Dist)));

            


            //求LatVertex
            LatVertex = Math.Acos(Math.Cos(LatS)*Math.Sin(C));

            //求起點到大圈頂點的距離
            Dlov = Math.Asin(Math.Cos(C)/Math.Sin(LatVertex));
        }

        private void Middle_Latitude_Destination(double LatS,double LonS,double Dist,double Co,ref double LatE,ref double LonE)
        {
            //用if判斷ComboBox裡面的方位
            if (comboBox1.Text == "S")
            {
                LatS = LatS * -1;
            }
            if (comboBox2.Text == "W")
            {
                LonS = LonS * -1;
            }

            Dist = Convert.ToDouble(textBox13.Text) / 60;
            Co = Convert.ToDouble(textBox14.Text) * Math.PI / 180;

            //計算
            l = Dist * Math.Cos(Co);
            LatE = (LatS + l);
            Lm = (LatS + LatE) / 2 * Math.PI / 180;
            p = l * Math.Tan(Co);
            Dlo = p / Math.Cos(Lm);
            LonE = (LonS + Dlo);
        }

        private void Mercator_Destination(double LatS, double LonS, double Dist, double Co, ref double LatE, ref double LonE)
        {
            //用if判斷ComboBox裡面的方位
            if (comboBox1.Text == "S")
            {
                LatS = LatS * -1;
            }
            if (comboBox2.Text == "W")
            {
                LonS = LonS * -1;
            }

            //距離分換度,方位度換
            Dist = Convert.ToDouble(textBox13.Text) / 60;
            Co = Convert.ToDouble(textBox14.Text) * Math.PI / 180;

            //計算l&LatE
            l = Dist * Math.Cos(Co);
            LatE = (LatS + l);

            //計算出緯度見長比數差(m)
            double M1, M2, m;
            M1 = 7915.704468 * Math.Log(Math.Tan(45 + LatS / 2)) - 23.0133633 * Math.Sin(LatS) - 0.051353 * Math.Pow(Math.Sin(LatS), 3) - 0.000206 * Math.Pow(Math.Sin(LatS), 5);
            M2 = 7915.704468 * Math.Log(Math.Tan(45 + LatE / 2)) - 23.0133633 * Math.Sin(LatE) - 0.051353 * Math.Pow(Math.Sin(LatE), 3) - 0.000206 * Math.Pow(Math.Sin(LatE), 5);
            m = 0;
            if (LatS > 0 && LatE < 0)
            {
                m = M2 + M1;
            }
            else if (LatS < 0 && LatE > 0)
            {
                m = M2 + M1;
            }
            else if (M1 > M2)
            {
                m = M1 - M2;
            }
            else if (M1 < M2)
            {
                m = M2 - M1;
            }

            //求Dlo
            Dlo = Math.Tan(Co) / m;
            LonE = LonS + Dlo;
        }

        private void Great_Circle_Destination(double LatS, double LonS, double Dist, double Co, ref double LatE, ref double LonE)
        {

        }
    }
}



2024/12/03/2112(問題)

第34行的LonE為何125+12/60+12/3600後是0?

LonE = Convert.ToDouble(textBox10.Text) + Convert.ToDouble(textBox11.Text) /60 + Convert.ToDouble(textBox12.Text) /3600;


2024/12/03(以解決)
之前用的時候把滑鼠放在變數上會出現數值,可以讓我找是哪裡出了問題,可是不知道動到甚麼設定,現在執行程式(偵錯)設中斷點的時候把滑鼠放在變數上沒出現數字。

拜託幫幫我,我功課做不出來,現在連找錯誤都沒辦法找,我快窒息了,好焦慮:(

https://ithelp.ithome.com.tw/upload/images/20241202/20170842bIERpStwG1.png
https://ithelp.ithome.com.tw/upload/images/20241203/20170842lFGx8zS1JC.jpg
圖一是現況
圖二是以前正常的時候

補上影片Yes

影片後面啟動偵錯以後我滑鼠放在程式碼上看變數的數值,但電腦內建的XBOX錄影沒錄進去,呈現出來的畫面就如同圖一。

看更多先前的討論...收起先前的討論...
copemoe iT邦好手 3 級 ‧ 2024-12-03 16:15:15 檢舉
錯誤訊息?
cloud1991 iT邦新手 5 級 ‧ 2024-12-03 16:15:15 檢舉
我猜一下
1.偵錯沒進入這段code
2.專案開啟-啟動但不偵錯
Bagou36 iT邦新手 5 級 ‧ 2024-12-03 16:35:30 檢舉
錯誤訊息是指?
Bagou36 iT邦新手 5 級 ‧ 2024-12-03 16:37:37 檢舉
1.請問要怎樣查看偵錯是否進入這段code?
2.沒有,我是按開始或是F5,不是按Ctrl+F5。
wdchiang iT邦新手 2 級 ‧ 2024-12-03 18:27:09 檢舉
副程序又沒觸發 postback 也沒看到其他地方有呼叫?怎麼可能會觸發?
Bagou36 iT邦新手 5 級 ‧ 2024-12-03 21:16:49 檢舉
我把完整的程式貼上來?
wdchiang iT邦新手 2 級 ‧ 2024-12-04 07:52:11 檢舉
if (comboBox3.Text=="Middle_Latitude") -> 這裡寫錯了,航法選擇應該不是 comboBox3 ??
supermaxfight iT邦研究生 4 級 ‧ 2024-12-04 09:10:41 檢舉
座標除完後要注意位數補足?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
bsexp301479
iT邦新手 3 級 ‧ 2024-12-03 15:29:45

你這段程式碼前面有沒有什麼條件式?
看起來偵錯點沒有被觸發到呀
程式碼只有一小段不能確定你實際遇到的狀況

第34行的LonE為何125+12/60+12/3600後是0?

LonE = Convert.ToDouble(textBox10.Text) + Convert.ToDouble(textBox11.Text) /60 + Convert.ToDouble(textBox12.Text) /3600;

既然你現在都可以用偵錯點檢查變數的值了,那也請你用相同的方式來檢查你的每個TextBox所傳入的值。
雖然畫面是仿照老師的樣子,但每個TextBox的名稱可能會不同,我是建議你每個TextBox都用容易辨識的名稱做區分(例如:距離就用txt_distance不要再用textBox幾),否則程式碼會很難看得懂。

Bagou36 iT邦新手 5 級 ‧ 2024-12-03 16:34:39 檢舉

沒有甚麼條件,按下開始或是F5就執行了。

如果是有包在Method中要確定你的程式有正確執行到這段語法,你確定有執行到Middle_CoAndDist這段語法嗎?有執行到的話會像你下圖一樣有個黃色箭頭,目前看起來你是沒執行到你要偵錯的地方,程式要有執行到才能看到變數的值

Bagou36 iT邦新手 5 級 ‧ 2024-12-03 21:15:17 檢舉

好的謝謝大哥!

我要發表回答

立即登入回答