CBusLine.cs
上传用户:xum6868
上传日期:2009-12-02
资源大小:102k
文件大小:4k
源码类别:

GIS编程

开发平台:

C#

  1. using System;
  2. namespace MainSystem
  3. {
  4. public class CGisSegLine
  5. {
  6. public MPoint m_ptStartPoint ;
  7. public MPoint m_ptEndPoint ;
  8. public CGisSegLine()
  9. {
  10. m_ptStartPoint = new MPoint() ;
  11. m_ptEndPoint = new MPoint();
  12. }
  13. public int GetDistance(ref MPoint point,ref MPoint ptHFoot,ref double distance )
  14. {
  15. double Px,Py,Ax,Ay,Bx,By;
  16. const double ZERODIST=0.00000001;
  17. double AB2,PA2,PB2,AB,PA,PB,S,AREA;
  18. double med,k1,k2,b1,b2;
  19. Px=point.x;
  20. Py=point.y;
  21. Ax=m_ptStartPoint.x;
  22. Ay=m_ptStartPoint.y;
  23. Bx=m_ptEndPoint.x;
  24. By=m_ptEndPoint.y;
  25. AB2=(Ax-Bx)*(Ax-Bx)+(Ay-By)*(Ay-By);
  26. PB2=(Px-Bx)*(Px-Bx)+(Py-By)*(Py-By);
  27. PA2=(Ax-Px)*(Ax-Px)+(Ay-Py)*(Ay-Py);
  28. if(AB2<ZERODIST)
  29. {
  30. med=System.Math.Sqrt(PA2);
  31. distance=med;
  32. ptHFoot.x=Ax;
  33. ptHFoot.y=Ay;
  34. return  -1 ;
  35. }
  36. if(PA2<ZERODIST)
  37. {
  38. med=System.Math.Sqrt(PA2);
  39. distance=med;
  40. ptHFoot.x=Ax;
  41. ptHFoot.y=Ay;
  42. return  -2 ;
  43. }
  44. if(PB2<ZERODIST)
  45. {
  46. med=System.Math.Sqrt(PB2);
  47. distance=med;
  48. ptHFoot.x=Bx;
  49. ptHFoot.y=By;
  50. return  -2 ;
  51. }
  52. if(PA2+AB2<PB2||AB2+PB2<PA2)
  53. {                       
  54. if(PA2>PB2)
  55. {
  56. med=PB2;
  57. ptHFoot.x=Bx;
  58. ptHFoot.y=By;
  59. }
  60. else
  61. {
  62. med=PA2;
  63. ptHFoot.x=Ax;
  64. ptHFoot.y=Ay;
  65. }
  66. med=System.Math.Sqrt(med);
  67. distance=med;
  68. return  -3 ;
  69. }
  70. else
  71. {
  72. AB=System.Math.Sqrt(AB2);
  73. PA=System.Math.Sqrt(PA2);
  74. PB=System.Math.Sqrt(PB2);
  75. S=(AB+PA+PB)/2.0;
  76. AREA=S;
  77. AREA*=(S-PA);
  78. AREA*=(S-PB);
  79. AREA*=(S-AB);
  80. AREA=System.Math.Sqrt(AREA);
  81. med=(2.0*AREA)/AB;
  82. distance=med;
  83. med=Ay-By;
  84. if(med==0.0)
  85. {
  86. ptHFoot.x=Px;
  87. ptHFoot.y=Ay;
  88. return  -4 ;
  89. }
  90. med=Ax-Bx;
  91. if(med==0.0)
  92. {
  93. ptHFoot.y=Py;
  94. ptHFoot.x=Ax;
  95. return  -4 ;
  96. }
  97. k1=(Ay-By)/(Ax-Bx);
  98. k2=-1.0/k1;
  99. b1=Ay-k1*Ax;
  100. b2=Py-k2*Px;
  101. S=(b2-b1)/(k1-k2);
  102. ptHFoot.x=S;
  103. S=k1*S+b1;
  104. ptHFoot.y=S;
  105. return 0;
  106. }
  107. }
  108. };
  109. /// <summary>
  110. /// Summary description for CBusLine.
  111. /// </summary>
  112. public class CBusLine
  113. {
  114. public CBusLine()
  115. {
  116. //
  117. // TODO: Add constructor logic here
  118. //
  119. }
  120. public void CutLine(MLine LineSrc,MPoint pt1,MPoint pt2,ref MLine LineRes)
  121. {
  122. //计算到线段的距离
  123. double dMinDistance1 = 10000.0;
  124. double dMinDistance2 = 10000.0;
  125. double dTheDis = 0.0;
  126. MPoint ptRealFrom,ptRealTo;
  127. MPoint ptTemp = new MPoint();
  128. int nPointOrderInMLine1=0,nPointOrderInMLine2=0;
  129. CGisSegLine SegMLine = new CGisSegLine();
  130. for(int i=0;i<LineSrc.nPointNumber-1;i++)
  131. {
  132. SegMLine.m_ptStartPoint.x = LineSrc.pPoint[i].x;
  133. SegMLine.m_ptStartPoint.y = LineSrc.pPoint[i].y;
  134. SegMLine.m_ptStartPoint.x = LineSrc.pPoint[i+1].x;
  135. SegMLine.m_ptStartPoint.y = LineSrc.pPoint[i+1].y;
  136. SegMLine.GetDistance(ref pt1,ref ptTemp,ref dTheDis);
  137. if(dTheDis<dMinDistance1)
  138. {
  139. dMinDistance1 = dTheDis;
  140. nPointOrderInMLine1 =i;
  141. ptRealFrom = ptTemp;
  142. }
  143. SegMLine.GetDistance(ref pt2,ref ptTemp,ref dTheDis);
  144. if(dTheDis<dMinDistance2)
  145. {
  146. dMinDistance2 = dTheDis;
  147. nPointOrderInMLine2 = i;
  148. ptRealTo = ptTemp;
  149. }
  150. }
  151. if(nPointOrderInMLine2 - nPointOrderInMLine1 >0 )
  152. {
  153. LineRes.nPointNumber = nPointOrderInMLine2 - nPointOrderInMLine1 +2;
  154. LineRes.pPoint = new MPoint[LineRes.nPointNumber];
  155. int i;
  156. LineRes.pPoint[0] = pt1;
  157. for(i=1;i<LineRes.nPointNumber-1;i++)
  158. {
  159. LineRes.pPoint[i] = LineSrc.pPoint[nPointOrderInMLine1+i];
  160. }
  161. LineRes.pPoint[i] = pt2;
  162. }
  163. else
  164. {
  165. LineRes.nPointNumber = nPointOrderInMLine1 - nPointOrderInMLine2 +2;
  166. LineRes.pPoint = new MPoint[LineRes.nPointNumber];
  167. int i;
  168. LineRes.pPoint[0] = pt2;
  169. for(i=1;i<LineRes.nPointNumber-1;i++)
  170. {
  171. LineRes.pPoint[i] = LineSrc.pPoint[nPointOrderInMLine2+i];
  172. }
  173. LineRes.pPoint[i] = pt1;
  174. }
  175. }
  176. }
  177. }