CBusLine.cs
上传用户:xum6868
上传日期:2009-12-02
资源大小:102k
文件大小:4k
- using System;
- namespace MainSystem
- {
- public class CGisSegLine
- {
- public MPoint m_ptStartPoint ;
- public MPoint m_ptEndPoint ;
- public CGisSegLine()
- {
- m_ptStartPoint = new MPoint() ;
- m_ptEndPoint = new MPoint();
- }
- public int GetDistance(ref MPoint point,ref MPoint ptHFoot,ref double distance )
- {
- double Px,Py,Ax,Ay,Bx,By;
- const double ZERODIST=0.00000001;
- double AB2,PA2,PB2,AB,PA,PB,S,AREA;
- double med,k1,k2,b1,b2;
- Px=point.x;
- Py=point.y;
- Ax=m_ptStartPoint.x;
- Ay=m_ptStartPoint.y;
- Bx=m_ptEndPoint.x;
- By=m_ptEndPoint.y;
- AB2=(Ax-Bx)*(Ax-Bx)+(Ay-By)*(Ay-By);
- PB2=(Px-Bx)*(Px-Bx)+(Py-By)*(Py-By);
- PA2=(Ax-Px)*(Ax-Px)+(Ay-Py)*(Ay-Py);
- if(AB2<ZERODIST)
- {
- med=System.Math.Sqrt(PA2);
- distance=med;
- ptHFoot.x=Ax;
- ptHFoot.y=Ay;
- return -1 ;
- }
- if(PA2<ZERODIST)
- {
- med=System.Math.Sqrt(PA2);
- distance=med;
- ptHFoot.x=Ax;
- ptHFoot.y=Ay;
- return -2 ;
- }
- if(PB2<ZERODIST)
- {
- med=System.Math.Sqrt(PB2);
- distance=med;
- ptHFoot.x=Bx;
- ptHFoot.y=By;
- return -2 ;
- }
- if(PA2+AB2<PB2||AB2+PB2<PA2)
- {
- if(PA2>PB2)
- {
- med=PB2;
- ptHFoot.x=Bx;
- ptHFoot.y=By;
- }
- else
- {
- med=PA2;
- ptHFoot.x=Ax;
- ptHFoot.y=Ay;
- }
- med=System.Math.Sqrt(med);
- distance=med;
- return -3 ;
- }
- else
- {
- AB=System.Math.Sqrt(AB2);
- PA=System.Math.Sqrt(PA2);
- PB=System.Math.Sqrt(PB2);
- S=(AB+PA+PB)/2.0;
- AREA=S;
- AREA*=(S-PA);
- AREA*=(S-PB);
- AREA*=(S-AB);
- AREA=System.Math.Sqrt(AREA);
- med=(2.0*AREA)/AB;
- distance=med;
-
- med=Ay-By;
- if(med==0.0)
- {
- ptHFoot.x=Px;
- ptHFoot.y=Ay;
- return -4 ;
- }
- med=Ax-Bx;
- if(med==0.0)
- {
- ptHFoot.y=Py;
- ptHFoot.x=Ax;
- return -4 ;
- }
- k1=(Ay-By)/(Ax-Bx);
- k2=-1.0/k1;
- b1=Ay-k1*Ax;
- b2=Py-k2*Px;
- S=(b2-b1)/(k1-k2);
- ptHFoot.x=S;
- S=k1*S+b1;
- ptHFoot.y=S;
- return 0;
- }
- }
- };
- /// <summary>
- /// Summary description for CBusLine.
- /// </summary>
- public class CBusLine
- {
- public CBusLine()
- {
- //
- // TODO: Add constructor logic here
- //
- }
- public void CutLine(MLine LineSrc,MPoint pt1,MPoint pt2,ref MLine LineRes)
- {
- //计算到线段的距离
- double dMinDistance1 = 10000.0;
- double dMinDistance2 = 10000.0;
- double dTheDis = 0.0;
- MPoint ptRealFrom,ptRealTo;
- MPoint ptTemp = new MPoint();
- int nPointOrderInMLine1=0,nPointOrderInMLine2=0;
- CGisSegLine SegMLine = new CGisSegLine();
- for(int i=0;i<LineSrc.nPointNumber-1;i++)
- {
- SegMLine.m_ptStartPoint.x = LineSrc.pPoint[i].x;
- SegMLine.m_ptStartPoint.y = LineSrc.pPoint[i].y;
- SegMLine.m_ptStartPoint.x = LineSrc.pPoint[i+1].x;
- SegMLine.m_ptStartPoint.y = LineSrc.pPoint[i+1].y;
-
- SegMLine.GetDistance(ref pt1,ref ptTemp,ref dTheDis);
- if(dTheDis<dMinDistance1)
- {
- dMinDistance1 = dTheDis;
- nPointOrderInMLine1 =i;
- ptRealFrom = ptTemp;
- }
- SegMLine.GetDistance(ref pt2,ref ptTemp,ref dTheDis);
- if(dTheDis<dMinDistance2)
- {
- dMinDistance2 = dTheDis;
- nPointOrderInMLine2 = i;
- ptRealTo = ptTemp;
- }
- }
- if(nPointOrderInMLine2 - nPointOrderInMLine1 >0 )
- {
- LineRes.nPointNumber = nPointOrderInMLine2 - nPointOrderInMLine1 +2;
- LineRes.pPoint = new MPoint[LineRes.nPointNumber];
- int i;
- LineRes.pPoint[0] = pt1;
- for(i=1;i<LineRes.nPointNumber-1;i++)
- {
- LineRes.pPoint[i] = LineSrc.pPoint[nPointOrderInMLine1+i];
- }
- LineRes.pPoint[i] = pt2;
- }
- else
- {
- LineRes.nPointNumber = nPointOrderInMLine1 - nPointOrderInMLine2 +2;
- LineRes.pPoint = new MPoint[LineRes.nPointNumber];
- int i;
- LineRes.pPoint[0] = pt2;
- for(i=1;i<LineRes.nPointNumber-1;i++)
- {
- LineRes.pPoint[i] = LineSrc.pPoint[nPointOrderInMLine2+i];
- }
- LineRes.pPoint[i] = pt1;
- }
- }
- }
- }