根据余弦定理求两个线段夹角 c#
时间:2011-05-19 来源:兰陵再世
支持原创!
/// <summary>
/// 根据余弦定理求两个线段夹角
/// </summary>
/// <param name="o">端点</param>
/// <param name="s">start点</param>
/// <param name="e">end点</param>
/// <returns></returns>
double Angle(PointF o, PointF s, PointF e)
{
double cosfi = 0, fi = 0, norm = 0;
double dsx = s.X - o.X;
double dsy = s.Y - o.Y;
double dex = e.X - o.X;
double dey = e.Y - o.Y;
cosfi = dsx * dex + dsy * dey;
norm = (dsx * dsx + dsy * dsy) * (dex * dex + dey * dey);
cosfi /= Math.Sqrt(norm);
if (cosfi >= 1.0) return 0;
if (cosfi <= -1.0) return Math.PI;
fi = Math.Acos(cosfi);
if (180 * fi / Math.PI < 180)
{
return 180 * fi / Math.PI;
}
else
{
return 360 - 180 * fi / Math.PI;
}
}