获得平行四边形中,那条较长的中心线

我会带着你远行 2022-08-07 01:33 267阅读 0赞
  1. /// <summary>
  2. /// 创建平行四边形中,较长的一条中心线
  3. /// </summary>
  4. public IGeometry CreateLine(IPolygon pPolygon)
  5. {
  6. if (pPolygon == null)
  7. {
  8. return null;
  9. }
  10. try
  11. {
  12. 首先判断pPolygon是否为平行四边形
  13. if (!IsParallelogram(pPolygon))
  14. {
  15. return null;
  16. }
  17. IPointCollection pPointCol = pPolygon as IPointCollection;
  18. IPoint pFstPt = null;
  19. IPoint pSecPt = null;
  20. IPoint pTrdPt = null;
  21. IPoint pForPt = null;
  22. 这些点事按照绘制顺序来存储的
  23. pFstPt = pPointCol.get_Point(0);
  24. pSecPt = pPointCol.get_Point(1);
  25. pTrdPt = pPointCol.get_Point(2);
  26. pForPt = pPointCol.get_Point(3);
  27. IPoint pFst = new PointClass();
  28. IPoint pSec = new PointClass();
  29. IPoint pTrd = new PointClass();
  30. IPoint pFor = new PointClass();
  31. 获得线段中点
  32. pFst.PutCoords((pFstPt.X + pSecPt.X) / 2, (pFstPt.Y + pSecPt.Y) / 2);
  33. pSec.PutCoords((pSecPt.X + pTrdPt.X) / 2, (pSecPt.Y + pTrdPt.Y) / 2);
  34. pTrd.PutCoords((pTrdPt.X + pForPt.X) / 2, (pTrdPt.Y + pForPt.Y) / 2);
  35. pFor.PutCoords((pForPt.X + pFstPt.X) / 2, (pForPt.Y + pFstPt.Y) / 2);
  36. object oBefore = Type.Missing;
  37. object oAfter = Type.Missing;
  38. ILine pLineLR = new LineClass();
  39. pLineLR.PutCoords(pFst, pTrd);
  40. ILine pLineUL = new LineClass();
  41. pLineUL.PutCoords(pSec, pFor);
  42. IPolyline pPolylineLR = new PolylineClass();
  43. IPolyline pPolylineUL = new PolylineClass();
  44. 转成IPolyline
  45. (pPolylineLR as ISegmentCollection).AddSegment(pLineLR as ISegment, ref oBefore, ref oAfter);
  46. (pPolylineUL as ISegmentCollection).AddSegment(pLineUL as ISegment, ref oBefore, ref oAfter);
  47. (pPolylineLR as ITopologicalOperator).Simplify();
  48. (pPolylineUL as ITopologicalOperator).Simplify();
  49. IGeometry pGeoLine;
  50. 得到较长的一条中心线
  51. if (pPolylineLR.Length > pPolylineUL.Length)
  52. {
  53. pGeoLine = pPolylineLR as IGeometry;
  54. }
  55. else
  56. {
  57. pGeoLine = pPolylineUL as IGeometry;
  58. }
  59. return pGeoLine;
  60. }
  61. catch (Exception ex)
  62. {
  63. return null;
  64. }
  65. }

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读