C#实现根据地球经纬度点计算所围成的面积

C#可以使用Haversine公式来计算地球上两个经纬度点之间的距离,然后再使用海龙公式计算所围成的面积。具体步骤如下:

  1. 定义一个包含经度和纬度的结构体,用于存储地球上的点。
  2. 使用Haversine公式计算两个点之间的距离,得到一个以千米为单位的距离值。
  3. 使用海龙公式计算所围成的面积,得到一个以平方千米为单位的面积值。
  4. 返回面积值。

下面是一个示例代码:

using System;

public struct Point
{
    public double Longitude; // 经度
    public double Latitude; // 纬度
}

public static class EarthCalculator
{
    private const double EarthRadius = 6371; // 地球半径,单位:千米

    // 计算两个点之间的距离,单位:千米
    public static double Distance(Point p1, Point p2)
    {
        double dLat = (p2.Latitude - p1.Latitude) * Math.PI / 180;
        double dLon = (p2.Longitude - p1.Longitude) * Math.PI / 180;
        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                   Math.Cos(p1.Latitude * Math.PI / 180) * Math.Cos(p2.Latitude * Math.PI / 180) *
                   Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        return EarthRadius * c;
    }

    // 计算所围成的面积,单位:平方千米
    public static double Area(Point[] points)
    {
        double area = 0;
        int count = points.Length;
        for (int i = 0; i < count; i++)
        {
            Point p1 = points[i];
            Point p2 = points[(i + 1) % count];
            area += (p2.Longitude - p1.Longitude) * (2 + Math.Sin(p1.Latitude * Math.PI / 180) +
                                                     Math.Sin(p2.Latitude * Math.PI / 180));
        }
        area = area * EarthRadius * EarthRadius / 2;
        return Math.Abs(area);
    }
}

// 示例用法
Point[] points = new Point[]
{
    new Point { Longitude = 116.3975, Latitude = 39.9082 },
    new Point { Longitude = 116.4074, Latitude = 39.9042 },
    new Point { Longitude = 116.4125, Latitude = 39.8956 },
    new Point { Longitude = 116.4026, Latitude = 39.8915 }
};
double distance = EarthCalculator.Distance(points[0], points[1]);
double area = EarthCalculator.Area(points);
Console.WriteLine($"两点之间的距离为:{distance}千米");
Console.WriteLine($"所围成的面积为:{area}平方千米");

--相关问题--:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值