- UID
- 1066743
|
计算[s,e]线段是否经过rc区域
/计算[s,e]线段是否经过rc区域
BOOL isLineInRetc(const RECT rc, const POINT s, const POINT e)
{
BOOL bRet = FALSE;
//两点式计算与边界的交点
// y = (y2-y1)/(x2-x1)*(x-x1) + y1;
double x1 = s.x, y1 = s.y;
double x2 = e.x, y2 = e.y;
double fYL = (y2-y1)/(x2-x1)*(rc.left - x1) + y1;
double fYR = (y2-y1)/(x2-x1)*(rc.right - x1) + y1;
double fXT = (x2-x1)/(y2-y1)*(rc.top - y1) + x1;
double fXB = (x2-x1)/(y2-y1)*(rc.bottom - y1) + x1;
//RECT范围
double fRMinx = rc.left;
double fRMaxx = rc.right;
double fRMiny = rc.top;
double fRMaxy = rc.bottom;
//线段范围
double fLMiny = min(y1, y2);
double fLMaxy = max(y1, y2);
double fLMinx = min(x1, x2);
double fLMaxx = max(x1, x2);
if(s.x >= fRMinx && s.x <= fRMaxx && s.y >= fRMiny && s.y <= fRMaxy)
{
//起点在区域内?
bRet |= 0x01;
}
if(e.x >= fRMinx && e.x <= fRMaxx && e.y >= fRMiny && e.y <= fRMaxy)
{
//结束点在区域内?
bRet |= 0x02;
}
//与左边界有交点 & 在线段内?
if(fYL >= fLMiny && fYL <= fLMaxy && fYL >= fRMiny && fYL <= fRMaxy)
{
bRet |= 0x10;
}
//与右边界有交点 & 在线段内?
if(fYR >= fLMiny && fYR <= fLMaxy && fYR >= fRMiny && fYR < fRMaxy)
{
bRet |= 0x20;
}
//与上边界有交点 & 在线段内?
if(fXT >= fLMinx && fXT <= fLMaxx && fXT >= fRMinx && fXT <= fRMaxx)
{
bRet |= 0x40;
}
//与下边界有交点 & 在线段内?
if(fXB >= fLMinx && fXB <= fLMaxx && fXB >= fRMinx && fXB <= fRMaxx)
{
bRet |= 0x80;
}
return bRet;
} |
|