根据两点坐标,计算距离和方位角。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Math;
namespace Geo
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入A点的X坐标: ");
double Xa = double.Parse(Console.ReadLine());
Console.Write("请输入A点的Y坐标: ");
double Ya = double.Parse(Console.ReadLine());
Console.Write("请输入B点的X坐标: ");
double Xb = double.Parse(Console.ReadLine());
Console.Write("请输入B点的Y坐标: ");
double Yb = double.Parse(Console.ReadLine());
double deltaX = Xb - Xa;
double deltaY = Yb - Ya;
Console.WriteLine("两点距离 = {0}", GeoCal.distance(deltaX, deltaY));
Console.WriteLine("B点相对于A点的方位角 = {0}", GeoCal.direction(deltaX, deltaY));
}
public class GeoCal
{
public static double distance(double deltaX, double deltaY)
{
return Math.Sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
}
public static double direction(double deltaX, double deltaY)
{
//判断象限
if(deltaX>0&&deltaY>=0)
{//第一象限【不变】
return Math.Atan(deltaY/deltaX)*180/Math.PI;//弧度转角度
}
else if(deltaX<0&&deltaY>=0)
{//第二象限【+180】
return Math.Atan(deltaY/deltaX)*180/Math.PI + 180;
}
else if(deltaX<0&&deltaY<=0)
{//第三象限【+180】
return Math.Atan(deltaY/deltaX)*180/Math.PI + 180;
}
else if(deltaX>0&&deltaY<=0)
{//第四象限【+360】
return Math.Atan(deltaY/deltaX)*180/Math.PI + 360;
}
else if(deltaX=0&&deltaY>0)
{//90°
return 90;
}
else if(deltaX=0&&deltaY<0)
{//270°
return 270;
}
else
{//同点
Console.WriteLine("点A和点B是同一个点,无相对方位角。");
return -1;
}
}
}
}
}