根据余弦定理求两个线段夹角


  /// <summary>
        /// 根据余弦定理求两个线段夹角
        /// </summary>
        /// <param name="o"></param>
        /// <param name="s"></param>
        /// <param name="e"></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;
             }

         }
    

你可能感兴趣的:(360)