中學的時候醉心於研究怎么“逃課”,大學的時候豁然開悟——最牛逼的逃課是准時准地兒去上每一課,卻不知到老師講的啥,“大隱隱於市”大概就是這境界吧。
用到才聽說有“余弦定理”這么一個東西,遂感嘆“白上了大學”。
又百度了一下,高中數學……

檢查角度先要根據已知的3點計算出其所組成的夾角的角度,這就需要“余弦定理”了。
![]()
其代碼實現應該是下面的樣子:
1 private static double GetAngle(IPoint first, IPoint cen, IPoint last) 2 { 3 double ma_x = first.X - cen.X; 4 double ma_y = first.Y - cen.Y; 5 double mb_x = last.X - cen.X; 6 double mb_y = last.Y - cen.Y; 7 double ab_x = first.X - last.X; 8 double ab_y = first.Y - last.Y; 9 double ab_val2 = ab_x * ab_x + ab_y * ab_y; 10 double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y); 11 double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y); 12 double cosM = (ma_val * ma_val + mb_val * mb_val - ab_val2) / (2 * ma_val * mb_val); 13 double angleAMB = Math.Acos(cosM) / System.Math.PI * 180; 14 return angleAMB; 15 }
在檢查方法CheckAcuteAngle中調用GetAngle方法獲取角度值,CheckAcuteAngle方法傳入IFeatureClass類型的要素類(應該是一個面層的)與給定的double型角度上限值。返回值是有錯的要素List與其對應的角度值List。
1 public static List<int> CheckAcuteAngle(IFeatureClass pFeatureClass, double acuteAngle, out List<double> listAngle) 2 { 3 IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)m_hookHelper.FocusMap.ActiveGraphicsLayer; 4 pGraphicsContainer.DeleteAllElements(); 5 IColor color = DisplayUtils.RGBColor(255, 0, 0); 6 List<int> listError = new List<int>(); 7 List<double> listOutAngle = new List<double>(); 8 IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true); 9 IFeature pFeature = pFeatureCursor.NextFeature(); 10 try 11 { 12 if (pFeature != null) 13 { 14 IPointCollection Temp_Vertices; 15 IPoint First_Point = new Point(); 16 IPoint Center_Point = new Point(); 17 IPoint Last_Point = new Point(); 18 while (pFeature != null) 19 { 20 IGeometryCollection pGeometryCollection=pFeature.Shape as IGeometryCollection; 21 int count = pGeometryCollection.GeometryCount; 22 for (int j = 0; j < count; j++) 23 { 24 IGeometry pGeometry = pGeometryCollection.get_Geometry(j); 25 Temp_Vertices = pGeometry as IPointCollection; 26 Temp_Vertices.AddPoint(Temp_Vertices.get_Point(1)); 27 for (int i = 0; i < Temp_Vertices.PointCount - 2; i++) 28 { 29 Temp_Vertices.QueryPoint(i, First_Point); 30 Temp_Vertices.QueryPoint(i + 1, Center_Point); 31 Temp_Vertices.QueryPoint(i + 2, Last_Point); 32 double angle = GetAngle(First_Point, Center_Point, Last_Point); 33 if (angle <= acuteAngle || angle >= 360 - acuteAngle) 34 { 35 listError.Add(pFeature.OID); 36 listOutAngle.Add(angle); 37 38 IPointArray pointArray = new PointArrayClass(); 39 pointArray.Add(First_Point); 40 pointArray.Add(Center_Point); 41 pointArray.Add(Last_Point); 42 43 IElement pElement = MarkElementUtils.PointArray2LineMarkElement(pointArray, 1.0, color); 44 pGraphicsContainer.AddElement(pElement, 0); 45 } 46 } 47 } 48 pFeature = pFeatureCursor.NextFeature(); 49 } 50 } 51 } 52 catch (Exception) 53 { 54 throw new Exception("執行角度計算時發生錯誤,錯誤FeatureID為:"+pFeature.OID); 55 } 56 finally 57 { 58 Marshal.FinalReleaseComObject(pFeatureCursor); 59 } 60 listAngle = listOutAngle; 61 return listError; 62 }
