测量中角度处理常用函数

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 改为相关项目的命名空间
{
    /// <summary>
    /// 常用角度处理类
    /// </summary>
    /// <author>llc3s@foxmail.com</author>
    public class CommonAngleFun
    {
        #region 十进制度转化为弧度
        /// <summary>
        /// 十进制度转化为弧度
        /// </summary>
        /// <param name="dd"></param>
        /// <returns></returns>
        public static double deg2rad(double dd)
        {
            return dd * Math.PI / 180.0;
        }

        #endregion

        #region 空格分隔的度分秒转化为十进制度
        /// <summary>
        /// 空格分隔的度分秒转化为十进制度
        /// </summary>
        /// <param name="temp"></param>
        public static double str2dfm(string temp)
        {
            temp = temp.Trim();
            double[] ang = new double[3];
            double j = -1.0;
            //验证是否为合格的度分秒
            try
            {
                string[] angles = temp.Split(" ".ToCharArray());
                if (angles.Length != 3)
                {
                    MessageBox.Show("度分秒格式不合格");
                    return j;
                }

                for (int i = 0; i < 3; i++)
                {
                    ang[i] = double.Parse(angles[i]);
                }

                j = ang[0] + ang[1] / 60 + ang[2] / 3600;
            }
            catch
            {
                MessageBox.Show("不是合格的度分秒数据,度分秒之间以空格分隔");
            }
            return j;
        }
        #endregion

        #region 空格分隔的度分秒字符串转化为弧度
        /// <summary>
        /// 空格分隔的度分秒转化为弧度
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public static double str2rad(string temp)
        {
            return deg2rad(str2dfm(temp));
        }
        #endregion

        #region 弧度转化为十进制度
        /// <summary>
        /// 弧度转化为十进制度分秒
        /// </summary>
        /// <param name="rad"></param>
        public static double rad2Degree(double rad)
        {
            double degree = rad * 180 / Math.PI;
            return degree;
        }
        #endregion

        #region 十进制度转化为度分秒
        /// <summary>
        /// 十进制度转化为度分秒
        /// </summary>
        /// <param name="degree"></param>
        /// <param name="n">表示十进制度转化为度分秒时秒位保留的小数位数</param>
        /// <returns></returns>
        public static double[] degree2DFM(double degree,int n)
        {
            double[] dfm = new double[3];
            double d = Math.Floor(degree);
            double f = Math.Floor((degree - d) * 60);
            double m = Math.Round(((degree - d) * 60 - f) * 60,0);
            dfm[0] = d;
            dfm[1] = f;
            dfm[2] = m;
            return dfm;
        }

        #endregion

        #region 度分秒转化为空格分隔的字符串
        /// <summary>
        /// 度分秒转化为空格分隔的字符串
        /// </summary>
        /// <param name="dfm"></param>
        /// <returns></returns>
        public static string dfm2Str(double[] dfm)
        {
            string str = string.Empty;
            for (int i = 0; i < dfm.Length; i++)
            {
                str += dfm[i].ToString() + " ";
            }
            return str;
        }

        #endregion

        #region 弧度转化为空格分隔的度分秒字符串
        /// <summary>
        /// 弧度转化为空格分隔的度分秒字符串
        /// </summary>
        /// <param name="rad"></param>
        /// <param name="n">十进制度转化为度分秒时秒位的小数位数</param>
        /// <returns></returns>
        public static string rad2StrDfm(double rad,int n)
        {
            return dfm2Str(degree2DFM(rad2Degree(rad),n));
        }
        #endregion

        #region 夹角计算
        /// <summary>
        /// 夹角计算.用于测量中方向观测法或类似的夹角计算
        /// </summary>
        /// <param name="ang1"></param>
        /// <param name="ang2"></param>
        /// <returns></returns>
        private double[] jiajiao(double[] ang1, double[] ang2)
        {
            if (ang1.Length != 3 || ang2.Length != 3)
            {
                MessageBox.Show("错误");
                return null;
            }
            else
            {
                //预处理
                //度位待求方向是否大于0方向,不大于,加360;
                if (ang2[0] < ang1[0])
                    ang2[0] += 360;

                //bool mj = false;
                //bool dj = false; //度/分借位标记.

                if (ang2[2] < ang1[2])
                {
                    //mj = true;
                    ang2[2] += 60;
                    ang2[1] -= 1; //秒位加60,分位-1
                }

                if (ang2[1] < ang1[1])
                {
                    //dj=true;
                    ang2[1] += 60;
                    ang2[0] -= 1; //分位加60,度位-1;
                }

                //开始计算
                double[] result = new double[3];
                for (int i = 0; i < 3; i++)
                {
                    result[i] = ang2[i] - ang1[i];
                }
                return result;
            }

        }
        #endregion

        #region 方位角计算
        /// <summary>
        /// 方位角计算(第二个点减第一个点)
        /// </summary>
        /// <param name="x1">第一个点</param>
        /// <param name="y1">第一个点</param>
        /// <param name="x2">第二个点</param>
        /// <param name="y2">第二个点</param>
        /// <returns></returns>
        public static double fangweijiao(double x1, double y1, double x2, double y2)
        {
            double fjrad = Math.Atan(Math.Abs((y1 - y2) / (x1 - x2)));
            //判断象限
            //第一象限
            if (y2 - y1 > 0 && x2 - x1 > 0)
                return fjrad;
            //第二象限
            else if (y2 - y1 > 0 && x2 - x1 < 0)
            {
                return Math.PI - fjrad;
            }
            else if (y2 - y1 < 0 && x2 - x1 < 0)
            {
                //第三象限
                return Math.PI + fjrad;
            }
            else if (y2 - y1 < 0 && x2 - x1 > 0)
            {
                //第四象限
                return (Math.PI * 2 - fjrad);
            }
            else
                return -1;

        }

        #endregion

        #region 任意两方位角的夹角计算

        #endregion
    }
}

 

posted @ 2013-06-28 13:54  DayDreamInGIS  阅读(368)  评论(0编辑  收藏  举报