两点计算角度
http://www.cnblogs.com/aoldman/archive/2013/07/04/3171559.html
/// <summary>
/// 两点计算角度
/// </summary>
/// <param name="startx"></param>
/// <param name="starty"></param>
/// <param name="endx"></param>
/// <param name="endy"></param>
/// <returns></returns>
public static double CalulateXYAnagle(double startx, double starty, double endx, double endy)
{
//除数不能为0
double tan = Math.Atan(Math.Abs((endy - starty) / (endx - startx))) * 180 / Math.PI;
if (endx > startx && endy > starty)//第一象限
{
return -tan;
}
else if (endx > startx && endy < starty)//第二象限
{
return tan;
}
else if (endx < startx && endy > starty)//第三象限
{
return tan - 180;
}
else
{
return 180 - tan;
}
}
/// <summary> /// 计算旋转角度 /// </summary> /// <param name="nowpoint"></param> /// <returns></returns> public double ComputeAngle(Point nowpoint) { //斜边长度 double length = PointLegth(nowpoint, CentPoint); //对边比斜边 sin double hudu = Math.Asin(Math.Abs(nowpoint.Y - CentPoint.Y)/length); double ag = hudu*180/Math.PI; //第一象限90- if ((CentPoint.X - nowpoint.X) <= 0 && (CentPoint.Y - nowpoint.Y) >= 0) ag = 90 - ag; //第二象限90+ else if ((CentPoint.X - nowpoint.X) <= 0 && (CentPoint.Y - nowpoint.Y) <= 0) ag = ag + 90; //第三象限270- else if ((CentPoint.X - nowpoint.X) >= 0 && (CentPoint.Y - nowpoint.Y) <= 0) ag = 270 - ag; //第四象限270+ else if ((CentPoint.X - nowpoint.X) >= 0 && (CentPoint.Y - nowpoint.Y) >= 0) ag = ag + 270; //偏移 ag -= 235; return ag; } /// <summary> /// 计算两点间距离 /// </summary> /// <param name="pa"></param> /// <param name="pb"></param> /// <returns></returns> public double PointLegth(Point pa, Point pb) { return Math.Sqrt(Math.Pow((pa.X - pb.X), 2) + Math.Pow((pa.Y - pb.Y), 2)); } |