33,008
社区成员
发帖
与我相关
我的任务
分享
size_t nSize = m_vcArea.size();
if (nSize <= 0)
{
nArea = -1; // out of range
return false;
}
if (nArea < 0 || (UINT)nArea > nSize-1)
{
nArea = -1; // out of range
return false;
}
int nRet = -1;
for(size_t i=nArea; i< nSize; i++)
{
int nLeft = 0;
UINT nCount = 0;
int nCurrent = 0;
double dX1, dY1, dX2, dY2;
if (m_vcArea[i].bEnabled == false) continue;
size_t nPoints = m_vcArea[i].vcPoints.size();
for(UINT j=0;j<nPoints;j++)
{
if (j != nPoints-1)
{
dX1 = m_vcArea[i].vcPoints[j].first;
dY1 = m_vcArea[i].vcPoints[j].second;
dX2 = m_vcArea[i].vcPoints[j+1].first;
dY2 = m_vcArea[i].vcPoints[j+1].second;
}
else
{
dX1 = m_vcArea[i].vcPoints[nPoints-1].first;
dY1 = m_vcArea[i].vcPoints[nPoints-1].second;
dX2 = m_vcArea[i].vcPoints[0].first;
dY2 = m_vcArea[i].vcPoints[0].second;
}
if (j == 0)
{
nLeft = OnLeft(dX1, dY1, dX2, dY2, dX, dY);
nCurrent = nLeft;
}
else
nCurrent = OnLeft(dX1, dY1, dX2, dY2, dX, dY);
if (nCurrent != nLeft) break;
if (nLeft == 0 || nCurrent == 0)
{
nArea = (int)i;
strName = m_vcArea[i].areaName;
return true;
}
nCount++;
}
if (nCount == nPoints)
{
nArea = (int)i;
strName = m_vcArea[i].areaName;
return true;
}
这段是OnLeft的实现:
double dResult = (dX2-dX1)*(dY-dY1)-(dY2-dY1)*(dX-dX1);
bool bLeft = false;
if (dResult >= -0.00000001 && dResult <= 0.0000001)
return 0;
if (dResult > 0.0f)
return -1;
if (dResult < 0.0f)
return 1;
return -2;
实际应用中,没见过矩形只用边长来描述的。俺前面说过了,脱离了实际存储方式,算法没有意义。