Esempio n. 1
0
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;
}