BOOL PointInPolygon(const CBCGPPointsArray& arPoly, const CBCGPPoint& pt) { int nSize = (int)arPoly.GetSize (); if (nSize < 3) { return FALSE; } const CBCGPPoint* pData = arPoly.GetData(); int count = 0; int current = 0; int next = 1; while(next < nSize) { BOOL b = (pData[next].y <= pt.y); if (pData[current].y <= pt.y) { b = !b; } if (b) { if(((pData[next].x - pData[current].x) * (pt.y - pData[current].y) / (pData[next].y - pData[current].y)) <= (pt.x - pData[current].x)) { count++; } } current++; next++; } if (pData[0] != pData[nSize - 1]) { current = nSize - 1; next = 0; BOOL b = (pData[next].y <= pt.y); if (pData[current].y <= pt.y) { b = !b; } if (b) { if(((pData[next].x - pData[current].x) * (pt.y - pData[current].y) / (pData[next].y - pData[current].y)) <= (pt.x - pData[current].x)) { count++; } } } return (count & 1) ? TRUE : FALSE; }
void CBCGPVector4::CalcPlane(const CBCGPPointsArray& arPoints) { int i; const CBCGPPoint* pData = arPoints.GetData(); if (pData == NULL) { return; } CBCGPMatrix4x4 mA; for (i = 0; i < 3; i++) { mA[i][0] = 1.; mA[i][1] = pData[i].y; mA[i][2] = pData[i].z; } CBCGPMatrix4x4 mB; for (i = 0; i < 3; i++) { mB[i][0] = pData[i].x; mB[i][1] = 1.; mB[i][2] = pData[i].z; } CBCGPMatrix4x4 mC; for (i = 0; i < 3; i++) { mC[i][0] = pData[i].x; mC[i][1] = pData[i].y; mC[i][2] = 1.; } CBCGPMatrix4x4 mD; for (i = 0; i < 3; i++) { mD[i][0] = pData[i].x; mD[i][1] = pData[i].y; mD[i][2] = pData[i].z; } m_data[0] = mA.Det(3); m_data[1] = mB.Det(3); m_data[2] = mC.Det(3); m_data[3] = -mD.Det(3); }
int FindPointInPolygon(const CBCGPPointsArray& arPoly, const CBCGPPoint& pt) { int nSize = (int)arPoly.GetSize (); if (nSize == 0) { return -1; } int index = -1; const CBCGPPoint* pData = arPoly.GetData(); for (int i = 0; i < nSize; i++) { if (pData[i].x == pt.x && pData[i].y == pt.y) { index = i; break; } } return index; }
BOOL BCGPCalculateIntersectPoint(const CBCGPPointsArray& arPoly1, const CBCGPPointsArray& arPoly2, CBCGPPoint& ptIntersect) { int nPoly1Size = (int)arPoly1.GetSize(); int nPoly2Size = (int)arPoly2.GetSize(); if (nPoly1Size == 0 || nPoly2Size == 0) { return FALSE; } const CBCGPPoint* pData1 = arPoly1.GetData (); const CBCGPPoint* pData2 = arPoly2.GetData (); CBCGPPointsArray arPolyRes; arPolyRes.SetSize (0, nPoly1Size + nPoly2Size); int i = 0; int j = 0; // 1. find all points from arPoly1F in arPoly2F int nSize1 = 0; for (i = 0; i < nPoly1Size; i++) { const CBCGPPoint& pt = pData1[i]; if (PointInPolygon (arPoly2, pt)) { if (AddPointInPolygon(arPolyRes, pt)) { nSize1++; } } } // all points in arPoly2F if (nSize1 != nPoly1Size) { // 2. find all points from arPoly2F in arPoly1F int nSize2 = 0; for (i = 0; i < nPoly2Size; i++) { const CBCGPPoint& pt = pData2[i]; if (PointInPolygon (arPoly1, pt)) { if (AddPointInPolygon(arPolyRes, pt)) { nSize2++; } } } // all points in arPoly1F if (nSize1 != 0 || nSize2 != nPoly2Size) { // 3. find all intersection points arPoly1F and arPoly2F for (i = 0; i < nPoly1Size; i++) { const CBCGPPoint& ptStart1 = pData1[i]; const CBCGPPoint& ptEnd1 = pData1[(i + 1) % nPoly1Size]; for (j = 0; j < nPoly2Size; j++) { const CBCGPPoint& ptStart2 = pData2[j]; const CBCGPPoint& ptEnd2 = pData2[(j + 1) % nPoly2Size]; if (bcgp_classify_point2D(ptStart1, ptEnd1, ptStart2) != bcgp_classify_point2D(ptStart1, ptEnd1, ptEnd2)) { CBCGPPoint pt; if (BCGPIntersectPoints2D (ptStart1, ptEnd1, ptStart2, ptEnd2, pt)) { AddPointInPolygon(arPolyRes, pt); } } } } } } int nSize = (int)arPolyRes.GetSize(); if (nSize < 2) { return FALSE; } ptIntersect.x = 0.0; ptIntersect.y = 0.0; for (i = 0; i < nSize; i++) { ptIntersect += arPolyRes[i]; } ptIntersect /= nSize; return TRUE; }