/// /// 根据余弦定理求两个线段夹角 /// /// 端点 /// 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; }
时间: 2024-10-10 20:18:28