最近开始重构不顺眼的辣鸡代码,顺带将某个模块的一个算法辅助类贴到这里。
/// <summary> /// 算法逻辑辅助类 /// </summary> internal static class AlgorithmHelper { /// <summary> /// “两点距离公式”求已知两点的距离 /// </summary> /// <param name="point_start"></param> /// <param name="point_end"></param> /// <returns></returns> internal static double GetDistance(IPoint point_start, IPoint point_end) { double x = point_end.X - point_start.X; double y = point_end.Y - point_start.Y; double distance = Math.Sqrt(x * x + y * y); return distance; } /// <summary> /// “余弦定理”求已知三点组成的夹角的角度。 /// </summary> /// <param name="first"></param> /// <param name="cen"></param> /// <param name="last"></param> /// <returns></returns> internal static double GetAngle(IPoint first, IPoint cen, IPoint last) { double ma_x = first.X - cen.X; double ma_y = first.Y - cen.Y; double mb_x = last.X - cen.X; double mb_y = last.Y - cen.Y; double ab_x = first.X - last.X; double ab_y = first.Y - last.Y; double ab_val2 = ab_x * ab_x + ab_y * ab_y; double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y); double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y); double cosM = (ma_val * ma_val + mb_val * mb_val - ab_val2) / (2 * ma_val * mb_val); double angleAMB = Math.Acos(cosM) / System.Math.PI * 180; return angleAMB; } /// <summary> /// 延长线段 /// </summary> /// <param name="passLine">传入去的线</param> /// <param name="mode">模式,1为从FromPoint处延长,2为从ToPint处延长,3为两端延长</param> /// <param name="dis">延长的距离</param> /// <returns></returns> internal static IPolyline GetExtendLine(IPolyline passLine, int mode, double dis) { IPointCollection pPointCol = passLine as IPointCollection; switch (mode) { case 1: IPoint fromPoint = new PointClass(); passLine.QueryPoint(esriSegmentExtension.esriExtendAtFrom, -1 * dis, false, fromPoint); pPointCol.InsertPoints(0, 1, ref fromPoint); break; case 2: IPoint endPoint = new PointClass(); object missing = Type.Missing; passLine.QueryPoint(esriSegmentExtension.esriExtendAtTo, dis + passLine.Length, false, endPoint); pPointCol.AddPoint(endPoint, ref missing, ref missing); break; case 3: IPoint fPoint = new PointClass(); IPoint ePoint = new PointClass(); object missing2 = Type.Missing; passLine.QueryPoint(esriSegmentExtension.esriExtendAtFrom, -1 * dis, false, fPoint); pPointCol.InsertPoints(0, 1, ref fPoint); passLine.QueryPoint(esriSegmentExtension.esriExtendAtTo, dis + passLine.Length, false, ePoint); pPointCol.AddPoint(ePoint, ref missing2, ref missing2); break; default: return pPointCol as IPolyline; } return pPointCol as IPolyline; } /// <summary> /// xy坐标转地理坐标 /// </summary> /// <param name="pActiveView"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> internal static IPoint XYToGeo(IActiveView pActiveView, double x, double y) { try { IMap pMap = pActiveView.FocusMap; IPoint pt = new PointClass(); ISpatialReferenceFactory pfactory = new SpatialReferenceEnvironmentClass(); ISpatialReference flatref = pMap.SpatialReference; ISpatialReference earthref = pfactory.CreateGeographicCoordinateSystem(4490); pt.PutCoords(x, y); IGeometry geo = (IGeometry)pt; geo.SpatialReference = flatref; geo.Project(earthref); return pt; } catch (Exception ex) { throw new Exception(ex.Message + "\r\n" + ex.StackTrace); } } /// <summary> /// 数字经纬度和度分秒经纬度转换 /// </summary> /// <param name="digitalDegree">十进制度</param> /// <return>度分秒经纬度</return> internal static string DigitalToDegrees(double digitalDegree) { const double num = 60; int degree = (int)digitalDegree; double tmp = (digitalDegree - degree) * num; int minute = (int)tmp; double second = (tmp - minute) * num; string degrees = "" + degree + "°" + minute + "′" + Math.Floor(second) + "″"; return degrees; } }