线段导航

 //求直线与直线的交点 bool CCQGrapView::GetLineIntersect(CPoint ptL1,CPoint ptL2,CPoint ptOL1,CPoint ptOL2,CPoint& rPoint) { double a1=ptL2.y-ptL1.y; double b1=ptL1.x-ptL2.x; double c1=ptL1.x*ptL2.y-ptL2.x*ptL1.y; double a2=ptOL2.y-ptOL1.y; double b2=ptOL1.x-ptOL2.x; double c2=ptOL1.x*ptOL2.y-ptOL2.x*ptOL1.y; double detab=a1*b2-a2*b1; if(detab==0) { double r=0.0; if(a2!=0) r=a1/a2; else r=b1/b2; if(c1==0&&c2==0) return false; if(r==c1/c2) return false; else return false; } rPoint.x=(c1*b2-c2*b1)/detab; rPoint.y=(a1*c2-a2*c1)/detab; int nD = Distance(ptL1,ptL2); int nD1 = Distance(ptL1,rPoint); int nD2 = Distance(ptL2,rPoint); int nR = Distance(ptOL1,ptOL2); int nR1 = Distance(ptOL1,rPoint); int nR2 = Distance(ptOL2,rPoint); if (abs(nD - (nD1+nD2)) <= 1 && abs(nR - (nR1+nR2)) <= 1 ) { return true; } return false; } // 两点间距离公式 int CCQGrapView::Distance(CPoint nptPosB, CPoint nptPosE) { int nD = sqrt( pow(static_cast<double>(nptPosB.x - nptPosE.x),2) + pow(static_cast<double>(nptPosB.y - nptPosE.y),2) ); return nD; } //功能:pt1外一点 // pt2垂足 // nLine距离长度 // nLR判断是左延长点和右延长点 // 返回点坐标 BOOL CCQGrapView::CreateProlong(CPoint pt1,CPoint pt2,int nLine,UINT nLR,CPoint &ptOut) { CPoint ptPos = GetPoint(pt2,pt1,nLine); double dbAng; if(nLR == TRUE) { dbAng = 90.000; } else { dbAng = 270.000; } ptOut = CircumgyratePoint(dbAng,pt2,ptPos); return TRUE; } //求圆心,半径和任意一点,求在圆上一点 //nR 半径距离 CPoint CCQGrapView::GetPoint(CPoint ptC, CPoint ptE, int nR) { int nD = Distance(ptC, ptE); CPoint pt; pt.x = (int)((ptE.x - ptC.x) * nR / nD + ptC.x); pt.y = (int)((ptE.y - ptC.y) * nR / nD + ptC.y); return pt; } //求1点绕另1点旋转某1角度后的新坐标 CPoint CCQGrapView::CircumgyratePoint(double dbAngle, CPoint ptCenter, CPoint ptAim) { CPoint Center; double dbRadian=dbAngle * 3.14159 / 180; Center.x = (ptAim.x - ptCenter.x) * cos(dbRadian) - (ptAim.y - ptCenter.y) * sin(dbRadian) + ptCenter.x; Center.y = (ptAim.x - ptCenter.x) * sin(dbRadian) + (ptAim.y - ptCenter.y) * cos(dbRadian) + ptCenter.y; return Center; } //三点为一条直线(用斜率解方程需解决分母为0的问题) //CPoint 直线的两端点 pt1,pt2 //CPoint 判断pt3点是否在该直线上 BOOL CCQGrapView::ThreeptOnLine(CPoint pt1,CPoint pt2,CPoint pt3) { int nK1 = (pt3.y - pt2.y); int nK12 = (pt3.x - pt2.x); nK1==0?++nK1:nK1; nK12==0?++nK12:nK12; nK1 = nK1 / nK12; int nK2 = (pt2.y - pt1.y); int nK22 = (pt2.x - pt1.x); nK2==0?++nK2:nK2; nK22==0?++nK22:nK22; nK2 = nK2/nK22; int nLine = Distance(pt1,pt2); int nptL1 = Distance(pt1,pt3); int nptL2 = Distance(pt2,pt3); if (abs(nK1 - nK2) <=1 && (nLine - abs(nptL1+nptL2)) >= 0) { return TRUE; } return FALSE; }

你可能感兴趣的:(线段导航)