中学的时候醉心于研究怎么“逃课”,大学的时候豁然开悟——最牛逼的逃课是准时准地儿去上每一课,却不知到老师讲的啥,“大隐隐于市”大概就是这境界吧。
用到才听说有“余弦定理”这么一个东西,遂感叹“白上了大学”。
又百度了一下,高中数学……
检查角度先要根据已知的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 }