zoukankan      html  css  js  c++  java
  • 线段余弦角+凸包算法

            ///
            /// 根据余弦定理求两个线段夹角
            ///
            /// 端点
            /// start点
            /// end点
            ///
            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;
                }
            }
    
            /// <summary>
            /// 凸包算法
            /// </summary>
            /// <param name="_list"></param>
            /// <returns></returns>
            private List<TuLine> BruteForceTu(List<Info> _list)
            {
    
                //记录极点对
                List<TuLine> role = new List<TuLine>();
    
                //遍历
                for (int i = 0; i < _list.Count - 1; i++)
                {
    
                    for (int j = i + 1; j < _list.Count; j++)
                    {
    
                        double a = _list[j].Y - _list[i].Y;
                        double b = _list[i].X - _list[j].X;
                        double c = _list[i].X * _list[j].Y - _list[i].Y * _list[j].X;
    
                        int count = 0;
                        //将所有点代入方程
                        for (int k = 0; k < _list.Count; k++)
                        {
    
                            double result = a * _list[k].X + b * _list[k].Y - c;
                            if (result > 0)
                            {
    
                                count++;
    
                            }
                            else if (result < 0)
                            {
    
                                count--;
    
                            }
    
                        }
                        //是极点,则将连线记录下来
                        if (Math.Abs(count) == _list.Count - 2)
                        {
    
                            TuLine line = new TuLine();
                            IPoint pi = new PointClass();
                            pi.X = _list[i].X;
                            pi.Y = _list[i].Y;
                            line.Begin = pi;
    
                            IPoint Pj = new PointClass();
                            Pj.X = _list[j].X;
                            Pj.Y = _list[j].Y;
                            line.End = Pj;
                            role.Add(line);
                        }
    
                    }
    
                }
                return role;
            }
    

      

  • 相关阅读:
    pandas Dataframe filter
    process xlsx with pandas
    data manipulate in excel with easyExcel class
    modify registry in user environment
    add number line in vim
    java import webservice
    ctypes MessageBoxA
    music 163 lyrics
    【python实例】自动贩卖机
    【python基础】sys模块(库)方法汇总
  • 原文地址:https://www.cnblogs.com/smilepeter/p/5563250.html
Copyright © 2011-2022 走看看