如何判断一点在另一个点的方位

发布时间 2023-08-24 10:27:25作者: 大王派我来巡山~

 

/// <summary>
/// 判断点2在点1的哪个方向 1:东 ,2:北, 3:西 4、南
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="nRegion"></param>
/// <returns></returns>
public static int GetDirect( XYZ p1, XYZ p2)
{
float fDis = (float)Math.Sqrt((double)((p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y)));
if (fDis < 0.001)
{
return -1;//重合
}

// 将p2转换为以p1为坐标中心的坐标系中
double p2X = p2.X;
double p2Y = p2.Y;

p2X -= p1.X;
p2Y -= p1.Y;

if (Math.Abs(p2X) > Math.Abs(p2Y) && p2X > 0)
{
return 1;
}
else if (Math.Abs(p2X) > Math.Abs(p2Y) && p2X < 0)
{
return 3;
}
else if (Math.Abs(p2X) < Math.Abs(p2Y) && p2Y < 0)
{
return 2;
}
else if (Math.Abs(p2X) < Math.Abs(p2Y) && p2Y > 0)
{
return 4;
}
return -1;
}