C#:(角度)前方交会(VS2010窗体+代码)

已知:A(Xa,Ya);B(Xb,Yb)

观测:∠A;∠B

求解:P(Xp,Yp)

求解示意:

求解公示:

分享给有需要的人,代码质量勿喷。 

private void btnRun_Click(object sender, EventArgs e)
        {
            #region 解算
            //已知坐标A
            string CoorAstr = txtCoorA.Text;
            if (!CoorAstr.Contains("-"))
            {
                MessageBox.Show("已知坐标A格式不正确!!!");
                return;
            }
            string[] CoorA = CoorAstr.Split('-');
            if (CoorA.Length != 2)
            {
                MessageBox.Show("已知坐标A格式不正确!!!");
                return;
            }
            double Xa = Convert.ToDouble(CoorA[0]);
            double Ya = Convert.ToDouble(CoorA[1]);

            //已知坐标B
            string CoorBstr = txtCoorB.Text;
            if (!CoorBstr.Contains("-"))
            {
                MessageBox.Show("已知坐标B格式不正确!!!");
                return;
            }
            string[] CoorB = CoorBstr.Split('-');
            if (CoorB.Length != 2)
            {
                MessageBox.Show("已知坐标B格式不正确!!!");
                return;
            }
            double Xb = Convert.ToDouble(CoorB[0]);
            double Yb = Convert.ToDouble(CoorB[1]);

            //观测角度A
            string AngleAstr = TxtAngleA.Text;
            if (!AngleAstr.Contains("-"))
            {
                MessageBox.Show("角度A格式不正确!!!");
                return;
            }
            string[] AngleA = AngleAstr.Split('-');
            if (AngleA.Length != 3)
            {
                MessageBox.Show("角度A格式不正确!!!");
                return;
            }
            float ad = Convert.ToSingle(AngleA[0]);
            if ((ad < 0) || (ad > 180))
            {
                MessageBox.Show("角度A的度不正确!!!");
                return;
            }
            float af = Convert.ToSingle(AngleA[1]);
            if ((af < 0) || (af > 60))
            {
                MessageBox.Show("角度A的分不正确!!!");
                return;
            }
            float am = Convert.ToSingle(AngleA[2]);
            if ((am < 0) || (am > 60))
            {
                MessageBox.Show("角度A的秒不正确!!!");
                return;
            }
            double A = (ad*1.0 + af / 60.0 + am / 3600.0) / 180.0 * Math.PI;

            //观测角度B
            string AngleBstr = TxtAngleB.Text;
            if (!AngleBstr.Contains("-"))
            {
                MessageBox.Show("角度B格式不正确!!!");
                return;
            }
            string[] AngleB = AngleBstr.Split('-');
            if (AngleB.Length != 3)
            {
                MessageBox.Show("角度B格式不正确!!!");
                return;
            }
            float bd = Convert.ToSingle(AngleB[0]);
            if ((bd < 0) || (bd > 180))
            {
                MessageBox.Show("角度B的度不正确!!!");
                return;
            }
            float bf = Convert.ToSingle(AngleB[1]);
            if ((bf < 0) || (bf > 60))
            {
                MessageBox.Show("角度B的分不正确!!!");
                return;
            }
            float bm = Convert.ToSingle(AngleB[2]);
            if ((bm < 0) || (bm > 60))
            {
                MessageBox.Show("角度B的秒不正确!!!");
                return;
            }
            double B = (bd * 1.0 + bf / 60.0 + bm / 3600.0) * Math.PI / 180.0;

            ////解算
            //double cotA = Math.Round(1.0 / Math.Tan(A), 6);
            //double cotB = Math.Round(1.0 / Math.Tan(B), 6);
            //double signX=Math.Sign(Ya - Yb);
            //double signY=Math.Sign(Xb - Xa);
            //double Xp = (Xa * cotB + Xb * cotA + (Ya - Yb)*signX) / (cotA + cotB);
            //double Yp = (Ya * cotB + Yb * cotA + (Xb - Xa)*signY) / (cotA + cotB);

            double Xp = (Xa * Math.Sin(A) * Math.Cos(B) + Xb * Math.Sin(B) * Math.Cos(A) + (Ya - Yb) * Math.Sign(Ya - Yb) * Math.Sin(A) * Math.Sin(B)) / (Math.Sin(A) * Math.Cos(B) + Math.Sin(B) * Math.Cos(A));
            double Yp = (Ya * Math.Sin(A) * Math.Cos(B) + Yb * Math.Sin(B) * Math.Cos(A) + (Xb - Xa) * Math.Sign(Xb - Xa) * Math.Sin(A) * Math.Sin(B)) / (Math.Sin(A) * Math.Cos(B) + Math.Sin(B) * Math.Cos(A));

            txtResultX.Text = Xp.ToString("F4");
            txtResultY.Text = Yp.ToString("F4");
            #endregion

            #region 成图
            //比例
            List<PointF> ListP = new List<PointF>();
            PointF PA = new PointF((float)Xa, (float)Ya);
            ListP.Add(PA);
            PointF PB = new PointF((float)Xb, (float)Yb);
            ListP.Add(PB);
            PointF PP = new PointF((float)Xp, (float)Yp);
            ListP.Add(PP);

            ListP.Sort((a, b) => { return a.X.CompareTo(b.X); });
            double Xmax = ListP[ListP.Count - 1].X;
            double Xmin = ListP[0].X;
            double deltaX = Xmax - Xmin;
            int picWidth = picMap.Width - 100;
            double ratioX = picWidth * 1.0 / deltaX;

            ListP.Sort((a, b) => { return a.Y.CompareTo(b.Y); });
            double Ymax = ListP[ListP.Count - 1].Y;
            double Ymin = ListP[0].Y;
            double deltaY = Ymax - Ymin;
            int picHeight = picMap.Height - 100;
            double ratioY = picHeight * 1.0 / deltaY;

            double ratio = Math.Min(ratioX, ratioY);

            //坐标点
            PointF PAnew = new PointF((float)((PA.X - Xmin) * ratio), (float)(picHeight-((PA.Y - Ymin) * ratio)));
            PointF PBnew = new PointF((float)((PB.X - Xmin) * ratio), (float)(picHeight-((PB.Y - Ymin) * ratio)));
            PointF PPnew = new PointF((float)((PP.X - Xmin) * ratio), (float)(picHeight-((PP.Y - Ymin) * ratio)));

            //线
            Graphics gh = picMap.CreateGraphics();
            Pen penAB = new Pen(Color.Black, 6);
            gh.DrawLine(penAB, PAnew,PBnew);
            Pen penP = new Pen(Color.Red, 2);
            gh.DrawLine(penP, PAnew, PPnew);
            gh.DrawLine(penP, PBnew,PPnew);

            //字符串
            Font drawFont = new Font("Times New Roman", 10);
            SolidBrush brushAB = new SolidBrush(Color.Green);
            string Astr = "A(" + PA.X.ToString() + "," + PA.Y.ToString() + ")";
            gh.DrawString(Astr, drawFont, brushAB, PAnew.X, PAnew.Y);
            string Bstr = "B(" + PB.X.ToString() + "," + PB.Y.ToString() + ")";
            gh.DrawString(Bstr, drawFont, brushAB, PBnew.X,PBnew.Y);
            SolidBrush brushP = new SolidBrush(Color.Red);
            string Pstr = "P(" + PP.X.ToString() + "," + PP.Y.ToString() + ")";
            gh.DrawString(Pstr, drawFont, brushP, PPnew);
            #endregion
        }

VS2010窗体+代码:https://download.csdn.net/download/xinjiang666/11290537

发布了63 篇原创文章 · 获赞 58 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/xinjiang666/article/details/94840509