前言:学校有堂课要用Windows Forms App(.Net Framework) C#做三则一(中纬、麦式、大圈)的航法程式,用以计算「距离和航向」或是「目的地经纬度」。
小妹我在这里先感谢大家愿意抽空看我这破程式码。
下图为分别为「介面」跟目前的「程式码」。
Save和Read这两个Button目前不用做,只是老师介面长那样,我先做出来而已。
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(以解决)之前用的时候把滑鼠放在变数上会出现数值,可以让我找是哪里出了问题,可是不知道动到甚么设定,现在执行程式(侦错)设中断点的时候把滑鼠放在变数上没出现数字。
拜託帮帮我,我功课做不出来,现在连找错误都没办法找,我快窒息了,好焦虑:(
图一是现况图二是以前正常的时候
补上影片
影片后面启动侦错以后我滑鼠放在程式码上看变数的数值,但电脑内建的XBOX录影没录进去,呈现出来的画面就如同图一。
1 个回答
3
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几),否则程式码会很难看得懂。
-
3 -
-
Bagou36
iT邦新手 5 级 ‧
2024-12-03 16:34:39
没有甚么条件,按下开始或是F5就执行了。
修改
bsexp301479
iT邦新手 3 级 ‧
2024-12-03 16:41:26
如果是有包在Method中要确定你的程式有正确执行到这段语法,你确定有执行到Middle_CoAndDist这段语法吗?有执行到的话会像你下图一样有个黄色箭头,目前看起来你是没执行到你要侦错的地方,程式要有执行到才能看到变数的值
修改
Bagou36
iT邦新手 5 级 ‧
2024-12-03 21:15:17
好的谢谢大哥!
修改