C#计算两地点最近直线距离

C# 计算两点最近直线距离

代码如下

参数介绍:
X1:起始地点精度
Y1:起始地点纬度
X2:目标地点精度
Y2:目标地点纬度
返回值单位为“米”

public static double CalcMil(double X1, double Y1, double X2, double Y2)
        {
            double PI = Math.PI;
            double EARTH_RADIUS = 6378137;  //地球半径 

            double CurRadLong = 0;	//两点经纬度的弧度
            double CurRadLat = 0;
            double PreRadLong = 0;
            double PreRadLat = 0;
            double a = 0, b = 0;              //经纬度弧度差
            double MilValue = 0;

            //将经纬度换算成弧度
            CurRadLong = (double)(X1);
            CurRadLong = CurRadLong * PI / 180.0;

            PreRadLong = (double)(X2);
            PreRadLong = PreRadLong * PI / 180.0;

            CurRadLat = (double)(Y1);
            CurRadLat = CurRadLat * PI / 180.0f;

            PreRadLat = (double)(Y2);
            PreRadLat = PreRadLat * PI / 180.0f;

            //计算经纬度差值
            if (CurRadLat > PreRadLat)
            {
                a = CurRadLat - PreRadLat;
            }
            else
            {
                a = PreRadLat - CurRadLat;
            }

            if (CurRadLong > PreRadLong)
            {
                b = CurRadLong - PreRadLong;
            }
            else
            {
                b = PreRadLong - CurRadLong;
            }

            MilValue = 2 * Math.Asin(Math.Sqrt(Math.Sin(a / 2.0) * Math.Sin(a / 2.0) + Math.Cos(CurRadLat) * Math.Cos(PreRadLat) * Math.Sin(b / 2.0) * Math.Sin(b / 2.0)));
            MilValue = (double)(EARTH_RADIUS * MilValue);
            return MilValue;
        }
在AutoCAD的开发环境中,使用C#语言来计算两条直线之间的距离,可以通过获取直线的端坐标来实现。AutoCAD中每条直线由起和终定义,计算距离的基本思路是根据两之间的距离公式(欧几里得距离公式)来计算直线两端与另一条直线两端之间的距离,然后找出这些距离中的最小值。 假设我们有两条直线L1和L2,L1由端A1(x1, y1)和B1(x2, y2)定义,L2由端A2(x3, y3)和B2(x4, y4)定义。那么计算L1和L2之间距离的步骤如下: 1. 计算L1两端到L2两端距离,即AB与A2B2、AB与A2B2、A1B1与A2B2和A1B1与A2B2这四对之间的距离。 2. 使用两之间的距离公式:distance = √((x2 - x1)^2 + (y2 - y1)^2)。 3. 比较这四个距离,最小的那个就是两条直线之间的最短距离。 以下是一个简单的C#代码示例,演示如何计算两条直线之间的距离: ```csharp using System; public class LineDistanceCalculator { public static double CalculateDistance(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double[] line1 = new double[] { x1, y1, x2, y2 }; double[] line2 = new double[] { x3, y3, x4, y4 }; return Math.Min( GetDistance(line1[0], line1[1], line2[0], line2[1]), Math.Min( GetDistance(line1[0], line1[1], line2[2], line2[3]), Math.Min( GetDistance(line1[2], line1[3], line2[0], line2[1]), GetDistance(line1[2], line1[3], line2[2], line2[3]) ) ) ); } private static double GetDistance(double x1, double y1, double x2, double y2) { return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李袁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值